listView的普通使用

 BaseAdapter的使用步骤:

1.       获取数据源      List<Map<String,Object>>一般是这样的模式

2.       构建适配器

3.       配置适配器

4.       设置监听事件

BaseAdapter的使用:

1.       写一个类继承BaseAdapter

2.       实现该类的方法

3.       在自己写的这个类中再写一个类用来定义listview中的子布局

               最核心的的代码:

                public View getView(intposition, View convertView, ViewGroup parent) {

                            ViewHolderholder=null;

                            if (convertView==null){

                                     holder=newViewHolder();

                                     convertView=getLayoutInflater().inflate(R.layout.lv_item,null);

                                     holder.iv=(ImageView)convertView.findViewById(R.id.iv);

                                     holder.tv=(TextView)convertView.findViewById(R.id.tv);

                                     convertView.setTag(holder);

                            }else {

                                     holder=(ViewHolder)convertView.getTag();

                            }

                            Integer.parseInt(list

                                               .get(position).get("icon").toString());

                            holder.tv.setText(list.get(position).get("name").toString());

                            return convertView;

                   }

                   public class ViewHolder{

                            private ImageViewiv;

                            private TextView tv;

                   }

灵异事件的解决方法:

           android:descendantFocusability="blocksDescendants"

如果想拿到listview的子布局中的一个控件,可以再lv的监听器中通过监听器中的view对象

拿到子布局,然后在由子布局拿到想要的控件

布局代码:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

    android:orientation="vertical">

 

    <ListView

       android:id="@+id/listView_main_info"

       android:layout_width="match_parent"

        android:layout_height="match_parent">

    </ListView>

 

</LinearLayout>

 

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent" >

 

     <ImageView

        android:id="@+id/imageView_item_icon"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_alignParentTop="true"

        android:src="@drawable/ic_launcher"/>

        

    <TextView

       android:id="@+id/textView_item_username"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentTop="true"

       android:layout_marginLeft="18dp"

        android:layout_toRightOf="@+id/imageView_item_icon"

        android:textSize="10pt"

        android:textColor="#00f"

        android:text="TextView" />

 

    <TextView

       android:id="@+id/textView_item_phone"

        android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignBottom="@+id/imageView_item_icon"

       android:layout_alignLeft="@+id/textView_item_username"

        android:text="TextView" />

</RelativeLayout>

 

逻辑代码:

public classMainActivity extends Activity {

         private final static String TAG ="getView";

         private ListView listView_main_info;

         private List<Map<String,Object>> list = null;

 

         private String[] arrName = new String[]{ "李白", "弄玉", "李清照", "虎头", "李白",

                            "弄玉", "李清照", "虎头", "李白", "弄玉", "李清照", "虎头", "李白", "弄玉", "李清照", "虎头" };

         private String[] arrPhone = newString[] { "1", "2", "3", "4","1", "2",

                            "3","4", "1", "2", "3", "4","1", "2", "3", "4" };

        

         private int[] arrIcon = new int[] {R.drawable.libai, R.drawable.nongyu,

                            R.drawable.qingzhao,R.drawable.tiger,R.drawable.libai, R.drawable.nongyu,

                            R.drawable.qingzhao,R.drawable.tiger,R.drawable.libai, R.drawable.nongyu,

                            R.drawable.qingzhao,R.drawable.tiger,R.drawable.libai, R.drawable.nongyu,

                            R.drawable.qingzhao,R.drawable.tiger };

        

         @Override

         protected void onCreate(BundlesavedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

 

                   listView_main_info= (ListView) findViewById(R.id.listView_main_info);

 

                   // 1.数据源

                   list = newArrayList<Map<String, Object>>();

                   for (int i = 0; i <arrName.length; i++) {

                            Map<String,Object> map = new HashMap<String, Object>();

                            map.put("username",arrName[i]);

                            map.put("phone",arrPhone[i]);

                            map.put("icon",arrIcon[i]);

 

                            list.add(map);

                   }

 

                   // 2.构建Adapter,需要建一个内部类继承BaseAdapter

                   // 3. 绑定

                  listView_main_info.setAdapter(newMyAdapter(list));

         }

 

         // 自定义一个Adapter

         public class MyAdapter extendsBaseAdapter {

                   List<Map<String,Object>> list = null;

 

                   publicMyAdapter(List<Map<String, Object>> list) {

                            this.list = list;

                   }

 

                   @Override

                   // 返回整个列表项的数量

                   public int getCount() {

                            return list.size();

                   }

 

                   @Override

                   // 返回具体某个列表位置上的对象

                   public Object getItem(intposition) {

                            return list.get(position);

                   }

 

                   @Override

                   // 返回具体某个列表位置上的Id

                   public long getItemId(intposition) {

                            return position;

                   }

                  

                   @Override

                   // 加载/获取布局文件上的视图控件,每一个Item都会调用getView方法,

                   public View getView(intposition, View convertView, ViewGroup parent) {

                            ViewHolder mHolder =null;

 

                            Log.i(TAG, position+ " " + convertView);

 

                            if (convertView == null) {

                                     mHolder =new ViewHolder();

                                     // 创建新的View

                                     convertView= getLayoutInflater().inflate(

                                                        R.layout.listview_item,null);

                                    

                                     // 获取布局文件上的控件对象

                                     mHolder.textView_item_username =(TextView) convertView

                                                        .findViewById(R.id.textView_item_username);

                                     mHolder.textView_item_phone= (TextView) convertView

                                                        .findViewById(R.id.textView_item_phone);

                                     mHolder.imageView_item_icon= (ImageView) convertView

                                                        .findViewById(R.id.imageView_item_icon);

                                    

                                     //设置标志

                                     convertView.setTag(mHolder);

 

                            } else {

                                     mHolder =(ViewHolder) convertView.getTag();

                            }

 

                            // 为具体的某一个Item设置数据源

                            mHolder.textView_item_username.setText(list.get(position)

                                               .get("username").toString());

 

                            mHolder.textView_item_phone.setText(list.get(position).get("phone")

                                               .toString());

 

                            mHolder.imageView_item_icon.setImageResource(Integer.parseInt(list

                                               .get(position).get("icon").toString()));

 

                            return convertView;

                   }

                  

                   // 用来获取布局文件上的控件对象,并作为这个类中的属性

                   public class ViewHolder {

                            private TextViewtextView_item_username;

                            private TextViewtextView_item_phone;

                            private ImageViewimageView_item_icon;

                   }

         }}

 

