Android ListView ArrayAdapter
In the last post we talked about the SimpleAdapter and how it is possible to add items to the ListView using this adapter. Another important and very useful adapter is the ArrayAdapter. This class is a standard Android class and it is very simple to use. We can use it every time we have an array or a list of items we want to show.
private void initList() {
// We populate the planets
planetsList.add(“Mercury”);
planetsList.add(“Venus”);
planetsList.add(“Mars”);
planetsList.add(“Jupiter”);
planetsList.add(“Saturn”);
planetsList.add(“Uranus”);
planetsList.add(“Neptune”);
}[/java]
Android ListView: Manage Items dynamically
Let’s suppose we want to manage the items shown inside the ListView dynamically, adding and removing items. First we focus our attention about the add functionality. To do so we need a button, so that when user presses it a dialog window appears. In the dialog window, we add an input text where the user can insert the planet name. To make the interface more friendly, we want the button is always visible even if when the list scrolls up and down. To do it we first create a LinearLayout like that:
[xml]<linearlayout android:layout_height=”match_parent”android:layout_width=”match_parent”
android:orientation=”vertical”
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”>
<listview android:id=”@+id/listView”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:layout_width=”match_parent”>
<button android:id=”@+id/addBtn”
android:layout_height=”80dp”
android:layout_weight=”0.5″
android:layout_width=”match_parent” android:onclick=”addPlanet”
android:text=”Add planet”>
</button>
</listview>
</linearlayout>
[/xml]
Android ListView With SectionIndexer And Fast Scroll
So in this method, we simply open a dialog where user inserts the new planet name.
[java]// Handle user clickpublic void addPlanet(View view) {
final Dialog d = new Dialog(this);
d.setContentView(R.layout.dialog);
d.setTitle(“Add planet”);
d.setCancelable(true);
final EditText edit = (EditText) d.findViewById(R.id.editTextPlanet);
Button b = (Button) d.findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String planetName = edit.getText().toString();
MainActivity.this.planetsList.add(planetName);
// We notify the data model is changed
MainActivity.this.aAdpt.notifyDataSetChanged();
d.dismiss();
}
});
d.show();
}[/java]
..and once the user clicks on add:
The important thing to notice is the notifyDataSetChanged(). We call this adapter method on to notify that the items are changed and it is necessary to refresh the view.
As we did for the add functionality, we can implement the delete item. In this case, we want that when the user long-press on an item, it will be deleted. To handle this event, as we showed in the last post we overide the onContextItemSelected method in this way:
// This method is called when user selects an Item in the Context menu
public boolean onContextItemSelected(MenuItem item) {
int itemId = item.getItemId();
AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) item.getMenuInfo();
planetsList.remove(aInfo.position)
aAdpt.notifyDataSetChanged();
return true;
}[/java]
In this snippet, first we retrieve the AdapterContextMenynfo needed to get the item position and then we remove it from the list. Again we notify to the adapter that the list is changed.
Very useful tutorials. Just few problems I had. inside of does not seem to work. And in must be "android:onClick" instead of "android:onclick". Please fix it. Or better give sources of working app. Thank you.
Fantastic! Thanks a lot!
This tutorial does not mention about Dialog layout, you should visit the source code to know about this xml file
Nice Article for knowledge update.
Can you provide the example code for listview with headers which updated from MySQL database received as json.Additionally column width set according to content received in listview.
Please sent the code on my mail id.
Great, Thanks for sharing.
Great, thanks for sharing us.