When I was a beginner in android, I felt that the ListView component was widely used and difficult. The following are my own problems:
1. array-based ListView
You must use ArrayAdpter to provide table items.
[Java]
...
Array [I] = cursor. getString (1 );
...
ArrayAdapter <String> arrayAdapter = new ArrayAdapter <String> (this, android. R. layout. simple_list_item_2, array );
...
Array [I] = cursor. getString (1 );
...
ArrayAdapter <String> arrayAdapter = new ArrayAdapter <String> (this, android. R. layout. simple_list_item_2, array );
The first parameter is context, the second is the appearance style, and the third is the array.
List. setAdapter (arrayAdapter). This is a simple listview.
2. Use SimpleAdapter to customize list items
This is a bit complicated, mainly because simpleadapter has many parameters,
SimpleAdapter (Context context, List <? Extends Map <String,?> Data, int resource, String [] from, int [])
The first parameter is context, the second is the set of list items, the third is the ID of the interface appearance style, and the fourth is related to the second, which is equivalent to the Key in the second Map, the fifth option is related to the third option, indicating which views constitute the list items.
If you want to make the modification more complex, you can customize your favorite layout to put it at the third parameter and modify other parameters accordingly.
If you want to make it more complex, rewrite the BaseAdapter.
3. Add a click event
Public void onItemClick (AdapterView <?> Arg0, View view, int position, long arg3)
This is a description of Baidu, which is very specific:
X and Y: listview. X contains four items: 1, 2, 3, and 4. Y contains four items: a, B, c, and d.
If you click item B. As follows:
Public void onItemClick (AdapterView <?> Parent, // parent is equivalent to a pointer of the listview Y adapter. You can use it to get everything in Y. Then, let us tell you That you click Y, not X --,
View view, // view is the view handle of item B. You can use this view to obtain the id of the control in item B and then operate the control.
Int position, // position is the position of B in the Y adapter (when the listview is generated, the adapter makes items one by one, and then queues them in order and puts them in the listview, that is, B is the position)
Long id // id is the position of B in the first row of listview Y (obviously 2nd rows). In most cases, the value of position and id is the same. If necessary, you can add a log to get both the position and id in logcat. After reading the log, you will be steadfast.
)
So how can we get the value in item?
[Java]
ListView = (ListView) this. findViewById (R. id. listview );
List <HashMap <String, String> data = new ArrayList <HashMap <String, String> ();
UserService us = new UserService (this );
List <User> users = us. getScrollDate (0, 10 );
For (User u: users ){
HashMap <String, String> map = new HashMap <String, String> ();
Map. put ("userid", String. valueOf (u. getUserid ()));
Map. put ("name", u. getName ());
Map. put ("age", String. valueOf (u. getAge ()));
Data. add (map );
}
SimpleAdapter adapter = new SimpleAdapter (this, data, R. layout. useritem, new String [] {"userid", "name", "age"}, new int [] {R. id. userid, R. id. name, R. id. age });
ListView. setAdapter (adapter );
ListView. setOnItemClickListener (new OnItemClickListener (){
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id ){
ListView listView = (ListView) parent;
HashMap <String, String> map = (HashMap <String, String>) listView. getItemAtPosition (position );
String userid = map. get ("userid ");
String name = map. get ("name ");
String age = map. get ("age ");
Toast. makeText (SQLiteCRUDActivity. this, userid + "," + name + "," + age, Toast. LENGTH_LONG). show ();
}
});
ListView = (ListView) this. findViewById (R. id. listview );
List <HashMap <String, String> data = new ArrayList <HashMap <String, String> ();
UserService us = new UserService (this );
List <User> users = us. getScrollDate (0, 10 );
For (User u: users ){
HashMap <String, String> map = new HashMap <String, String> ();
Map. put ("userid", String. valueOf (u. getUserid ()));
Map. put ("name", u. getName ());
Map. put ("age", String. valueOf (u. getAge ()));
Data. add (map );
}
SimpleAdapter adapter = new SimpleAdapter (this, data, R. layout. useritem, new String [] {"userid", "name", "age"}, new int [] {R. id. userid, R. id. name, R. id. age });
ListView. setAdapter (adapter );
ListView. setOnItemClickListener (new OnItemClickListener (){
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id ){
ListView listView = (ListView) parent;
HashMap <String, String> map = (HashMap <String, String>) listView. getItemAtPosition (position );
String userid = map. get ("userid ");
String name = map. get ("name ");
String age = map. get ("age ");
Toast. makeText (SQLiteCRUDActivity. this, userid + "," + name + "," + age, Toast. LENGTH_LONG). show ();
}
});