Layout1.6

ListView BaseAdapter:

package com.example.android_listview_activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @desc	自定义适配器
 * @author	ljt
 * @time	2014年8月27日 上午11:15:27
 */
public class MainActivity5 extends Activity{
	
	private static final String TAG = "MainActivity";
	
	private ListView listView;
	
	private int[] images = {R.drawable.abc_ic_go_search_api_holo_light,R.drawable.abc_ic_go_search_api_holo_light};
	
	private String[] names = {"北京","上海"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		listView = (ListView)this.findViewById(R.id.listView1);
		MyAdapter adapter = new MyAdapter();
		listView.setAdapter(adapter);
	}
	
	/**
	 * @desc	自定义适配器
	 * @author	ljt
	 * @time	2014年8月27日 上午11:09:13
	 */
	class MyAdapter extends BaseAdapter{

		/**
		 * How many items are in the data set represented by this Adapter.
		 */
		@Override
		public int getCount() {
			return names.length;
		}

		/**
		 * Get the data item associated with the specified position in the data set.
		 */
		@Override
		public Object getItem(int position) {
			return names[position];
		}
		
		/**
		 * Get the row id associated with the specified position in the list.
		 */
		@Override
		public long getItemId(int position) {
			return position;
		}

		/**
		 * 创建列表项
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.i(TAG, "position == "+position);
			
			// 将layout的xml布局文件实例化为View类对象
			View view = getLayoutInflater().inflate(R.layout.source2,null);
			ImageView imageView = (ImageView)view.findViewById(R.id.imageView3);
			imageView.setImageResource(images[position]);
			
			TextView textView = (TextView)view.findViewById(R.id.textView3);
			textView.setText(names[position]);
			
			return view;
		}
		
	}
	
}

猜你喜欢

转载自luan.iteye.com/blog/2109472
1.6