简体   繁体   中英

How Do I Remove List View Item On Button Click From The Array List?

带删除按钮的列表视图

In my app I am working with a List View that uses ArrayList. Basically all I need help with is removing the item when the user taps on the remove button. I added the remove button to my item list layout. So each time the user adds an item to the list the remove button will show next to it. I just need that remove button to remove the corresponding item.

Here's my code...

Main XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="650dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="650dp">

        <ListView
            android:id="@+id/lstView"
            android:layout_width="match_parent"
            android:layout_height="650dp"
            tools:ignore="NestedScrolling" />

    </RelativeLayout>
</ScrollView>

<include layout="@layout/add_and_clear_list_buttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>

Item List XML:

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

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<TextView
    android:id="@+id/item_tv"
    android:layout_width="315dp"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textSize="20sp"
    android:textColor="@android:color/black"/>

    <Button
        android:id="@+id/remove_item_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="14sp"
        android:textStyle="bold" />

</LinearLayout>
</ScrollView>

Java Class:

public class MainActivity extends AppCompatActivity {

ListView lstVw;
Button addBtn, removeBtn, clearListBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lstVw = findViewById(R.id.lstView);
    addBtn = findViewById(R.id.add_item_btn);
    removeBtn = findViewById(R.id.remove_item_btn);
    clearListBtn = findViewById(R.id.clear_list_btn);

    final ArrayAdapter<String> adapter;
    final ArrayList<String> arrayList;

    arrayList = new ArrayList<>();
    adapter = new ArrayAdapter<>(this, R.layout.item_list, R.id.item_tv, arrayList);
    lstVw.setAdapter(adapter);

    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
            adb.setTitle("Enter Item Name");
            final EditText itemTxt = new EditText(MainActivity.this);
            itemTxt.setText(getString(R.string.default_item_name_value));
            itemTxt.setInputType(InputType.TYPE_CLASS_TEXT);
            adb.setView(itemTxt);

            adb.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String getItem = itemTxt.getText().toString();

                    Set<String> s = new LinkedHashSet<>(arrayList); 
                    if (s.contains(getItem)){
                        arrayList.clear();
                        arrayList.addAll(s);
                        Toast.makeText(getApplicationContext(), getItem + " already exists in the list!", Toast.LENGTH_LONG).show();
                    }else{
                        arrayList.add(getItem);
                        adapter.notifyDataSetChanged();
                    }
                }
            });

            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {}
            });

            adb.create();
            adb.show();
        }
    });

    clearListBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!arrayList.isEmpty()) {
                arrayList.clear();
                adapter.notifyDataSetChanged();
                }
            }
        });
    }
}

Thanks!

Basically you need to add a listener to the list view and expose the position of the item that received a tap, then the Remove button can deletes the item by position.

See this question How to get the selected item from ListView? to get Listener implementation

To remove the item in your MainActivity

removeBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            list.remove(position); // remove the item
            notifyDataSetChanged(); // update de dataset 
        }
    });
    yourBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
       View parentRow = (View) v.getParent();
       ListView listView = (ListView) parentRow.getParent();
       final int position = listView.getPositionForView(parentRow);
      youArrayList.remove(position);
      yourArrayAdapter.notifyDataSetChanged();
    }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM