如何在android中的每个List项中单击删除按钮时删除listview项

时间:2017-04-25 18:35:36

标签: android listview android-arrayadapter

当我单击每个listItem中的删除文本时,我想删除列表项。我尝试了不同的方法,但对我来说没有任何作用

我有MainActivity,自定义数组适配器和listview XML文件。当我点击删除文本查看它时,我的所有列表行都有一个删除文本视图,需要从列表中删除特定项目。

这是我的代码:

MainActivity.java

public class MainActivity extends Activity {
    ListView listView;
    ImageButton delete;
    private List<ListItems> listItems = new ArrayList<ListItems>();
    ArrayAdapter arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        //assigning another class list to listItems
        listItems = DataPopulate.data;
        arrayAdapter = ArrayAdapter(this, listItems);
    }
}

ArrayAdapter.java

public class ArrayAdapter extends BaseAdapter {
    private static final String TAG = ArrayAdapter.class.getSimpleName();
    TextView title;
    TextView count;
    static TextView delete;
    private Activity activity;
    private LayoutInflater inflater;
    private List<ListItems> listItems;


    public ArrayAdapter(Activity activity, List<ListtItems> listItems) {
        this.activity = activity;
        this.listItems = listItems;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int location) {
        return listItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.order_list, null);

        title = (TextView) convertView.findViewById(R.id.title);
        count = (TextView) convertView.findViewById(R.id.count);
        delete = (TextView) convertView.findViewById(R.id.delete);
        ListItems c = listItems.get(position);
        title.setText(c.getName());
        count.setText(String.valueOf(c.getItemCount()));

        return convertView;
    }
}

order_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/title"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/title"
        />

    <TextView
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancel"
        android:layout_toRightOf="@id/price"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:3)

尝试

convertView.setOnClickListener(new OnClickListener () {

    @Override
    public void onClick(View v) {
        listItems.remove(position) ;
        notifyDataSetChanged() ;
   } 
});  

答案 1 :(得分:2)

您只需要从ArrayList中删除项目,即填充适配器。然后在适配器上使用它:

notifyDataSetChanged()