我有一个ListView。我要动态地更改使用的listview.I的内容adapter.notifyDataSetChanged();改变的内容。我也曾尝试设置列表视图的适配器为空,但我仍得到相同的结果。这是令人耳目一新,但不清除列表视图,而不是它与previous结果追加吧。我要的是previous内容应该被删除,新的结果应该显示出来。
我无法明白哪里是我的错误,我所缺少的。
我会很感激,如果有人可以请帮我出这一点。
下面是code生成列表视图
公共无效ListDrawer(){
尝试 {
JSONObject的jsonResponse =新的JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray(myRegisteredList);
的for(int i = 0; I< jsonMainNode.length();我++){
的JSONObject jsonChildNode = jsonMainNode.getJSONObject(ⅰ);
字符串registered_name = jsonChildNode.optString(研讨会);
字符串组织= jsonChildNode.optString(组织者);
字符串sem_id = jsonChildNode.optString(ID);
串r_sem = jsonChildNode.optString(seminar_id);
registeredlist =新的HashMap<字符串,字符串>();
registeredlist.put(registeredList,registered_name);
registeredlist.put(seminar_id,r_sem);
registeredlist.put(sem_id,sem_id);
registeredlist.put(组织者,组织);
registeredList.add(registeredlist);
}
}赶上(JSONException E){
。//Toast.makeText(this,e.getMessage()的toString()+1,Toast.LENGTH_LONG).show();
Toast.makeText(ViewReservedSeminarList.this,对不起......你没有任何保留的研讨会/秒,Toast.LENGTH_LONG).show();
}
尝试{
simpleAdapter =新SimpleAdapter(这一点,registeredList,
android.R.layout.simple_list_item_2,
新的String [] {registeredList,组织者,seminar_id},新的INT [] {android.R.id.text1,android.R.id.text2,android.R.id.title});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>一种,视图V,INT I,长L){
// TODO自动生成方法存根
@燮pressWarnings(未登记)
HashMap的<字符串,字符串> selected_row =(&HashMap中所述;字符串,字符串&GT)simpleAdapter.getItem(ⅰ);
S_ID = selected_row.get(seminar_id);
意向意图=新的意图(ViewReservedSeminarList.this,ReservedSeminar.class);
startActivity(意向);
//Toast.makeText(ViewReservedSeminarList.this,选定条目:+ S_ID,Toast.LENGTH_LONG).show();
}
});
}赶上(例外五){
。//Toast.makeText(this,e.getMessage()的toString()+2,Toast.LENGTH_LONG).show();
Toast.makeText(ViewReservedSeminarList.this,没有数据被取出,Toast.LENGTH_LONG).show();
}
}
下面是我放置在onCreate方法来刷新列表视图每10秒的片段。我呼吁下accessWebService方法ListDrawer方法。
最后的处理程序处理程序=新的处理程序();
handler.postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
accessWebService();
handler.postDelayed(此,10 * 1000);
Log.i(TAG,通知);
}
},10 * 1000
);
解决方案
试试这个,请:
清除该ArrayList / HashMap的的
添加新项的
adapter.notifyDataSetChanged();
设置适配器。的
I have a ListView. I want to dynamically change the contents of the listview.I used "adapter.notifyDataSetChanged();" to change the contents. I have also tried setting the adapter of the listview to null but still I get the same results. It is refreshing but not clearing the listview, instead it is appending it with the previous results. What i want is the previous contents should be deleted and the new results should be displayed.
I am not able to understand where is my mistake and what i am missing.
I would be Thankful if someone could please help me out with this.
Below is the code that generates the listview
public void ListDrawer() {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("myRegisteredList");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String registered_name = jsonChildNode.optString("seminar");
String org = jsonChildNode.optString("organizer");
String sem_id = jsonChildNode.optString("id");
String r_sem = jsonChildNode.optString("seminar_id");
registeredlist = new HashMap<String, String>();
registeredlist.put("registeredList", registered_name);
registeredlist.put("seminar_id", r_sem);
registeredlist.put("sem_id", sem_id);
registeredlist.put("organizer", org);
registeredList.add(registeredlist);
}
} catch (JSONException e) {
//Toast.makeText(this, e.getMessage().toString() + " 1", Toast.LENGTH_LONG).show();
Toast.makeText(ViewReservedSeminarList.this, "Sorry...You don't have any reserved seminar/s.", Toast.LENGTH_LONG).show();
}
try{
simpleAdapter = new SimpleAdapter(this, registeredList,
android.R.layout.simple_list_item_2,
new String[] { "registeredList" ,"organizer", "seminar_id" }, new int[] { android.R.id.text1 , android.R.id.text2 , android.R.id.title });
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// TODO Auto-generated method stub
@SuppressWarnings("unchecked")
HashMap<String, String> selected_row = (HashMap<String, String>) simpleAdapter.getItem(i);
s_id = selected_row.get("seminar_id");
Intent intent = new Intent(ViewReservedSeminarList.this, ReservedSeminar.class);
startActivity(intent);
//Toast.makeText(ViewReservedSeminarList.this, "Selected Item : "+s_id, Toast.LENGTH_LONG).show();
}
});
}catch(Exception e){
//Toast.makeText(this, e.getMessage().toString() + " 2", Toast.LENGTH_LONG).show();
Toast.makeText(ViewReservedSeminarList.this, "There is no data being fetched", Toast.LENGTH_LONG).show();
}
}
Below is the snippet I placed on the onCreate method to refresh the listview every 10 seconds. I am calling the ListDrawer method under the accessWebService method.
final Handler handler = new Handler();
handler.postDelayed( new Runnable(){
@Override
public void run(){
accessWebService();
handler.postDelayed(this, 10 * 1000);
Log.i("TAG", "notify");
}
}, 10 * 1000
);
解决方案
Try this please:
Clear the arraylist/HashMap
Add new items
adapter.notifyDataSetChanged();
set adapter.