gpt4 book ai didi

带复选框的 Android CustomListAdapter 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 21:43:38 25 4
gpt4 key购买 nike

我正在开发一个 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"));
}*/

}

最佳答案

您可以尝试将 emailTxt 设为 CheckedTextView,然后像下面那样完成您的工作

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

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

}
});

关于带复选框的 Android CustomListAdapter 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16454606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com