例子二:

布局:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

   android:layout_height="fill_parent"

    android:orientation="vertical">

 

    <TextView

       android:id="@+id/textView_main_info"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

        android:background="#000"

        android:text=" "

        android:textColor="#fff"/>

 

    <RelativeLayout

       android:layout_width="match_parent"

       android:layout_height="wrap_content" >

 

        <Button

           android:id="@+id/button_main_selectall"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_alignParentLeft="true"

           android:layout_alignParentTop="true"

           android:onClick="clickButton"

            android:text="全选" />

 

        <Button

           android:id="@+id/button_main_deselectall"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_alignParentTop="true"

            android:layout_toRightOf="@+id/button_main_selectall"

           android:onClick="clickButton"

            android:text="取消全选" />

 

        <Button

           android:id="@+id/button_main_invertselect"

           android:layout_width="wrap_content"

            android:layout_height="wrap_content"

           android:layout_alignParentTop="true"

           android:layout_toRightOf="@+id/button_main_deselectall"

           android:onClick="clickButton"

            android:text="反选" />

 

        <Button

            android:id="@+id/button_main_ok"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_alignParentRight="true"

           android:layout_alignParentTop="true"

            android:onClick="clickButton"

            android:text="完成" />

 

    </RelativeLayout>

        

    <ListView

       android:id="@+id/listView_main"

       android:layout_width="match_parent"

       android:layout_height="wrap_content" >

    </ListView>

 

</LinearLayout>

 

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent" >

 

    <TextView

       android:id="@+id/textView_item_username"

       android:layout_width="wrap_content"

        android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_alignParentTop="true"

        android:textSize="24sp"

        android:textColor="#00f"

        android:text="TextView" />

 

    <TextView

        android:id="@+id/textView_item_pwd"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentLeft="true"

       android:layout_below="@+id/textView_item_username"

        android:text="TextView" />

 

    <CheckBox

        android:id="@+id/checkBox_item"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignBaseline="@+id/textView_item_pwd"

       android:layout_alignBottom="@+id/textView_item_pwd"

        android:layout_alignParentRight="true"

        android:text="" />

 

</RelativeLayout>

 

逻辑代码:

packagecom.jason.listview;

 

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

 

importcom.jason.listview.MainActivity.MyAdapter.ViewHolder;

 

importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.AdapterView;

importandroid.widget.AdapterView.OnItemClickListener;

importandroid.widget.BaseAdapter;

importandroid.widget.CheckBox;

importandroid.widget.ListView;

importandroid.widget.TextView;

 

