android summary and finishing----(2) Custom ListView (SimpleAdapter implementation)

       Adapter is an important adapter interface linking back-end data and front-end UI, acting as a link between UI and data. The relationship between Data, View and Adapter is as follows:


Common Adapter subclasses are:
BaseAdapter: It is an abstract class, it needs to implement more methods to inherit it, and it has high flexibility.
ArrayAdapter: The simplest subclass that can only display one line of text.
SimpleAdapter: Various effects can be customized.
SimpleCursorAdapter: Used with a simple plain text ListView, it needs the fields of the Cursor to correspond to the id of the UI.

1. Activity

MainActivity.java source code:


public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		//Step1
		String[] contentArray = new String[]{"Content1","Content2","Content3","Content4","Content5"};
		ListView lv = (ListView)findViewById(R.id.lv);
		
		//Step2
		ArrayList<HashMap<String,Object>> lists = new ArrayList<HashMap<String,Object>>();
		for(int i=0; i<contentArray.length; i++){
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("startTimeText", "22:00");
            map.put("endTimeText", "23:00");
            map.put("separator", R.drawable.separator);
            map.put("content", contentArray[i]);
            lists.add(map);
		}
		
		//Step3
		SimpleAdapter adapter = new SimpleAdapter(
				this,
				lists,
				R.layout.list_item,
				new String[]{"startTimeText","endTimeText","separator","content"},
				new int[] {R.id.startTimeText,R.id.endTimeText,R.id.separator,R.id.content});
		
		//Step4
		lv.setAdapter(adapter);
	}
}

Second, the xml layout file


(1) The layout source code activity_main.xml of the MainActivity main interface:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
	    android:id="@+id/lv"
 		android:layout_width="match_parent"     
		android:layout_height="wrap_content"/>

</LinearLayout>
(2) The layout source code list_item.xml of the ListView list unit:

<?xml version="1.0" encoding="utf-8" ?>
 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_height="match_parent<span style="font-family: Arial, Helvetica, sans-serif;">" </span>
	android:gravity="center_vertical"
	android:layout_width="match_parent"
	android:orientation="horizontal">
	
      <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
		android:layout_weight="2"
		android:gravity="center_vertical"
		android:orientation="vertical">
          <TextView
              android:id="@+id/startTimeText"
              android:layout_height="wrap_content"
			  android:layout_width="match_parent"
			  />
          <TextView
              android:id="@+id/endTimeText"
              android:layout_height="wrap_content"
			  android:layout_width="match_parent"
			  />
      </LinearLayout>
      
	<ImageView
	    android:id="@+id/separator"
	    android:layout_weight="1"
 	    android:layout_width="0dp"
		android:layout_height="wrap_content"
		/>

	 <TextView
	    android:id="@+id/content"
 	    android:layout_width="0dp"
	    android:layout_height="wrap_content"
		android:layout_weight="12"/>
</LinearLayout>

3. Related resources

Files under res/drawable-mdpi/:


 separator.png


Fourth, the effect screenshot




5. Reference

(1)http://blog.csdn.net/fznpcy/article/details/8658155/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325916017&siteId=291194637