基于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 ArrayAdapter<String> adapter=new ArrayAdapter<String>(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); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。