简体   繁体   中英

Android Toolbar item OnClickListener

I have a Toolbar and an item (add) which, when clicked, adds a view in listView below. However, the onOptionsItemSelected gives you the effect of a single click so it only adds one view, and in my case, I need multiple views, thus multiple clicks are required. How do I set up everything so that the item behaves as an onClickListener rather than a single click?

@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");
        ((ArrayAdapter) addAdapter).notifyDataSetChanged();
    }

    if (id == R.id.addPeople) {
        return true;
    }


    return super.onOptionsItemSelected(item);
}

Android is always listening for menu item clicks. And on click your action will happen, so you'll need to click multiple times anyways if you want this add feature in the menu.

I usually setup my list adapter in onCreate or onCreateView. Once it's established you can do addAdapter.clear() and addAdapter.add(item). You shouldn't need to reference your listitems directly since the ArrayAdapter.add() method is setup to append to that list anyways and then if i'm not mistaken you can get rid of notifyDataSetChange() - I've never had to use this method with any of the default list adapters or the custom adapters I've written. .clear(), .add(), .insert(), and .remove() should be sufficient.

My listview is usually filled out using a for loop. If you want multiple views added then could you just setup a loop instead of waiting/requiring for more clicks?

Maybe I'm not fully understanding the usecase but a basic for loop seems like the answer here.

Edit:

//For Each Loop - "For each individualItem in itemHolder"
listadapter.clear();
for(ItemType individualItem : itemHolder){
    listAdapter.add(individualItem.getText());
}

or you can do a traditional for loop

//"For i(index) starting at index 0, run until index < itemHolder.getItemCount() is false"
//for(initialize index variable : condition check : increment after each     iteration)
for(int index =0; index<itemHolder.getItemCount(); index++)
{
  listAdapter.add(itemHolder.getItemAt(index));
}

Something like that. I made up method names obviously it's going to depend on your data structures.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM