将列表项添加到另一个列表

时间:2016-04-01 10:59:37

标签: android listview arraylist

我有两个列表视图。我想将列表项从One List视图添加到另一个。 当Item添加到另一个列表时,item应从第一个列表中删除。 这是我的 EventDetailActivity 代码。

public class EventDetailActivity extends AppCompatActivity {

    private ListView myListViewEventDetailList, myListViewAddEventDetailList;
    String[] eventItemList;
    Context context;
    ArrayList<String> addEventList = new ArrayList<String>();
    ArrayAdapter<String> arrayAdapter;

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

        myListViewEventDetailList = (ListView) findViewById(R.id.listViewEventDetailList);
        myListViewAddEventDetailList = (ListView) findViewById(R.id.listViewAddEventDetailList);
        context = this;

        String[] eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.event_detail_item,eventItemList);
        myListViewEventDetailList.setAdapter(adapter);

        myListViewEventDetailList.setItemsCanFocus(false);
        myListViewEventDetailList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        myListViewEventDetailList.setOnItemClickListener(new OnCheckBoxClick());

        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, addEventList);

    }

    private class OnCheckBoxClick implements android.widget.AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                addEventList.add(eventItemList[position]);
        }
    }
}

当我点击listItem添加项目时,会出现尝试从空数组中读取的错误 我怎么能这样做?

谢谢。

这是我的错误:

  

致命的例外:主要                                                                                处理:com.sabhagruh.ysgoperations,PID:28001                                                                                java.lang.NullPointerException:尝试从空数组中读取                                                                                    在   com.sabhagruh.ysgoperations.EventDetailActivity $ OnCheckBoxClick.onItemClick(EventDetailActivity.java:45)                                                                                    在android.widget.AdapterView.performItemClick(AdapterView.java:305)                                                                                    在android.widget.AbsListView.performItemClick(AbsListView.java:1146)                                                                                    在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3053)                                                                                    在android.widget.AbsListView $ 3.run(AbsListView.java:3860)                                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:135)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5343)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在java.lang.reflect.Method.invoke(Method.java:372)                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

我也想删除已选中的事件列表 我做了这个代码.. 但没有工作。

 private class OnCheckBoxClick implements android.widget.AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            addEventList.add(eventItemList[position]);
            arrayAdapter.notifyDataSetChanged();
          /*  adapter.remove(adapter.getItem(position));
            myListViewEventDetailList.invalidateViews();
            adapter.notifyDataSetChanged();*/



        }
    }

2 个答案:

答案 0 :(得分:2)

onCreate更改此行

 String[] eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"};

  eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"};

因为您在oncreate中创建另一个局部变量eventItemList而不是使用成员变量

同时添加

myListViewAddEventDetailList.setAdapter(arrayAdapter)

的最后一行之后

onCreate

arrayAdapter.notifyDataSetChanged()方法

中的addEventList.add(eventItemList[position]);之后

onItemClick

答案 1 :(得分:1)

您正在隐藏eventItemList属性

替换

String[] eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"}

通过:

eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"}

您的实际代码仅在onCreate方法中声明一个局部变量,并且对activity属性的初始化没有影响。