2 o19830015 o19830015 于 2013.05.09 14:26 提问

Android CustomListAdapter有问题

在customlistadapter中创建了一行带有文本视图和复选框。列表显示没问题,只是在选择其中一项时其他项目也被自动选上了,滚动列表的话选择的选项又被取消了。

列表适配器的代码:

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"));
    }*/
}

1个回答

lyyybz
lyyybz   Rxr 2016.08.19 10:42

请问你的问题解决了么,我碰到和你一样的问题!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!