还是接着上篇。
三.通过继承BaseAdapter来ListView的每个Item中显示一个组合控件
其实第三种方法和上篇中的应该归到一类,只不过方法不同,姑且算上一种吧。这篇文章参考的是http://blog.csdn.net/hellogv/的一篇文章。
UI界面XMl代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/myList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
list.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_below="@id/textView1"
/>
</RelativeLayout>
Activity代码如下:
package com.example.testlistview;
import android.os.Bundle;
public class TestListView extends Activity {
ListView listView;
String [] titles={"标题1","标题2","标题3","标题4"};
String [] texts={"文本内容1","文本内容2","文本内容3","文本内容4"};
int [] Ids={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_list_view);
listView=(ListView)findViewById(R.id.myList);
listView.setAdapter(new ListViewAdapter(titles, texts, Ids));
}
public class ListViewAdapter extends BaseAdapter{
View [] itemViews;
public ListViewAdapter(String[] stitles,String[] stexts,int[] sIds ){
itemViews=new View[stitles.length];
for(int i=0;i<stitles.length;i++){
itemViews[i] = makeItemView(stitles[i], stexts[i],
sIds[i]);
}
}
private View makeItemView(String strtitle,String strtext,int resid ){
LayoutInflater inflater=(LayoutInflater)TestListView.this.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 使用View的对象itemView与R.layout.item关联
View itemView=inflater.inflate(R.layout.list, null);
// 通过findViewById()方法实例R.layout.item内各组件
TextView title=(TextView)itemView.findViewById(R.id.textView1);
title.setText(strtitle);
TextView text=(TextView)itemView.findViewById(R.id.textView2);
text.setText(strtext);
ImageView image=(ImageView)itemView.findViewById(R.id.image);
image.setImageResource(resid);
return itemView;
}
public int getCount() {
// TODO Auto-generated method stub
return itemViews.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return itemViews[arg0];
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if (arg1 == null)
return itemViews[arg0];
return arg1;
}
}
}
运行结果如下:
详细的解释可参考文章:http://blog.csdn.net/hellogv/article/details/4548659
这里要说明的一点自己在编写好后,调试时老是出错,经过将近1个小时的排查才发现是Activity代码中36、39、42中是写成了TextView title=(TextView)findViewById(R.id.textView1);而应该写成
TextView title=(TextView)itemView.findViewById(R.id.textView1)
大家注意一下这个细节。