public classMainActivity extends Activity {

         private TextView textView_main_info;

         private ListView listView_main_info;

         private ArrayList<Map<String,Object>> list = null;

         private MyAdapter myAdapter = null;

        

         @Override

         protected void onCreate(BundlesavedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

 

                   textView_main_info =(TextView) findViewById(R.id.textView_main_info);

                   listView_main_info =(ListView) findViewById(R.id.listView_main);

 

                   // 准备数据源

                   list = newArrayList<Map<String, Object>>();

                   for (int i = 0; i < 15;i++) {

                            Map<String,Object> map = new HashMap<String, Object>();

                            map.put("username","jason_" + i);

                            map.put("pwd","123456_" + i);

                            list.add(map);

                   }

 

                   // 定义一个自定义适配器

                   myAdapter = newMyAdapter(list);

                   // 为listView_main_info绑定适配器

                   listView_main_info.setAdapter(myAdapter);

 

                   listView_main_info.setOnItemClickListener(newOnItemClickListener() {

 

                            @Override

                            public voidonItemClick(AdapterView<?> parient, View view,

                                               intposition, long id) {

                                     ViewHoldermHolder = (ViewHolder) view.getTag();

                                     //在原来的基础上进行反转

                                     mHolder.checkBox_item.toggle();

                                     //判断是真还是假

                                     myAdapter.checkedStatusMap.put(position,

                                                        mHolder.checkBox_item.isChecked());

                            }

                   });

         }

 

         public void clickButton(View view) {

                   switch (view.getId()) {

                   caseR.id.button_main_selectall:

                            for (int i = 0; i< myAdapter.getCount(); i++) {

                                     myAdapter.checkedStatusMap.put(i,true);

                            }

                            myAdapter.notifyDataSetChanged();

                            textView_main_info.setText("");

                            break;

                           

                   caseR.id.button_main_deselectall:

                            for (int i = 0; i< myAdapter.getCount(); i++) {

                                     myAdapter.checkedStatusMap.put(i,false);

                            }

                            myAdapter.notifyDataSetChanged();

                            textView_main_info.setText("");

                            break;

 

                   caseR.id.button_main_invertselect:

                            for (int i = 0; i< myAdapter.getCount(); i++) {

                                     myAdapter.checkedStatusMap.put(i,

                                                        !myAdapter.checkedStatusMap.get(i));

                            }

                            myAdapter.notifyDataSetChanged();

                            textView_main_info.setText("");

                            break;

 

                   case R.id.button_main_ok:

                            StringBuffer sb =new StringBuffer();

                            sb.append("被勾选的条目有:");

                            for (int i = 0; i< myAdapter.getCount(); i++) {

                                     if(myAdapter.checkedStatusMap.get(i)) {

                                               sb.append(i+ ",");

                                     }

                            }

                            textView_main_info.setText(sb.toString());

                            break;

 

                   default:

                            break;

                   }

         }

 

         class MyAdapter extends BaseAdapter {

                   privateList<Map<String, Object>> list = null;

                   private Map<Integer,Boolean> checkedStatusMap = null;

 

                   publicMyAdapter(List<Map<String, Object>> list) {

                            this.list = list;

                            checkedStatusMap=new HashMap<Integer, Boolean>();

                            for (int i = 0; i< list.size(); i++) {

                                     checkedStatusMap.put(i,false);

                            }

                   }

 

                   @Override

                   public int getCount() {

                            // TODOAuto-generated method stub

                            return list.size();

                   }

 

                   @Override

           //实际上返回的是一个map对象

                   public Object getItem(intposition) {

                            // TODOAuto-generated method stub

                            returnlist.get(position);

                   }

 

                   @Override

                   public long getItemId(intposition) {

                            // TODOAuto-generated method stub

                            return position;

                   }

 

                   @Override

                   public View getView(intposition, View convertView, ViewGroup parent) {

                            ViewHolder mHolder;

                            if (convertView ==null) {

 

                                     mHolder =new ViewHolder();

 

                                     convertView= getLayoutInflater().inflate(

                                                        R.layout.listview_item_main,null);

                                              

                                     mHolder.textview_item_username= (TextView) convertView

                                                        .findViewById(R.id.textView_item_username);

                                     mHolder.textview_item_pwd= (TextView) convertView

                                                        .findViewById(R.id.textView_item_pwd);

                                     mHolder.checkBox_item= (CheckBox) convertView

                                                        .findViewById(R.id.checkBox_item);

                                     convertView.setTag(mHolder);

                            } else {

                                     mHolder =(ViewHolder) convertView.getTag();

                            }

 

                            mHolder.textview_item_username.setText(list.get(position)

                                               .get("username").toString());

                            mHolder.textview_item_pwd.setText(list.get(position).get("pwd")

                                               .toString());

                            mHolder.checkBox_item.setChecked(checkedStatusMap.get(position));

                            return convertView;

                   }

 

                   public class ViewHolder {

                            private TextViewtextview_item_username;

                            private TextViewtextview_item_pwd;

                            private CheckBoxcheckBox_item;

                   }

         }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值