So I have this 'add' item in my toolbar. This item is supposed to add views to a listView below the toolbar and it works when you press it the first time. However, once the first view is added you cannot add anymore. How do I fix this?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.addButton) {
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
}
return super.onOptionsItemSelected(item);
}
Each time you are creating a new list and you are adding there only one item. You can try declaring your list as a field in your class. Move this:
final ArrayList<String> listItems=new ArrayList<String>();
out of your onOptionsItemSelected
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.