Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用删除按钮从listview中删除项目_Android_Listview - Fatal编程技术网

Android 如何使用删除按钮从listview中删除项目

Android 如何使用删除按钮从listview中删除项目,android,listview,Android,Listview,我想在edittext下面的listview中添加项目。 这很好用 现在我想能够删除一些条目。这是我不知道怎么做的。我正在为delete按钮实现onclicklistener,但程序一直崩溃。我想这是因为删除按钮不存在。 我试着在网上找东西,但从来没有得到一个我可以使用的答案。 我想你可以帮我,那将是一个巨大的帮助 PS:并非所有这些代码都来自我,我在以下网站上找到了一些示例代码: 以下是我的主要活动: /** Note that here we are inheriting ListA

我想在edittext下面的listview中添加项目。 这很好用

现在我想能够删除一些条目。这是我不知道怎么做的。我正在为delete按钮实现
onclicklistener
,但程序一直崩溃。我想这是因为删除按钮不存在。 我试着在网上找东西,但从来没有得到一个我可以使用的答案。 我想你可以帮我,那将是一个巨大的帮助

PS:并非所有这些代码都来自我,我在以下网站上找到了一些示例代码:

以下是我的主要活动:

    /** Note that here we are inheriting ListActivity class instead of Activity class **/
    public class MainActivity extends ListActivity {
    Button del;
    ListView lv;

    /** Items entered by the user is stored in this ArrayList variable */
    ArrayList<String> list = new ArrayList<String>();





    /** Declaring an ArrayAdapter to set items to ListView */
    ArrayAdapter<String> adapter;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);







        /** Setting a custom layout for the list activity */
        setContentView(R.layout.main);

        del = (Button) findViewById(R.id.removeButton);

        /** Reference to the button of the layout main.xml */
        Button btn = (Button) findViewById(R.id.btnAdd);

        /** Defining the ArrayAdapter to set items to ListView */
        adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.nameText, list);

        /** Defining a click event listener for the button "Add" */
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText edit = (EditText) findViewById(R.id.txtItem);
                list.add(edit.getText().toString());
                edit.setText("");
                adapter.notifyDataSetChanged();

            }
        };



        del.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int pos = v.getId();
                System.out.println(pos);
                Object toRemove = adapter.getItem(pos);
                adapter.remove(toRemove);
            }
        });


        /** Setting the event listener for the add button */
        btn.setOnClickListener(listener);

        /** Setting the adapter to the ListView */
        setListAdapter(adapter);
    }
}

NullPointerException的原因是,在设置活动的内容视图之前,您正在设置
del
的值。在设置内容视图(视图)之后移动
del=findViewById(id)


至于删除内容,您需要从适配器中删除,而不是从列表中删除。您不需要保存自己的项目副本(除非您将它们用于其他用途),因为数组中有一个项目列表。您需要调用
adapter.remove(item)
而不是
list.remove(item)

请从日志添加完整的堆栈跟踪
del=(Button)findViewById(R.id.removeButton)另外,您如何选择要删除的内容?在每个条目上都有一个删除按钮您是否可以更新代码部分以反映您的更改并指出引发异常的行?在setContentView(view)也更改了列表之后移动del=fiendViewById(id)。连接到适配器。当我将Onclicklistener放在两个应用程序之间时,仍然得到相同的NullPointerException//应用程序没有崩溃,所以我认为问题出在那里的某个地方。
<?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="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/txtItem"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="@string/hintTxtItem"
    />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/lblBtnAdd"
        android:layout_toRightOf="@id/txtItem"
        android:onClick="removeAtomPayOnClickHandler"

    />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtItem"
    />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtItem"
        android:text="@string/txtEmpty"
        android:gravity="center_horizontal"
    />



</RelativeLayout>
 <?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="fill_parent"
    android:orientation="horizontal" >


   <TextView 
    android:id="@+id/nameText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="15dp"
    android:textSize="20sp" >


   </TextView>

   <Button
    android:id="@+id/removeButton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/remove"
  android:layout_alignParentRight="true"

   />

  </RelativeLayout>
11-12 17:37:57.637: D/AndroidRuntime(302): Shutting down VM
11-12 17:37:57.637: W/dalvikvm(302): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-12 17:37:57.667: E/AndroidRuntime(302): FATAL EXCEPTION: main
11-12 17:37:57.667: E/AndroidRuntime(302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.additemsdynami/com.example.additemsdynami.MainActivity}: java.lang.NullPointerException
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.os.Looper.loop(Looper.java:123)
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-12 17:37:57.667: E/AndroidRuntime(302):  at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:37:57.667: E/AndroidRuntime(302):  at java.lang.reflect.Method.invoke(Method.java:521)
11-12 17:37:57.667: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-12 17:37:57.667: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-12 17:37:57.667: E/AndroidRuntime(302):  at dalvik.system.NativeStart.main(Native Method)
11-12 17:37:57.667: E/AndroidRuntime(302): Caused by: java.lang.NullPointerException
11-12 17:37:57.667: E/AndroidRuntime(302):  at com.example.additemsdynami.MainActivity.onCreate(MainActivity.java:69)
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-12 17:37:57.667: E/AndroidRuntime(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-12 17:37:57.667: E/AndroidRuntime(302):  ... 11 more