【问题标题】:Add delete button in custom Listview在自定义列表视图中添加删除按钮
【发布时间】:2016-05-10 00:53:09
【问题描述】:

首先,请原谅我蹩脚的英语。

我想知道当数据来自其他类时如何使用列表视图中的删除按钮删除项目。

我在 MainActivity 中使用了一个按钮,它将一些数据插入到称为昵称的数组中。

这是我的 CustomAdapter 类。

public class CustomAdapter extends ArrayAdapter<String>{

public CustomAdapter(Context context, String[] name) {
    super(context,R.layout.custom_row ,name);

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(getContext());
    final View customView = inflater.inflate(R.layout.custom_row, parent, false);


    final String singleName = getItem(position);
    TextView nickname = (TextView) customView.findViewById(R.id.nickname);

    nickname.setText(singleName);

    return customView;
}}

这是我在 MainActivity 类中的 onClick

  public void resultNickname(View view){
    nicknames[] = dbHandler.retrieveName();
    setContentView(R.layout.nickname_list);
    ListAdapter listAdapter = new CustomAdapter(this,nicknames);
    ListView nameListView = (ListView) findViewById(R.id.nickList);
    nameListView.setAdapter(listAdapter);


}

我认为我的问题可以根据这个问题的答案来解决。 android: how to delete a row from a ListView with a delete button in the row 但我无法让它工作,因为我不明白该项目的含义以及如何在我的代码中实现它。

有人可以帮我至少解释一下其他问题的含义以及如何在我的代码中实现它吗?

【问题讨论】:

    标签: android listview


    【解决方案1】:
     public class CustomAdapter extends ArrayAdapter<String>
     {
     Context c1;
     String s1[];
     int s2[];
     CustomAdapter(Context c,String s[],int s3[])
     {
     super(c,R.layout.tcustom,s);
      this.c1=c;
      this.s1=s;
      this.s2=s3;
      }
    
      public View getView(int position,View v,ViewGroup parent)
      {
          LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    
    v=li.inflate(R.layout.tcustom,null);
    TextView tv=(TextView)v.findViewById(R.id.textView);
    tv.setText(s1[position]);
    
    Button bt = (Button) v.findViewById(R.id.button);
    bt.setTag(position); //important so we know which item to delete on button click
    
    bt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            int positionToRemove = (int)v.getTag(); //get the position of the view to delete stored in the tag
            removeItem(positionToRemove);
        notifyDataSetChanged(); //remove the item
        }
    });
    
    return v;
    }
    
     public void removeItem(int position){
     //convert array to ArrayList, delete item and convert back to array
     ArrayList<String> a = new ArrayList<>(Arrays.asList(s1));
     a.remove(position);
     String[] s = new String[a.size()];
     s=a.toArray(s);
      s1 = s;
      notifyDataSetChanged(); //refresh your listview based on new data
    
     }
     public int getCount() {
     return s1.length;
     }
     public String getItem(int position) {
     return s1[position];
     }}
    

    【讨论】:

    • 非常感谢你的代码,我会学习代码并进一步理解它。
    【解决方案2】:

    尝试回答,首先我认为您应该使用删除按钮定义 onclick,然后使用行的位置删除列表/数组。

    【讨论】:

    • 是的,我知道我需要在自定义适配器中定义带有删除按钮的 onclick,并根据我发布的相关问题中的视图位置删除项目。无论如何,感谢您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多