android之list基本用法

1.//在ListView上输入字母,就会自动筛选出以此内容开头的Item
getListView().setTextFilterEnabled(true);

列表的显示需要三个元素:
1.ListVeiw 用来展示列表的View。
2.适配器 用来把数据映射到ListView上的中介。
3.数据 具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
===============================================
ArrayAdapter用法
===============================================
xml添加ListView控件
new一个ListView
创建一个ArrayAdapter
ListView.setAdapter
--------------------------------------
private ListView listView;
private List data = new ArrayList();
listView = new ListView(this);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, getData());
listView.setAdapter(adapter);
setContentView(listView);
private List getData() {
data.add("test1");
data.add("test2");
data.add("test3");
return data;
}
-------------------------------------

==============================================
SimpleCursorAdapter用法
==============================================
listView = new ListView(this);

Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor); //将获得的Cursor对象交由Activity管理,这样Cursor的生命周期
//和Activity便能够自动同步,省去自己手动管理Cursor

ListAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_1,
cursor,
new String[]{People.NAME},
new int[]{android.R.id.text1});

listView.setAdapter(listAdapter);
setContentView(listView);


=============================================
SimpleAdapter用法
=============================================
private ListView listView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = new ListView(this);
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.vlist, new String[]{"image","text"}, new int[]{R.id.image,R.id.text});
listView.setAdapter(adapter);
setContentView(listView);
}

private List> getData() {
List> list = new ArrayList>();
Map map = new HashMap();
map.put("image", R.drawable.icon);
map.put("text", "item1");
list.add(map);

map = new HashMap();
map.put("image", R.drawable.icon);
map.put("text", "item2");
list.add(map);

return list;
}


============================================
重写BaseAdapter,自定义item,其他和SimpleAdapter,就是adapter是自定义的
============================================
public final class ViewHolder{
public ImageView img;
public TextView title;
public TextView info;
public Button viewBtn;
}

public class MyAdapter extends BaseAdapter{

private LayoutInflater mInflater;

public MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;
if (convertView == null) {

holder=new ViewHolder();

convertView = mInflater.inflate(R.layout.vlist2, null);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.info = (TextView)convertView.findViewById(R.id.info);
holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
convertView.setTag(holder);

}else {
holder = (ViewHolder)convertView.getTag();
}


holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
holder.title.setText((String)mData.get(position).get("title"));
holder.info.setText((String)mData.get(position).get("info"));
holder.viewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInfo();
}
});


return convertView;
}

}



你可能感兴趣的:(android(UI))