我正在一個自定義適配器,其中我創建了一行textview和複選框。 列表正在呈現正確,但是當我選擇一個項目自動選擇一些其他項目,當我滾動列表選定的項目被取消選擇。Android 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"));
}*/
}
感謝博爾頓it'w工作。 – 2013-05-09 05:28:27