基于BaseAdapter 实现ListView
成都创新互联专注于沙湾企业网站建设,响应式网站建设,商城网站建设。沙湾网站建设公司,为沙湾等地区提供建站服务。全流程定制网站开发,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
注意扩展BaseAdapter 需要重写以下四个方法
getCount();
getItem();
getItemId();
getView();
程序清单
package xiaocool.net.listviewtest; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; 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 ActionBarActivity { private ListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview2); list=(ListView)this.findViewById(R.id.list1); /*//定义一个数组 String arr[]={"校酷网络1","校酷网络2","校酷网络3","校酷网络4","校酷网络5"}; //将数组包装成ArrayAdapter ArrayAdapteradapter=new ArrayAdapter (this,R.layout.array_item,R.id.textView,arr); list.setAdapter(adapter);*/ BaseAdapter adapter=new BaseAdapter() { //返回总的列表项数 @Override public int getCount() { return 20; } @Override public Object getItem(int position) { return null; } //返回列表项的id @Override public long getItemId(int position) { return 0; } //返回每个具体item的样子 @Override public View getView(int position, View convertView, ViewGroup parent) { //创建一个LinearLayout,并向其中添加两个控件 LinearLayout layout=new LinearLayout(MainActivity.this); layout.setOrientation(0); ImageView p_w_picpathView=new ImageView(MainActivity.this); p_w_picpathView.setImageResource(R.drawable.syudio); TextView textView=new TextView(MainActivity.this); textView.setText("第"+(position+1)+"项"); textView.setTextColor(Color.BLUE); textView.setTextSize(20); layout.addView(p_w_picpathView); layout.addView(textView); return layout; } }; list.setAdapter(adapter); } }