我是Android开发的新手,并且仍在学习一些技能,但是在使用ListView和ArrayAdapter删除项目时遇到了问题。我一直在尝试大量的例子和阅读尽可能多的材料,但我想我可能会遗漏一些东西。对于我的生活,我无法在我的代码中使用它。
任何帮助将不胜感激。我在这里包含了代码。
public class Favourites extends ListActivity implements OnClickListener {
private static final String TAG = "favourites";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favourites);
// Test list of strings- eventually to be replaced
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
// First paramenter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
setListAdapter(adapter);
ListView list = getListView();
Log.d(TAG,"adapter count: " + adapter.getCount());
// Define listView Long Click listener
list.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String) getListAdapter().getItem(position);
// Remove Item
AlertDialog.Builder adb = new AlertDialog.Builder(Favourites.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to remove " + item +" (" + position + ")");
final int positionToRemove = position;
final String removeItem = item;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// *** Here is where I am experiencing issues ***
adapter.remove(positionToRemove);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),
"Favourite "+ removeItem +" (" + positionToRemove + ") Removed!", Toast.LENGTH_LONG).show();
}
});
adb.show();
// Return true to consume the click event. In this case the
// onListItemClick listener is not called anymore.
return true;
}
});
// Close button
View butClose = findViewById(R.id.closeButton);
butClose.setOnClickListener(this);
// Add button
View butAdd = findViewById(R.id.addButton);
butAdd.setOnClickListener(this);
}
答案 0 :(得分:1)
您实际上并未直接从适配器中删除。您必须首先从ArrayList中删除数据。 所以,改变
adapter.remove(positionToRemove);
adapter.notifyDataSetChanged();
到
values.remove(positionToRemove);
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
对我有用的代码是:
public void onClick(View v)
{
if (idx != 0)
{
String delete = (String) ((OdrLst.getAdapter()).getItem(idx));
//Log.d("Itemdeleted",delete);
adapter.remove(delete);
adapter.notifyDataSetChanged();
OdrLst.setAdapter(adapter);
//Log.d("adapter count after", adapter.getCount() + "");
//Log.d("lv count after", OdrLst.getCount() + "");
}
}
idx它应该是你的索引位置。
答案 2 :(得分:0)
您可以尝试使用对象“适配器”调用getListAdapter()
。像
getListAdapter().remove(positionToRemove);
getListAdapter().notifyDataSetChanged();
答案 3 :(得分:0)
我在HTC野火和Nexus 7上遇到类似的问题,ListView
在删除项目后没有刷新屏幕。然而,一些设备没有遇到这样的问题 - 例如三星S II没有问题
修复很奇怪(至少对我而言),我在布局中从background="@android:color/black"
中取出了ListView
行,一切都运行良好。