簡體   English   中英

如何從數組列表中刪除單擊按鈕時的列表視圖項?

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

帶刪除按鈕的列表視圖

在我的應用程序中,我正在使用使用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();
                }
            }
        });
    }
}

謝謝!

基本上,您需要在列表視圖中添加一個偵聽器,並公開接收點擊的項目的位置,然后“刪除”按鈕可以按位置刪除該項目。

看到這個問題。 如何從ListView獲取選定的項目? 獲取監聽器實現

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM