AndroidUI system: ListView
1. Create a project: ListViewLearn
2. Modify MainActivity, inherited from ListActivity
3. Create a String array to save the actual content in ListView.
- Package com. learn. listviewlearn. utility;
-
- Public class Util {
- Public static final String [] COUNTRYS = {"China", "USA", "Russia", "UK", "France "};
- }
4. Modify the onCreate method and set an Adapter. The content in the array is displayed in ListView.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.activity_main);
- this.setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_dropdown_item_1line, Util.COUNTRYS));
- }
Ii. Use the XML layout file to define the ListView Style
1. Modify the activity_main.xml file and add a ListView. The id must be android: id = "@ android: id/list"
- LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <ListView
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFFFFF00" >
- </ListView>
-
- </LinearLayout>
2. Modify the OnCreate Method
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_dropdown_item_1line, Util.COUNTRYS));
- }
3. Use an XML layout file to define the ListViewItem Style
1. Create a layout file list_view.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
-
- <ImageView
- android:id="@+id/imageViewIcon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:contentDescription="@string/icon" >
- </ImageView>
-
- <TextView
- android:id="@+id/textViewContent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- </TextView>
-
- </LinearLayout>
2. Create an Adapter that inherits from BaseAdapter and mainly modifies the getCount () and getView () methods.
- package com.learn.listviewlearn.adapter;
-
- import com.learn.listviewlearn.R;
- import com.learn.listviewlearn.utility.Util;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
-
- public class ListViewAdapter extends BaseAdapter {
- private Context context;
-
- public ListViewAdapter() {
- // TODO Auto-generated constructor stub
- }
-
- @Override
- public int getCount() {
- return Util.COUNTRYS.length;
- }
-
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null) {
- convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
- ItemViewCache itemViewCache = new ItemViewCache();
- itemViewCache.imageView = (ImageView)convertView.findViewById(R.id.imageViewIcon);
- itemViewCache.textView = (TextView)convertView.findViewById(R.id.textViewContent);
- convertView.setTag(itemViewCache);
- }
-
- ItemViewCache cache = (ItemViewCache) convertView.getTag();
-
- cache.imageView.setImageResource(Util.images[position]);
- cache.textView.setText(Util.COUNTRYS[position]);
- return convertView;
- }
-
- private static class ItemViewCache{
- public TextView textView;
- public ImageView imageView;
- }
-
- }
Util. java
- Package com. learn. listviewlearn. utility;
-
- Import com. learn. listviewlearn. R;
-
- Public class Util {
- Public static final String [] COUNTRYS = {"China", "USA", "Russia", "UK", "France "};
- Public static final int [] images = {R. drawable. ic_launcher,
- R. drawable. ic_launcher, R. drawable. ic_launcher,
- R. drawable. ic_launcher, R. drawable. ic_launcher };
- }
4. Add a Click event for the ListView. You only need to implement the onListItemClick () method in MainActivity.
- @ Override
- Protected void onListItemClick (ListView l, View v, int position, long id ){
- Toast. makeText (this, "you selected" + Util. COUNTRYS [position], Toast. LENGTH_SHORT)
- . Show ();
- }
Link: http://www.cnblogs.com/zhangtingkuo/archive/2014/08/01/3884689.html