从listview baseadapter获取复选框

时间:2013-03-19 00:38:13

标签: android listview checkbox baseadapter

我在布局中有列表视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100" >

<ListView
        android:id="@+id/listInbox"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:layout_weight="20"
        android:listSelector="@drawable/list_selector" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="80"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/bdelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="@string/delete_selected" />

</LinearLayout>

</LinearLayout>

并且这是此列表视图的布局适配器

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- ListRow Left sied Thumbnail image -->

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:background="@drawable/image_bg"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip" />
    </LinearLayout>

    <!-- Title Of Song -->

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Title"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- Artist Name -->

    <TextView
        android:id="@+id/artist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:gravity="left"
        android:text="Sender"
        android:textColor="#343434"
        android:textSize="15sp" />

    <!-- Rightend Duration -->


    <!-- Rightend Arrow -->

    <ImageView
        android:id="@+id/ivArraw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/url" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/artist"
        android:layout_alignBottom="@+id/artist"
        android:layout_alignRight="@+id/artist" 
        android:visibility="invisible"
        />

</RelativeLayout>

以及适配器在我的活动中定义的内容

        lv.setAdapter(new ImageInboxAdapter(inbox.this, bitmap, messageNo, messageTitle, senderId, senderName,selected));

这是baseadapter代码

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageNewsAdapter extends BaseAdapter{
    Context context;
    String[] newsNo,title_news;
    Bitmap[] bitmap;
    public ImageNewsAdapter(Context context,String[] newsNo,String[] title_news, Bitmap[] bitmap ) {
        this.context=context;
        this.newsNo=newsNo;
        this.title_news=title_news;
        this.bitmap=bitmap;
        // TODO Auto-generated constructor stub
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return title_news.length;
    }

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

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if(convertView==null){

            gridView=new View(context);

            gridView=inflater.inflate(R.layout.custom_news, null);




        }else{
            gridView = (View) convertView;
        }

        TextView tv=(TextView)gridView.findViewById(R.id.tvTitle);

        tv.setText(title_news[position]);

        /*RelativeLayout rlImageMain=(RelativeLayout)gridView.findViewById(R.id.RlImageTitle);

        rlImageMain.setBackground(new BitmapDrawable(bitmap[position]));

        */
        ImageView ivMain= (ImageView)gridView.findViewById(R.id.ivMain);

        ivMain.setImageBitmap(bitmap[position]);
        return gridView;
    }


}

我希望从复选框中获取选中值并将其返回到我的活动

的问题

1 个答案:

答案 0 :(得分:1)

我找到了解决方案

首先制作一个静态布尔变量

static Boolean checkboxstate[];

然后在构造函数中使用列表的大小

定义它
checkboxstate=new Boolean[messageNo.length];

然后在单击侦听器上创建复选框并填充布尔数组

if(checkboxstate[position]==null){
                checkboxstate[position]=false;
            }
            cb.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(((CheckBox)v).isChecked()){
                        checkboxstate[position]=true;
                        v.setSelected(true);
                    }else{
                        checkboxstate[position]=false;
                        v.setSelected(false);
                    }
                }
            });

最后

public class ImageInboxAdapter extends BaseAdapter{
    Context context;
    String[] messageNo,messageTitle,senderId,senderName;
    Bitmap[] bitmap;
    Boolean selected;
    static Boolean checkboxstate[];
    int checkedp;
    public ImageInboxAdapter(Context context, Bitmap[] bitmap,String[] messageNo,String[] messageTitle, String[] senderId,String[] senderName,Boolean selected) {
        this.context=context;
        this.bitmap=bitmap;
        this.messageNo=messageNo;
        this.messageTitle=messageTitle;
        this.senderId=senderId;
        this.senderName=senderName;
        this.selected=selected;
        checkboxstate=new Boolean[messageNo.length];



    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return messageNo.length;
    }

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

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView;
        if(convertView==null){

            gridView=new View(context);

            gridView=inflater.inflate(R.layout.list_row_message, null);




        }else{
            gridView = (View) convertView;
        }

        TextView tvTitle=(TextView)gridView.findViewById(R.id.title);
        tvTitle.setText(messageTitle[position]);

        TextView tvSender=(TextView)gridView.findViewById(R.id.artist);
        tvSender.setText(senderName[position]);

        CheckBox cb=(CheckBox)gridView.findViewById(R.id.checkBox1);
        ImageView ivArraw=(ImageView)gridView.findViewById(R.id.ivArraw);

        /*RelativeLayout rlImageMain=(RelativeLayout)gridView.findViewById(R.id.RlImageTitle);

        rlImageMain.setBackground(new BitmapDrawable(bitmap[position]));

        */
        ImageView ivMain= (ImageView)gridView.findViewById(R.id.list_image);

        ivMain.setImageBitmap(bitmap[position]);

        if(selected==true){
            cb.setVisibility(CheckBox.VISIBLE);
            ivArraw.setVisibility(ImageView.INVISIBLE);
            if(checkboxstate[position]==null){
                checkboxstate[position]=false;
            }
            cb.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(((CheckBox)v).isChecked()){
                        checkboxstate[position]=true;
                        v.setSelected(true);
                    }else{
                        checkboxstate[position]=false;
                        v.setSelected(false);
                    }
                }
            });
        }
        return gridView;
    }

}