- Back to Home »
- Android »
- How to dynamically add custom ListView
Tuesday, June 26, 2012
We first Menu allows users to dynamically add or remove,
First Menu inside to join the two options, add item and the remove item
You will see a screen
Should just let it do a simple increase in column action like in the side of the addItem (int position)
It will perform getCount () to obtain the number of columns and re-run time getView ()
If there is no expected results, it does not matter
@Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, Menu.FIRST, 0, "add item"); menu.add(0, Menu.FIRST+1, 0, "remove item"); return super.onCreateOptionsMenu(menu); }
You will see a screen
Then we want to press the add item when the ListView out a
Join the public boolean onOptionsItemSelected (MenuItem item)
When you press the menu option to handle events
@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch(item.getItemId()){ case Menu.FIRST: myAdapter.addItem(myAdapter.getCount()+1); this.setSelection(myAdapter.getCount()+1); //上面這行是在增加新列之後,讓ListView順便移到新列break; case Menu.FIRST+1: break; } return super.onOptionsItemSelected(item); }
At this time, switch to page of MyAdapter.java, two additional methods, namely,
Side should ListView to save the location,
public void addItem(int position) public void removeItem(int position)
When pressing the add item, it calls the addItem (),
Press the remove item and they will call removeItem ()
private ArrayList<Integer> arrayList;
In the constructor to initialize it
GetCount () again is the return to into arrayList the size of the arrayList = new ArrayList<Integer>();
@Override public int getCount() { // TODO Auto-generated method stub return arrayList.size(); }
Should just let it do a simple increase in column action like in the side of the addItem (int position)
public void addItem(int position){ arrayList.add(position); this.notifyDataSetChanged(); }
It will perform getCount () to obtain the number of columns and re-run time getView ()
@Override public View getView(int position, View view, ViewGroup parent) { // TODO Auto-generated method stub final TagView tag; if (view == null) { view = adapterLayoutInflater.inflate(R.layout.adapter, null); tag = new TagView( (Button)view.findViewById(R.id.AdapterButton), (ImageView)view.findViewById(R.id.AdapterImage), (TextView)view.findViewById(R.id.AdapterText)); view.setTag(tag); } else { tag = (TagView) view.getTag(); } tag.image.setBackgroundResource(R.drawable.icon); tag.button.setText("button"+arrayList.get(position)); tag.text.setText("text"+arrayList.get(position)); return arrayList.get(position); }
Now you can run to take a look at the program, the beginning of what did not, when you press the Menu-> add item
Will run out of one of our custom ListView
Then we write the remove item part
Back to onOptionsItemSelected () This function is case Menu.FIRST +1: behind to join the call removeitem
case Menu.FIRST+1: myAdapter.removeItem(myAdapter.getCount()-1); break;
Then MyAdapter.java inside the modified removeItem ()
public void removeItem(int position){ if(!arrayList.isEmpty()){ arrayList.remove(position); this.notifyDataSetChanged(); } }
Up to this point, we can operate the Add / delete functions
If successful, will appear the following screen
Download this file, you can see all of the code
If there are errors, please sure to tell me to avoid misleading everyone thank you
Finally thanks to the wording of the ptt badhabit users to provide better