当我点击所选项目时,我试图从列表中删除一个项目,但我在logcat上收到错误,说AddapterView不支持removeViewAt方法。知道我可以在这里使用什么来实现这个目标吗?
非常感谢。 这是我的代码。
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
listView.removeViewAt(position);
listView.invalidateViews();
}
});
}
答案 0 :(得分:1)
试试这个:
ArrayList<String> arrlist=new ArrayList<String>();
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
ListView listView = (ListView) findViewById(R.id.mylist);
String[] items = { "Milk", "Butter", "Yogurt", "Toothpaste", "Ice Cream" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
items.remove(position);
adapter.notifyDataSetChanged();
}
});
}
答案 1 :(得分:0)
这会使用动画消除所点击的项目,需要API level 16
private List<String> myList;
File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listerX);
myList = new ArrayList<String>();
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/myAppCache/" );
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable() {
@Override
public void run()
{
Toast.makeText(getApplicationContext(), ""+item,
Toast.LENGTH_LONG).show();
}
});
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter); //Set all the file in the list.
}
答案 2 :(得分:0)
您使用任何数组或集合项填充列表 然后在点击位置的位置删除项目。 现在通过调用adapter.notifydatasetchange()来通知适配器; 享受您的代码