Android的UI组件之ListView(三)

还是接着上篇。

三.通过继承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)

大家注意一下这个细节。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值