Implementing Listview in Android is hard for new users to understand. After reading a number of articles, I feel that the following ideas can be used to make new users better understand (and keep records so that they do not forget it later ).
Can refer to the blog: http://cinderella7.blog.51cto.com/7607653/1281696 (here using MVC ideas to understand Listview, I think it is good)
Http://blog.csdn.net/jueblog/article/details/11857281 (a complete implementation)
-------------------------------------------------------------------------- Split line -----------------------------------------------------------------
I. Straighten out the entire idea.
Custom Listview contains three important concepts, which are understood in the form of MVC:
ListView is equivalent to V (View) in the MVC framework)
The Adapter is equivalent to the C (Controller) in the MVC framework)
The data source is equivalent to M (model) in the MVC framework)
2. Step by step
1. First customize the layout of each Item in listview
Listitem. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="100dp" android:id="@+id/imageView"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/textView"/></LinearLayout>
2. define the main layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView"/></LinearLayout>
3. Then, define the data source, view, and adapter
private List<HashMap<String,Object>> mData; //M private ListView listView; //V private MyAdapter myAdapter; //C
4. Obtain the data source
Private List <HashMap <String, Object> getData () {ArrayList <HashMap <String, Object> arrayList = new ArrayList <HashMap <String, Object> (); hashMap <String, Object> hashMap = null; for (int I = 0; I <10; I ++) {hashMap = new HashMap <String, Object> (); hashMap. put ("index", Integer. toString (I + 1); hashMap. put ("img", R. drawable. my); arrayList. add (hashMap); // add to data source} return arrayList ;}
5. Customize the adapter and add the data source to the adapter.
// Create a custom adapter private class MyAdapter extends BaseAdapter {private LayoutInflater mlayoutInflater;
Public MyAdapter (Context context) {mlayoutInflater = LayoutInflater. from (context); // Dynamic Layout ing} @ Override public int getCount () {return mData. size () ;}@ Override public Object getItem (int position) {return null ;}@ Override public long getItemId (int position) {return 0 ;} @ Override public View getView (int position, View convertView, ViewGroup parent) {convertView = mlayoutInflater. inflate (R. layout. listitem, null); // instantiate view TextView TV = (TextView) convertView Based on the layout file. findViewById (R. id. textView); TV. setText (mData. get (position ). get ("index "). toString (); ImageView img = (ImageView) convertView. findViewById (R. id. imageView); img. setBackgroundResource (Integer) mData. get (position ). get ("img"); return convertView ;}}
6. Implementation in the onCreate Function
listView = (ListView)findViewById(R.id.listView); //---- mData = getData(); myAdapter = new MyAdapter(this); listView.setAdapter(myAdapter);
Iii. Summary
The entire implementation process is strictly in accordance with the MVC mode. First, you need to customize your own element layout, then obtain the data source, rewrite the adapter, and load the data source to the adapter, use listview to display the adapter content.
The full version of Java code is attached below:
Package com. example. listViewTest; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. listView; import android. widget. textView; import java. util. arrayList; import java. util. hashMap; import java. util. list; public class MyActivity extends Activity {/*** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); listView = (ListView) findViewById (R. id. listView); // ---- mData = getData (); myAdapter = new MyAdapter (this); listView. setAdapter (myAdapter);} private List <HashMap <String, Object> mData; // M private ListView listView; // V private MyAdapter myAdapter; // C private List <HashMap <String, Object> getData () {ArrayList <HashMap <String, Object> arrayList = new ArrayList <HashMap <String, object >>> (); HashMap <String, Object> hashMap = null; for (int I = 0; I <10; I ++) {hashMap = new HashMap <String, object> (); hashMap. put ("index", Integer. toString (I + 1); hashMap. put ("img", R. drawable. my); arrayList. add (hashMap); // add to data source} return arrayList;} // create a custom adapter private class MyAdapter extends BaseAdapter {private LayoutInflater mlayoutInflater; public MyAdapter (Context context) {mlayoutInflater = LayoutInflater. from (context); // Dynamic Layout ing} @ Override public int getCount () {return mData. size () ;}@ Override public Object getItem (int position) {return null ;}@ Override public long getItemId (int position) {return 0 ;} @ Override public View getView (int position, View convertView, ViewGroup parent) {convertView = mlayoutInflater. inflate (R. layout. listitem, null); // instantiate view TextView TV = (TextView) convertView Based on the layout file. findViewById (R. id. textView); TV. setText (mData. get (position ). get ("index "). toString (); ImageView img = (ImageView) convertView. findViewById (R. id. imageView); img. setBackgroundResource (Integer) mData. get (position ). get ("img"); return convertView ;}}}