Android CustomListAdapter,复选框无法正常工作

时间:2013-05-09 04:51:47

标签: android listadapter

我正在开发一个customlistadapter,我在其中创建了一个带有textview和checkbox的行。 列表正在正确呈现,但是当我选择一个项目时,会自动选择其他项目,当我滚动列表时,所选项目未被选中。

这是我的列表适配器代码:

import java.util.ArrayList;

import com.example.interactiveapp.R;
import com.interactiveapp.Constants;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class FBContactlistAdapter extends BaseAdapter implements Filterable {
    private LayoutInflater mInflater;
    private ArrayList<Object> itemList;
    private Activity _activity;

    String contactName="";
    String photoPath="";
    String emailId="";

    public FBContactlistAdapter(Activity activity, ArrayList<Object> itemList) {
        this._activity = activity;
        this.itemList = itemList;
        mInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return itemList != null ? itemList.size(): 0;
    }

    public Object getItem(int position) {
        return itemList.get(position);
    }

    public Filter getFilter() {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolder {
        CheckBox checkBox;
        TextView emailTxt;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        final ContactBean contactBean=(ContactBean)getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.fb_contact_list_row, null);
            holder = new ViewHolder();
            holder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1);
            holder.emailTxt = (TextView)convertView.findViewById(R.id.username_txt);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.emailTxt.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                emailId=contactBean.getEmailId();
                if(contactBean.isSelected==false){
                    contactBean.setSelected(true);
                    holder.checkBox.setChecked(true);
                    Constants.fbContactListArrayList.set(position, contactBean);
                }else{
                    contactBean.setSelected(false);
                    holder.checkBox.setChecked(false);
                    Constants.fbContactListArrayList.set(position, contactBean);
                }
            }
        });

        //SET VALUES ON LIST VIEW
        emailId=contactBean.getEmailId();
        if(emailId.length()!=0 ||emailId!=null){
            holder.emailTxt.setText(emailId);
        }
        return convertView;
    }

    //HANDLER
    private Handler _handler = new Handler() {
        public void dispatchMessage(Message msg) {
            switch (msg.arg1) {

            case 123:
                break;
            default:
                break;
            }
        }
    };

    public void serverResponse(String response, int processid) {
        Message msg = new Message();
        msg.arg1 = processid;
        msg.obj = response;
        _handler.dispatchMessage(msg);
    }

    /*//SEND EMAIL TO INVITE FRIEND
    public void sendEmail(String emailId){
        Intent intent = new Intent(Intent.ACTION_SEND);
        //intent.setType("text/plain");
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_EMAIL, emailId);
        intent.putExtra(Intent.EXTRA_SUBJECT,Constant.EMAIL_SUBJECT);
        intent.putExtra(Intent.EXTRA_TEXT, Constant.EMAIL_CONTENT);
        _activity.startActivity(Intent.createChooser(intent, "Send Email"));
    }*/

}

3 个答案:

答案 0 :(得分:1)

您已在getView()中刷新数据,因为listview将回收视图:

public View getView(final int position, View convertView, ViewGroup parent) {
    final ContactBean contactBean=(ContactBean)getItem(position);
    final ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.fb_contact_list_row, null);
        holder = new ViewHolder();
        holder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1);
        holder.emailTxt = (TextView)convertView.findViewById(R.id.username_txt);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.emailTxt.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            emailId=contactBean.getEmailId();
            if(contactBean.isSelected==false){
                contactBean.setSelected(true);
                holder.checkBox.setChecked(true);
                Constants.fbContactListArrayList.set(position, contactBean);
            }else{
                contactBean.setSelected(false);
                holder.checkBox.setChecked(false);
                Constants.fbContactListArrayList.set(position, contactBean);
            }
        }
    });

    //SET VALUES ON LIST VIEW
    emailId=contactBean.getEmailId();
    if(emailId.length()!=0 ||emailId!=null){
        holder.emailTxt.setText(emailId);
    }
    holder.setChecked(contactBean.isSelected);
    return convertView;
}

如果您不想回收视图,您可以看到我自己管理视图的另一个答案: Android, List Adapter returns wrong position in getView

答案 1 :(得分:1)

您可以尝试将emailTxt设为CheckedTextView,然后执行以下工作

    holder.emailTxt.setChecked(contactBean.isSelect);
    holder.emailTxt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            contactBean.isSelect = ! contactBean.isSelect;
            notifyDataSetChanged();

        }
    });

答案 2 :(得分:0)

需要做两件事

  1. 获取ArrayList中已选中的项目并填充onChecked名称或ID或email wich Unique
  2. 将该项目与当前getting名称或ID wich匹配,填写ArraList
  3. 如果(checkedid.contains(allReadyInvitedSetGet.getNvitedlist_gmail()。得到(位置).getEmail())

    {
           holder.chbox.setChecked(真);

                    } else {
                        holder.chbox.setChecked(false);
                    }