有人可以给我一个删除所有ListView项目并替换为新项目的示例代码吗?
我尝试更换适配器项目但没有成功。我的代码是
populateList(){
results //populated arraylist with strings
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
listview.setOnItemClickListener(this);
}
// now populating list again
repopulateList(){
results1 //populated arraylist with strings
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results1);
listview.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listview.setOnItemClickListener(this);
}
此处repopulateList()
方法将添加到ListView项目,但它不会删除/替换所有ListView项目。
答案 0 :(得分:71)
您需要remove()
来自适配器对象的项目,然后只需运行适配器上的notifyDatasetChanged()
,任何ListView
将(应该)回收并自行更新。
以下是AlertDialog
s的简短活动示例:
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
答案 1 :(得分:23)
我认为如果您添加以下代码,它将起作用
listview.invalidateViews();
要删除项目,只需从我们传递给适配器的arraylist中删除该项目并执行listview.invalidateViews();
这将刷新列表视图
答案 2 :(得分:19)
您可以使用
adapter.clear()
将删除第一个适配器的所有项目,然后您可以设置另一个适配器或重新使用适配器并将项目添加到旧适配器。如果你使用
adapter.add()
将数据添加到列表中,您无需调用notifyDataSetChanged
答案 3 :(得分:15)
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
adapter.remove(adapter.getItem(i));
}
然后致电notifyDataSetChanged();
答案 4 :(得分:9)
从适配器中删除它,然后通知arrayadapter数据集已更改。
m_adapter.remove(o);
m_adapter.notifyDataSetChanged();
答案 5 :(得分:4)
names = db.getSites();
la = new ArrayAdapter<String>(EditSiteList.this,
android.R.layout.simple_list_item_1, names);
EditSiteList.this.setListAdapter(la);
listview.invalidateViews();
这段代码对我来说很好。
答案 6 :(得分:4)
这里有一个例子:
List<String> myList = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(this);
populateList(){
List<String> result = getDataMethods();
myList.addAll(result);
adapter.notifyDataSetChanged();
}
repopulateList(){
List<String> result = getDataMethods();
myList.clear();
myList.addAll(result);
adapter.notifyDataSetChanged();
}
答案 7 :(得分:3)
试试这段代码,它对我有用。
public class Third extends ListActivity {
private ArrayAdapter<String> adapter;
private List<String> liste;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
liste = new ArrayList<String>();
Collections.addAll(liste, values);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, liste);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
liste.remove(position);
adapter.notifyDataSetChanged();
}
}
答案 8 :(得分:1)
首先,您应该从列表中删除该项目。稍后您可以清空适配器并用新列表重新填充。
private void add(final List<Track> trackList) {
MyAdapter bindingData = new MyAdapter(MyActivity.this, trackList);
list = (ListView) findViewById(R.id.my_list); // TODO
list.setAdapter(bindingData);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
// ShowPlacePref(places, position);
AlertDialog.Builder showPlace = new AlertDialog.Builder(
Favoriler.this);
showPlace.setMessage("Remove from list?");
showPlace.setPositiveButton("DELETE", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
trackList.remove(position); //FIRST OF ALL REMOVE ITEM FROM LIST
list.setAdapter(null); // THEN EMPTY YOUR ADAPTER
add(trackList); // AT LAST REFILL YOUR LISTVIEW (Recursively)
}
});
showPlace.setNegativeButton("CANCEL", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
showPlace.show();
}
});
}
答案 9 :(得分:1)
这很简单。首先,您应该清除您的收藏,并清除清单,如下代码:
yourCollection.clear();
setListAdapter(null);
答案 10 :(得分:0)
您还可以使用listView.setOnItemLongClickListener删除所选项目。 下面是代码。
// listView = ListView的名称
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int
position, long id) {
// it will get the position of selected item from the ListView
final int selected_item = position;
new AlertDialog.Builder(MainActivity.this).
setIcon(android.R.drawable.ic_delete)
.setTitle("Are you sure...")
.setMessage("Do you want to delete the selected item..?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
list.remove(selected_item);
arrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No" , null).show();
return true;
}
});