BaseAdapter深入浅出

getCount():该方法的返回值控制该Adapter将会包含多少个列表项。
getItem(int position) :该方法的返回值决定第position处的列表项的内容。
getItemId(int position):该方法的返回值决定第position处的列表项的ID。
getView(int position, View convertView, ViewGroup parent) :该方法的返回值决定第position处的列表项组件。
package com.jia.baseadapter;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private ListView myList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myList = (ListView) findViewById(R.id.myList);
		BaseAdapter adapter = new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// 創建一个LinearLayout,并向其中添加两个组件
				LinearLayout line = new LinearLayout(MainActivity.this);
				line.setOrientation(0);
				ImageView imageView = new ImageView(MainActivity.this);
				imageView.setImageResource(R.drawable.ic_launcher);
				TextView textView = new TextView(MainActivity.this);
				textView.setText("第" + (position + 1) + "个列表项");
				textView.setTextSize(30);
				textView.setTextColor(Color.GREEN);
				line.addView(imageView);
				line.addView(textView);
				// 返回LinearLayout实例
				return line;
			}

			@Override
			public long getItemId(int position) {
				// 重写该方法,该方法的返回值将作为列表项的ID
				return position;
			}

			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public int getCount() {
				// 指定一共包含8000个选项
				return 8000;
			}
		};
		myList.setAdapter(adapter);
	}

}


你可能感兴趣的:(idtransmarks,idtransmarkspa,idtransmarks,idtransmark,idtransmar,lisspan,imagspan,launcspan,ospan,objspan)