我已按如下方式创建了自定义适配器。
public class sendivitesadapter extends ArrayAdapter<Item>{
private Context context;
private ArrayList<Item> items;
private qrusers qrusers;
private LayoutInflater vi;
private String[] array;
qrusers qrus;
public sendivitesadapter(Context context,ArrayList<Item> items) {
super(context, 0,items);
this.context= context;
this.qrusers =(qrusers) context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
@Override
public Item getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
sendItem ei = (sendItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
final TextView title = (TextView)v.findViewById(R.id.contactname);
final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
final CheckBox checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
//checkBox.setTag(qrus.uid);
// Log.e("IDDDDDDD", text);
//checkBox.setTag("12");
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s=(String) v.getTag();
Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
}
});
if (title != null)
title.setText(ei.contactname);
if(subtitle != null)
subtitle.setText(ei.companyname);
}
}
return v;
}
}
我将此适配器分配给列表视图的活动如下所示。
@Override
protected void onPostExecute(String result) {
JSONArray jarray;
try {
jarray= new JSONArray(result);
name= new String[jarray.length()];
company=new String[jarray.length()];
uid=new String[jarray.length()];
for (int i=0;i<jarray.length();i++){
JSONObject jobj = jarray.getJSONObject(i);
name[i]= jobj.getString("Name");
company[i]=jobj.getString("Company");
uid[i]=jobj.getString("UserID");
System.out.println(uid[i]);
items.add(new sendItem(name[i], company[i], qrcheckBox));
sendadapter = new sendivitesadapter(qrusers.this,items);
listView.setAdapter(sendadapter);
现在你看到我从webservice获得了名字,公司和uid。我在字符串数组uid中获取用户的ID。现在我想将uid设置为我在自定义适配器中创建的复选框。并点击它应该持有uid。我怎样才能做到这一点?
答案 0 :(得分:1)
尝试以下自定义适配器
private class Demo_List_Adapter extends ArrayAdapter<Data_Model> {
private ArrayList<Data_Model> inviteList;
public Demo_List_Adapter(Context context, int textViewResourceId,
ArrayList<Data_Model> inviteList) {
super(context, textViewResourceId, inviteList);
this.inviteList = new ArrayList<Data_Model>();
this.inviteList.addAll(inviteList);
}
private class ViewHolder {
TextView textView_FromPersonName;
TextView textView_Subject;
TextView textView_Body;
TextView textView_Date;
ImageView imageView_Star;
CheckBox checkBox_MsgSelection;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_row_message_inbox, null);
holder = new ViewHolder();
holder.textView_FromPersonName = (TextView) convertView
.findViewById(R.id.textView_FromPersonName);
holder.textView_Subject = (TextView) convertView
.findViewById(R.id.textView_Subject);
holder.textView_Body = (TextView) convertView
.findViewById(R.id.textView_Body);
holder.textView_Date = (TextView) convertView
.findViewById(R.id.textView_Date);
holder.imageView_Star = (ImageView) convertView
.findViewById(R.id.imageView_Star);
holder.checkBox_MsgSelection = (CheckBox) convertView
.findViewById(R.id.checkBox_MsgSelection);
convertView.setTag(holder);
holder.checkBox_MsgSelection
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Data_Model inviteModel = (Data_Model) cb
.getTag();
inviteModel.setChecked(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Data_Model inviteModel = inviteList.get(position);
if (inviteModel.getJa_MS_IsRead().equals("0"))
convertView.setBackgroundColor(getActivity().getResources()
.getColor(R.color.gray_background));
else
convertView.setBackgroundColor(getActivity().getResources()
.getColor(R.color.white));
holder.textView_FromPersonName.setText(inviteModel
.getJa_ME_FromPesrsonName());
holder.textView_Subject.setText(inviteModel.getJa_ME_Subject());
holder.textView_Body
.setText(stripHtml(inviteModel.getJa_ME_Body()));
String s[] = inviteModel.getJa_ME_CreatedDate().split("\\s+");
holder.textView_Date.setText(s[0]);
if (inviteModel.getJa_MS_IsFlagged().trim().equalsIgnoreCase("1")) {
holder.imageView_Star.setImageResource(R.drawable.star);
} else {
holder.imageView_Star.setImageResource(R.drawable.star_unsele);
}
holder.checkBox_MsgSelection.setTag(inviteModel);
return convertView;
}
}
holder.checkBox_MsgSelection.setTag(inviteModel);用于设置标签