如何在Android Studio的列表视图中添加编辑和删除操作

时间:2018-06-07 18:03:06

标签: listview android-studio-3.0

我试图制作一个"待办事项列表"用户最初将项添加到ListView的程序: enter image description here

在列表视图中,用户可以编辑或删除列表项: enter image description here

我的问题是如何将操作添加到ListView?

这是我的片段文件,其中应以编程方式添加,编辑和删除项目:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;


import java.util.ArrayList;


public class GroceryListFragment extends android.support.v4.app.Fragment {


    private static final String KEY_ITEM = "item";

    private ListView mShoppingList;
    private EditText mItemEdit;
    private Button mAddButton;
    private Button mDelButton;
    private Button mEditButton;
    private ArrayAdapter<String> mAdapter;
    private ArrayList<String> mItemsList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_grocery_list, container, false);

        mShoppingList = (ListView) view.findViewById(R.id.shopping_listView);
        mItemEdit = (EditText) view.findViewById(R.id.item_editText);
        mAddButton = (Button) view.findViewById(R.id.add_button);
        mAddButton = (Button) view.findViewById(R.id.dele_btn);
        mEditButton = (Button) view.findViewById(R.id.edit_btn);

        mAdapter = new ArrayAdapter<>(this.getContext(), android.R.layout.simple_list_item_1);
        mShoppingList.setAdapter(mAdapter);

        if (savedInstanceState == null) {
            mItemsList = new ArrayList<>();
        } else {
            mItemsList = savedInstanceState.getStringArrayList(KEY_ITEM);
            mAdapter.addAll(mItemsList);
        }

        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item = mItemEdit.getText().toString();
                mAdapter.add(item);
                mItemsList.add(item);
                mAdapter.notifyDataSetChanged();
                mItemEdit.setText("");
            }
        });

        mDelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item = mItemEdit.getText().toString();
                mAdapter.remove(item);
                mItemsList.remove(item);
                mAdapter.notifyDataSetChanged();
                mItemEdit.setText("");
            }
        });

        mEditButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item = mItemEdit.getText().toString();
                mAdapter.add(item);
                mItemsList.add(item);
                mAdapter.notifyDataSetChanged();
                mItemEdit.setText("");
            }
        });

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putStringArrayList(KEY_ITEM, mItemsList);
    }
}

这是我的XML文件,我试图插入两个按钮,以便理论上显示每个项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:orientation="vertical">

    <ListView
        android:id="@+id/shopping_listView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ImageButton
            android:id="@+id/edit_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="79dp"
            android:src="@drawable/ic_action_edit" />

        <ImageButton
            android:id="@+id/dele_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="13dp"
            android:src="@drawable/ic_action_delete" />

    </ListView>

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

        <EditText
            android:id="@+id/item_editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/item_editText_hint"/>

        <Button
            android:id="@+id/add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add_button_text"/>
    </LinearLayout>

</LinearLayout>

当我尝试在listview中嵌入按钮时,我在LogCat中收到以下错误:

enter image description here

有什么我可以尝试让程序工作吗?如果问题是重复问题,我真诚地道歉。

PS:我已经在应用程序崩溃的地方设置了一个断点,这就是我所拥有的:

enter image description here

0 个答案:

没有答案