首页 > 解决方案 > 如何从数组列表中删除按钮单击时的列表视图项?

问题描述

带有删除按钮的列表视图

在我的应用程序中,我正在使用使用 ArrayList 的列表视图。基本上,我需要帮助的只是当用户点击删除按钮时删除该项目。我将删除按钮添加到我的项目列表布局中。因此,每次用户将项目添加到列表时,删除按钮都会显示在它旁边。我只需要那个删除按钮来删除相应的项目。

这是我的代码...

主要 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"/>

项目列表 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 类:

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

谢谢!

标签: javaandroid

解决方案


基本上,您需要在列表视图中添加一个侦听器并公开接收到点击的项目的位置,然后“删除”按钮可以按位置删除该项目。

看到这个问题 How to get the selected item from ListView? 获取监听器实现

删除 MainActivity 中的项目

removeBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            list.remove(position); // remove the item
            notifyDataSetChanged(); // update de dataset 
        }
    });

推荐阅读