每次單擊按鈕時,我都需要更新我的ListView
內容(從數據庫中檢索)。我正在嘗試在我的android應用程序中包含圖書搜索功能。如何每次點擊按鈕時更新列表視圖內容?
爲此,我將一些關鍵字作爲輸入。在按鈕上單擊從數據庫中檢索與關鍵詞匹配的書籍詳細信息,並以相同佈局顯示在列表視圖中。
我已經寫將創建一個新ListView
與每個按鈕上點擊更新內容,並跌破前期ListView
。其中我需要正確的顯示它的代碼。
首選的解決方案是按鈕點擊更新列表視圖內容。 刪除之前顯示的ListView
也是可以接受的。
爲此我試過getListView().inValidate()
但沒有工作。 notifyDataSetChanged()
也不能正常工作,請您及時的幫助:)
public class searchActivity extends ListActivity {
String keyword;
Button bsearch;
EditText input;
JSONParser jParser = new JSONParser();
JSONArray books = null;
SimpleAdapter adapter;
ArrayList<HashMap<String, String>> booksList;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState{
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
bsearch=(Button)findViewById(R.id.search);
input=(EditText)findViewById(R.id.inputSearch);
booksList = new ArrayList<HashMap<String, String>>();
adapter=newSimpleAdapter(searchActivity.this,booksList,R.layout.search_list,new String[]{"bookname"},new int[] {R.id.bookname});
getListView().setAdapter(adapter);
bsearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
keyword=input.getText().toString();
new LoadAll().execute();
}
});
}
class LoadAll extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("keyword", keyword));
JSONObject json = Jparser.makeHttpRequest("http://10.0.2.2 /libraryconnect/search_book.php", "GET", params);
Log.d("All books: ", json.toString());
try {
int success = json.getInt("success");
if (success == 1) {
books = json.getJSONArray("books");
for (int i = 0; i < books.length(); i++) {
JSONObject c = books.getJSONObject(i);
String name = c.getString("bookname");
HashMap<String, String> map = new HashMap<String, String>();
map.put("bookname",bookname);
booksList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
}
}
}
上無效,不需要(),notifyDataSetChanged()直接更新適配器添加到列表中的buttonclicklistener – Venkat 2013-04-26 17:47:40
我想我會做setAdapter上的onCreate,不onPostExecute – Duc 2013-04-27 09:33:40
@Duc也試過。但沒有改變! – Revathy 2013-04-27 09:51:07