我有一个列表视图,其列表项包括一个文本视图和一个复选框。Textview 从数据库中获取数据。所以我的列表视图由多个文本视图组成,每个文本视图都有一个复选框。我的问题是我无法将每个复选框映射到相应的列表项
问问题
113 次
2 回答
1
做一个像这样的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout >
在自定义适配器自定义适配器中使用它:
public class AttendeeAdapter extends ArrayAdapter<String> {
private LayoutInflater inflator = null;
private ArrayList<String> attendeeList = null;
public AttendeeAdapter(Context context, int resource,
List<String> objects) {
super(context, resource, objects);
attendeeList = (ArrayList<String>) objects;
inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
convertView = inflator.inflate(R.layout.client_row, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.txtViewName = (TextView) convertView.findViewById(R.id.txtViewClientName);
} else {
holder = (ViewHolder) convertView.getTag();
}
String attendee = attendeeList.get(position);
holder.txtViewName.setText("name");
return convertView;
}
private static class ViewHolder {
public TextView txtViewName = null;
public CheckBox check= null;
}
}
R.layout.client_row 是包含文本视图和复选框的自定义布局
于 2013-07-05T11:27:22.283 回答
0
使用ViewHolder类绑定您的列表视图。
以下是视图持有者 m 用于在列表视图中的每一行设置朋友姓名和复选框的示例
//Array Adapter 用于在 Listview 上显示好友姓名
public class FriendAdapter extends ArrayAdapter<BinforFacebook>
{
ImageLoader loader=new ImageLoader(getApplicationContext());
Bitmap res;
String url;
ArrayList<BinforFacebook> namelist = new ArrayList<BinforFacebook>();
public FriendAdapter(Context context,int textViewResourceId,ArrayList<BinforFacebook> objects)
{
super(getApplicationContext(), textViewResourceId, objects);
this.namelist=objects;
// TODO Auto-generated constructor stub
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
ContactsViewHolder viewHolder;
// loader.clearCache();
Log.v("in get View","ger View of new create miline");
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.friendsnameadd, parent, false);
viewHolder = new ContactsViewHolder();
viewHolder.txtname = (TextView) convertView.findViewById(R.id.name_contact);
viewHolder.txtmilineuptag = (TextView) convertView.findViewById(R.id.tagmilineup);
viewHolder.chkmiteam = (CheckBox) convertView.findViewById(R.id.Checkmilineup);
viewHolder.frndimg = (ImageView) convertView.findViewById(R.id.frndimg);
//adding facebook friend id into list to store in database and to post image to their wall
viewHolder.chkmiteam.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getPosition = (Integer) buttonView.getTag();
namelist.get(getPosition).setFlag((buttonView.isChecked())); // Set the value of checkbox to maintain its state.
if(isChecked)
{
Log.v("namelist id",namelist.get(getPosition).getFbid().toString());
if(!fbidlist_todb.contains(namelist.get(getPosition).getFbid().toString()))
{
fbidlist_todb.add(namelist.get(getPosition).getFbid().toString());
}
else
{
fbidlist_todb.remove(namelist.get(getPosition).getFbid());
}
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.name_contact, viewHolder.txtname);
convertView.setTag(R.id.tagmilineup, viewHolder.txtmilineuptag);
convertView.setTag(R.id.Checkmilineup, viewHolder.chkmiteam);
convertView.setTag(R.id.frndimg, viewHolder.frndimg);
}
else {
viewHolder = (ContactsViewHolder) convertView.getTag();
}
viewHolder.chkmiteam.setTag(position); // This line is important.
viewHolder.txtname.setText(namelist.get(position).getFbname());
viewHolder.chkmiteam.setChecked(namelist.get(position).flag);
url=(namelist.get(position).getUrl());
Log.v("url in getview",url);
// viewHolder.frndimg.setImageBitmap(namelist.get(position).getB1());
loader.DisplayImage(url,viewHolder.frndimg);
/*checking if current position friend is already added in to team or not.
* if added then make checkbox checked
*/
if(namelist.get(position).getFbid()!=null)
{
Log.v("fb name list uid",namelist.get(position).getFbid());
for(int i=0;i<fbidlist_fromdb.size();i++)
{
if(namelist.get(position).getFbid().equals(fbidlist_fromdb.get(i).toString()))
{
viewHolder.chkmiteam.setChecked(true);
}
}
}
return convertView;
}
}
于 2013-07-05T11:28:22.863 回答