你的位置:首页 > 操作系统

[操作系统]listview中 Baseadapter的简单使用


代码也是来自于网上,在自己理解之后稍微做下笔记,主要是在Baseadapter的部分增添了一些注释。

main.

 

<?<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

 

<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
/>

 

</LinearLayout>

 

list_item.

 

<?<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

 

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
/>
</LinearLayout>

 

</LinearLayout>

 

 

Demo17Activity.java

package com.example.baseadapter1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Demo17Activity extends Activity {
private ListView lv;
private List<Map<String,Object>> data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

lv=(ListView)findViewById(R.id.lv);
data=getdata();
MyAdapter adapter=new MyAdapter(this);
lv.setAdapter(adapter);

}

private List<Map<String,Object>> getdata()
{
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
for(int i=0;i<10;i++)
{
Map<String,Object> map=new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("title", "hehehehehe");
map.put("info", "66666666");
list.add(map);
}
return list;
}

static class ViewHolder
{
public ImageView img;
public TextView title;
public TextView info;
}

public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater=null;

public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.mInflater=LayoutInflater.from(context);  //这里就是确定你listview在哪一个layout里面展示
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size(); //这个决定你listview有多少个item
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder=null;
if(convertView==null)
{
holder=new ViewHolder();
convertView=mInflater.inflate(R.layout.list_item,null); //这里确定你listview里面每一个item的layout
holder.img = (ImageView)convertView.findViewById(R.id.img); //此处是将内容与控件绑定。

holder.title = (TextView)convertView.findViewById(R.id.tv);//注意:此处的findVIewById前要加convertView.
holder.info = (TextView)convertView.findViewById(R.id.info);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag(); //这里是为了提高listview的运行效率
}
holder.img.setBackgroundResource((Integer)data.get(position).get("img"));
holder.title.setText((String)data.get(position).get("title"));
holder.info.setText((String)data.get(position).get("info"));

return convertView;
}
}

}