The use of Android ListView and the use of Adapter

1. The use steps of listView and the usage of ArrayAdapter

(1), define an array to store the content of the item in the ListView.

(E.g:

private static final String[] strs = { "first", "second", "third", "fourth", "fifth" };

 

(2) Create an ArrayAdapter object by implementing the ArrayAdapter constructor.

(E.g:

ArrayAdapter<String>  adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strs);

, where the second parameter can customize a layout, but this layout must have a TextView control. );      

 

(3) Bind the ArrayAdapter through the ListViewsetAdapter() method.

(E.g:

listView.setAdapter(adapter)) 

in addition:

(1) Implement a ListView with a selection box by specifying the resource android.R.layout.simple_list_item_checked.

        You need to use the setChoiceMode() method to set the selection as multi-selection or single-selection, otherwise the selection effect will not be achieved.

(E.g:

listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, strs));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

 

(2) Implement ListView with CheckBox by specifying the resource android.R.layout.simple_list_item_multiple_choice.

(E.g:

listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,strs));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

 

(3) Implement ListView with RadioButton by specifying the resource android.R.layout.simple_list_item_single_choice.

(E.g:

listView.setAdapter(newArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,strs));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

 

 

 

 

ListView binds a click listener:

 

listView.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2 ,long arg3) {
       }
});

 

2. ListView uses SimpleAdapter

 

(1) Define the layout implemented by each row of ListView as needed.

(2) Define a list composed of HashMap, and store the data in it in the form of key-value pairs.

(3) Construct SimpleAdapter object.

(4) Bind LsitView to SimpleAdapter.

 

3. ListView uses BaseAdapter and ListView optimization

 

 To use BaseAdapter, you must write a class to inherit it, and BaseAdapter is an abstract class, and you must implement its methods to inherit it. The most important of these is the getView() method.

getCount();

getItem(int position);

getItemId(int position) ;

getView(int position, View convertView, ViewGroup parent);

 

When the system starts to draw the ListView, it first calls the getCount() method. Get its return value, which is the length of the ListView. Then the system calls the getView() method to draw each row of the ListView one by one according to this length. getItem() and getItemId() are called when the data in the Adapter needs to be processed and retrieved.

When the convertView is empty, use the setTag() method to bind a ViewHolder object that stores controls for each View.

When the convertView is not empty and the created view is reused, use the getTag() method to obtain the bound ViewHolder object, thus avoiding the layer-by-layer query of the control by findViewById, but quickly locating the control.

 

public class WeatherActivityAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<WeatherBean> list;

    public WeatherActivityAdapter(Context context, ArrayList<WeatherBean> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.weather_item_layout, parent, false);
            vh.tv_date = (TextView) convertView.findViewById(R.id.date_textView);
            convertView.setTag (vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        WeatherBean bean=list.get(position);
        vh.tv_date.setText(bean.date);
        return convertView;
    }
}
class ViewHolder {
    TextView tv_date;
}	

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326992371&siteId=291194637