![](/img/trans.png)
[英]android how to click the button which is in the listview item to delete that item
[英]Android - How to delete a custom listview item on button click?
我有一個自定義列表,其中包含兩個textview和一個刪除按鈕。 我想刪除listview項目當我點擊刪除按鈕我已經嘗試過這些答案從自定義列表視圖中刪除項目,從ListView中刪除所選項目 , 刪除Android中的ListView項目沒有運氣。
這是我的Adapter類
public class SpecialListAdapter extends BaseAdapter
{
Activity context;
List<String> id = new ArrayList<String>();
List<String> name = new ArrayList<String>();
List<String> newid = new ArrayList<String>();
List<String> newname = new ArrayList<String>();
ViewHolder holder;
public SpecialListAdapter(Activity context, List<String> id, List<String> name) {
super();
this.context = context;
this.id = id;
this.name = name;
}
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewID;
TextView txtViewName;
ImageButton btnDelete;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.special_list_item, null);
holder = new ViewHolder();
holder.txtViewID = (TextView) convertView.findViewById(R.id.specialId);
holder.txtViewName = (TextView) convertView.findViewById(R.id.specialName);
holder.txtViewID.setText(id.get(position));
holder.txtViewName.setText(name.get(position));
holder.btnDelete = (ImageButton) convertView.findViewById(R.id.bDelete);
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
id.remove(position);
name.remove(position);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
編輯
錯誤日志:
12-18 17:39:46.030: D/AndroidRuntime(17893): Shutting down VM
12-18 17:39:46.030: W/dalvikvm(17893): threadid=1: thread exiting with uncaught exception (group=0x40dc3930)
12-18 17:39:46.053: E/AndroidRuntime(17893): FATAL EXCEPTION: main
12-18 17:39:46.053: E/AndroidRuntime(17893): java.lang.UnsupportedOperationException
12-18 17:39:46.053: E/AndroidRuntime(17893): at java.util.AbstractList.remove(AbstractList.java:638)
12-18 17:39:46.053: E/AndroidRuntime(17893): at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
12-18 17:39:46.053: E/AndroidRuntime(17893): at java.util.AbstractCollection.remove(AbstractCollection.java:229)
12-18 17:39:46.053: E/AndroidRuntime(17893): at .SpecialListAdapter$1.onClick(SpecialListAdapter.java:83)
12-18 17:39:46.053: E/AndroidRuntime(17893): at android.view.View.performClick(View.java:4202)
12-18 17:39:46.053: E/AndroidRuntime(17893): at android.view.View$PerformClick.run(View.java:17340)
12-18 17:39:46.053: E/AndroidRuntime(17893): at android.os.Handler.handleCallback(Handler.java:725)
12-18 17:39:46.053: E/AndroidRuntime(17893): at android.os.Handler.dispatchMessage(Handler.java:92)
12-18 17:39:46.053: E/AndroidRuntime(17893): at android.os.Looper.loop(Looper.java:137)
12-18 17:39:46.053: E/AndroidRuntime(17893): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-18 17:39:46.053: E/AndroidRuntime(17893): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 17:39:46.053: E/AndroidRuntime(17893): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 17:39:46.053: E/AndroidRuntime(17893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-18 17:39:46.053: E/AndroidRuntime(17893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-18 17:39:46.053: E/AndroidRuntime(17893): at dalvik.system.NativeStart.main(Native Method)
你只是從“名稱” List
中刪除,你有
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
所以列表項的數量沒有變化。
添加到onClick
行id.remove(position);
,然后notifyDataSetChanged();
也把這個
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
id.remove(position);
name.remove(position);
notifyDataSetChanged();
}
});
outside / below if(convertView == null){...} else {...}
並將ViewHolder
在方法ViewHolder holder;
在此之上if
假設您的Activity類中有一個全局變量String Array ...
List<String> globalIDArray = new List<String>();
List<String> globalNamesArray = new List<String>();
您將適配器設置為列表,如下所示...
myListView.setAdapter(new SpecialListAdapter(getApplicationContext(), globalIDArray, globalNamesArray);
現在點擊按鈕...
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
globalIDArray.remove("id to remove");
globalNamesArray.remove("name to remove");
// now just invalidate the views and it will do remaining work automatically
myListView.invalidateViews();
}
}
編輯:
您可以在適配器的getView函數內實現刪除項目的功能。 像這樣......
public View getView(final int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.special_list_item, null);
holder = new ViewHolder();
holder.txtViewID = (TextView) convertView.findViewById(R.id.specialId);
holder.txtViewName = (TextView) convertView.findViewById(R.id.specialName);
holder.txtViewID.setText(id.get(position));
holder.txtViewName.setText(name.get(position));
holder.btnDelete = (ImageButton) convertView.findViewById(R.id.bDelete);
//
// Set tag to button so we can use ID and Name values at 'onClick'
//
holder.btnDelete.setTag(new ID_Name_Set(id.get(position), name.get(position)));
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// retrieve tag object and fetch ID and Name from this object
ID_Name_Set set = (ID_Name_Set)v.getTag();
id.remove(set.ID);
name.remove(set.Name);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private class ID_Name_Set
{
public String ID = "";
public String Name = "";
public ID_Name_Set(String id, String name)
{
this.ID = id;
this.Name = name;
}
}
我希望這有幫助。
:)
您應該通過以下方式將項目的位置添加到按鈕中,然后在onClick方法中,您也應該將其從實際列表中刪除。
holder.btnDelete = (ImageButton) convertView.findViewById(R.id.bDelete);
holder.btnDelete.setTag(position);
在你的onClick方法中,請添加以下新行:
int pos = (Integer)v.getTag();
//remove item from list and notifydatasetChanged
name.remove(pos);
notifyDataSetChanged();
還有一件事是,在“convertview!= null”的情況下,您還應該為視圖添加標簽。
我希望這會對你有所幫助,因為它可以幫助我解決我的問題。
問候。
List<String> id = new ArrayList<String>();
List<String> name = new ArrayList<String>();
應該
ArrayList<String> id;
ArrayList<String> name;
List
將不加修改,使用ArrayList
然后在你的構造函數中使用
this.id = new ArrayList<String>(id);
this.name = new ArrayList<String>(name);
if (convertView == null)
{
convertView = inflater.inflate(R.layout.special_list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewID = (TextView) convertView.findViewById(R.id.specialId);
holder.txtViewName = (TextView) convertView.findViewById(R.id.specialName);
holder.txtViewID.setText(id.get(position));
holder.txtViewName.setText(name.get(position));
holder.btnDelete = (ImageButton) convertView.findViewById(R.id.bDelete);
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
id.remove(position);
name.remove(position);
notifyDataSetChanged();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.