Reputation: 1561
I have two list view. I want to add list item from One List view to another. when Item add to another list , item should remove from first list. this is my EventDetailActivity code.
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]);
}
}
}
when I click to listItem to add items it will give error that Attempt to read from null array how can I do that?
thank you.
this is my error:
FATAL EXCEPTION: main Process: com.sabhagruh.ysgoperations, PID: 28001 java.lang.NullPointerException: Attempt to read from null array at com.sabhagruh.ysgoperations.EventDetailActivity$OnCheckBoxClick.onItemClick(EventDetailActivity.java:45) at android.widget.AdapterView.performItemClick(AdapterView.java:305) at android.widget.AbsListView.performItemClick(AbsListView.java:1146) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053) at android.widget.AbsListView$3.run(AbsListView.java:3860) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
also I want to remove Event List which are checked I have done this code.. but not working.
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();*/
}
}
Upvotes: 1
Views: 2054
Reputation: 488
In onCreate
change this line
String[] eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"};
to
eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"};
because you are creating another local variable eventItemList
in oncreate instead of using member variable
Also add
myListViewAddEventDetailList.setAdapter(arrayAdapter)
after last line in onCreate
and
arrayAdapter.notifyDataSetChanged()
after addEventList.add(eventItemList[position]);
in onItemClick
method
Upvotes: 2
Reputation: 4218
You are shadowing the eventItemList
attribute
Replace
String[] eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"}
By :
eventItemList = {"EventItemList1","EventItemList2","EventItemList3","EventItemList4","EventItemList5"}
Your actual code only declares a local variable within the onCreate method and has no effect on the initialization of the activity attribute.
Upvotes: 1