【问题标题】:Android - How to delete a custom listview item on button click?Android - 如何在单击按钮时删除自定义列表视图项?
【发布时间】:2014-12-18 10:37:29
【问题描述】:

我有一个自定义列表,其中包含两个两个文本视图和一个删除按钮。单击删除按钮时,我想删除列表视图项 我已经尝试了这些答案delete item from custom listviewRemove selected item from ListViewRemove ListView items in Android,但没有运气。

这是我的适配器类

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)

【问题讨论】:

  • 你试过我的解决方案了吗?

标签: android android-listview custom-lists


【解决方案1】:

您应该按照此将项目的位置添加到您的按钮中,然后在 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”的情况下也是如此。

我希望这会对你有所帮助,因为它可以帮助我解决我的问题。

问候。

【讨论】:

  • 我试过你的代码,但它不起作用!它在“onClick”上给了我一个 java.lang.UnsupportedOperationException
  • 你能把完整的错误日志写在这里,让我看看。
【解决方案2】:

假设您的 Activity 类中有一个用于字符串数组的全局变量...

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;
    }
}

我希望这会有所帮助。

:)

【讨论】:

  • 如何从我的 Activity 类中访问删除按钮!
  • 在 id.remove(set.ID) 上仍然给我同样的错误。
【解决方案3】:

您仅从“名称”List 中删除并且您拥有

public int getCount() {
// TODO Auto-generated method stub
    return id.size();
}

所以列表项的数量不会改变。

添加到onClickid.remove(position);,然后添加notifyDataSetChanged();

也放这个

holder.btnDelete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            id.remove(position);
            name.remove(position);
            notifyDataSetChanged();

        }
    });

if(convertView == null){...} else {...} 的外部/下方并保持ViewHolder 内部方法ViewHolder holder; 在此if 上方

【讨论】:

  • 我已经写了 id.remove()。只需水平滚动,您就会看到它。并将 setOnClickListener 移到 if(convertView == null){...} else {...} 给我 java.lang.UnsupportedOperationException at id.remove(position)
【解决方案4】:
    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();

        }
    });

【讨论】:

    【解决方案5】:
    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 2013-09-12
      相关资源
      最近更新 更多