Android开发之BaseApdater

概要

SimpleAdapter和BaseAdapter都可以用于实现相同功能,但是相比较而言我比较喜欢BaseAdapter,都是java代码操作,没那么多xml文件要写。

 

概要步骤:

这里没什么步骤,无非是在布局里添加一个ListView之类的,我把BaseApdater的构造函数解释一下就完事了。

       BaseAdapter ba = new BaseAdapter(){

 

           @Override

           public int getCount() {

              return 4;//ListView中有几条数据要显示

           }

 

           @Override

           public Object getItem(int position) {

              return null;

           }

 

           @Override

           public long getItemId(int position) {

              return 0;

           }

 

           @Override

           public View getView(int position, View convertView, ViewGroupparent) {

              //这里定义了每一项显示的内容是什么

           }

       };

完整代码

public class ListViewActivity extends Activity {

   

    private OnItemSelectedListener itemSelectedListener;

    private OnItemClickListener itemClickListener;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.listview);

       initListView();

    }

   

    public void initListView(){

       final int[] images = {R.drawable.bluebutton,R.drawable.greenbutton,R.drawable.imagebutton,R.drawable.imagebuttondown};

       final int[] strings = {R.string.image1,R.string.image2,R.string.image3,R.string.image4};

      

       ListView lv = (ListView) this.findViewById(R.id.listview);

       itemSelectedListener = new OnItemSelectedListener(){

 

           @Override

           public void onItemSelected(AdapterView arg0, View arg1,

                  int arg2, long arg3) {

              LinearLayout ll = (LinearLayout) arg1;

              TextView tv = (TextView) ll.getChildAt(1);

              Toast.makeText(ListViewActivity.this, tv.getText(),1000).show();

           }

 

           @Override

           public void onNothingSelected(AdapterView arg0) {

             

           }

       };

      

       itemClickListener = new OnItemClickListener(){

 

           @Override

           public void onItemClick(AdapterView arg0, View arg1, int arg2,

                  long arg3) {

              LinearLayout ll = (LinearLayout) arg1;

              TextView tv = (TextView) ll.getChildAt(1);

              Toast.makeText(ListViewActivity.this, tv.getText(),1000).show();

           }

       };

          

       BaseAdapter ba = new BaseAdapter(){

 

           @Override

           public int getCount() {

              return 4;

           }

 

           @Override

           public Object getItem(int position) {

              return null;

           }

 

           @Override

           public long getItemId(int position) {

              return 0;

           }

 

           @Override

           public View getView(int position, View convertView, ViewGroupparent) {

              LinearLayout ll = new LinearLayout(ListViewActivity.this);

              ll.setOrientation(LinearLayout.HORIZONTAL);

              ll.setPadding(5, 5, 5, 5);

              ImageView iv = new ImageView(ListViewActivity.this);

              iv.setImageDrawable(getResources().getDrawable(images[position]));

              iv.setScaleType(ImageView.ScaleType.FIT_XY);

              iv.setLayoutParams(new Gallery.LayoutParams(100, 98));

              ll.addView(iv);

             

              TextView tv = new TextView(ListViewActivity.this);

              tv.setText(getResources().getText(strings[position]));

              tv.setTextSize(24);

              tv.setPadding(5, 5, 5, 5);

              tv.setGravity(Gravity.LEFT);

              ll.addView(tv);

              return ll;

           }

       };

       lv.setAdapter(ba);

       lv.setOnItemSelectedListener(itemSelectedListener);

       lv.setOnItemClickListener(itemClickListener);

    }

}

运行效果图

你可能感兴趣的:(自建项目)