最近要实现动态搜索显示历史记录,需要用到ListView加载历史记录。这东西感觉历经几个星期了,一直出现各种问题。其实利用ListView进行加载Item已经比较成熟了,各种方式都可以,只是自己之前没有深入探索,照书搬,没有深入理解才出现了各种奇葩问题。现将一些方式总结如下:
一、使用默认的布局以及适配器实现ListView
首先在布局文件中定义ListView:
然后,在MainActivity中实例化控件,并设置相关监听事件:
//定义ListView的Item数据
String[] data={};//自定义,也可以是ArrayList类型的
//声明并实例化
ListView lv_item= (ListView) findViewById(R.id.lv_item);
//定义ArrayAdapter适配器
ArrayAdapter adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_2,data);
//设置适配器
lv_item.setAdapter(adapter);
//设置监听事件
lv_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
//触发ListView的Item的事件
}
});
*这是最简单的一种,其中android.R.layout.simple_list_item_2即为一种最简单的默认布局
二、使用自定义的布局
将第一种方式的android.R.layout.simple_list_item_2换成自定义的布局文件,但是自定义布局文件时可能就不单单是只显示文件,有可能需要在文字里配上图标。这样就必须要定制适配器了。在定制适配器的时候又有两种不同的方式,一种是继承BaseAdapter,一种是集成ArrayAdapter;
1、使用带图标的自定义布局并继承ArrayAdapter
1)自定义布局文件:
2)Java代码实现:
public class MainActivity extends ActionBarActivity {
ArrayList- arrayList = new ArrayList
();
ListView lv_item;
ImageView imageView;
TextView textView;
ItemAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
lv_item = (ListView) findViewById(R.id.lv_item);
adapter = new ItemAdapter(MainActivity.this,
R.layout.listview_item, arrayList);
lv_item.setAdapter(adapter);
lv_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
String sendValue = lv_item.getItemAtPosition(position).toString();
Intent intent = new Intent();
intent.putExtra("value", sendValue);
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
//初始化数据
private void initData() {
Item item;
int[] resourceID = {R.drawable.ic1, R.drawable.ic2, R.drawable.ic3, R.drawable.ic4, R.drawable.ic5
, R.drawable.ic6, R.drawable.ic7, R.drawable.ic8, R.drawable.ic9, R.drawable.ic10
, R.drawable.ic11, R.drawable.ic12, R.drawable.ic13, R.drawable.ic14, R.drawable.ic15
, R.drawable.ic16, R.drawable.ic17, R.drawable.ic18, R.drawable.ic19, R.drawable.ic20};
for (int i = 0; i < 20; i++) {
item = new Item(resourceID[i], "Item " + i);
arrayList.add(item);
}
}
// 定义一个Item
public class Item {
private int imageView_id;
private String textView_text;
public Item(int imageView_id, String textView_text) {
this.imageView_id = imageView_id;
imageView_id = R.drawable.ic1;
this.textView_text = textView_text;
}
public int getImageView_id() {
return imageView_id;
}
public String getTextView_text() {
return textView_text;
}
public void setTextView_text(String textView_text) {
this.textView_text = textView_text;
}
@Override
public String toString() {
return "资源ID:" + imageView_id + '\'' + " Item:" + textView_text;
}
}
// 自定义适配器
public class ItemAdapter extends ArrayAdapter- {
int resourceId;
public ItemAdapter(Context context, int resource, List
- objects) {
super(context, resource, objects);
this.resourceId = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Item item = getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId,
null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) view.findViewById(R.id.iv);
viewHolder.textView = (TextView) view.findViewById(R.id.tv);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.imageView.setImageResource(item.getImageView_id());
viewHolder.textView.setText(item.getTextView_text());
return view;
}
class ViewHolder {
ImageView imageView;
TextView textView;
}
}
}
由于其中涉及到一些资源文件,可能加载很慢,或者运行不出来,建议使用小一点的图标。
2、使用带图标的自定义布局并继承BaseAdapter
1)自定义布局文件:
2)Java代码实现:
public class ItemAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private List- mList;
public ItemAdapter(Context context, List
- mList) {
this.mInflater = LayoutInflater.from(context);
this.mList = mList;
}
@Override
public int getCount() {
if(mList.size()>0){
return mList.size();
}
return 0;
}
@Override
public Object getItem(int i) {
if(mList.size()>0){
return mList.get(i);
}
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Item item = (Item) getItem(position);
View view;
ViewHolder viewHolder;
if (convertView == null) {
view=convertView;
view = mInflater.inflate(R.layout.listview_item, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) view.findViewById(R.id.iv);
viewHolder.textView = (TextView) view.findViewById(R.id.tv);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.imageView.setImageResource(item.getImageView_id());
viewHolder.textView.setText(item.getTextView_text());
return view;
}
class ViewHolder {
ImageView imageView;
TextView textView;
}
}
运行效果图:
在ListView中还可以放一些其他的控件,只要封装并注意监听控制就可以实现。有兴趣可以尝试尝试。