获取android listview中已检查项目数组中的Tag值

时间:2014-09-11 08:08:11

标签: android android-listview android-checkbox android-viewholder

这是我的ContactsAdapter类,使用Checkbox显示列表。在这里我可以获得已检查项目的标签。还有另一个类FanFragment,在单击按钮(ContactList)时,我必须获取已检查项目数组中Tag的所有值。

public class ContactsAdapter extends AbsAdapter {

TextView contactId = null;
TextView contactDesc = null;
static boolean checked[];
String[] arrPath;
int selectedCount;
CheckBox saveddesc;

public ContactsAdapter(Activity activity, ArrayList<RantModel> contenList) {
    super(activity, contenList);
}   

@Override
public View getView(int position, View convertview, ViewGroup parent) {
    // TODO Auto-generated method stub      
    super.getView(position, convertview, parent);
    final ViewHolder holder;

    LayoutInflater inflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertview == null){
    holder = new ViewHolder();
    convertview = inflater.inflate(R.layout.contacts_row_layout, parent,
            false);
    try {
        holder.contactId = (TextView) convertview.findViewById(R.id.tvListId);
        holder.contactDesc = (TextView) convertview.findViewById(R.id.tvListName);
        holder.saveddesc = (CheckBox) convertview.findViewById(R.id.ckList);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    convertview.setTag(holder);
    } else {

        holder = (ViewHolder) convertview.getTag();
    }


    holder.saveddesc.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(((CompoundButton) v).isChecked()){

                String ale = (String) holder.saveddesc.getTag();

                System.out.println(ale);
            }
        }
    });

    RantModel ContactModel = mListContent.get(position);
    holder.contactDesc.setText(ContactModel.contactName);
    holder.contactId.setText(ContactModel.contactId);
    holder.saveddesc.setId(position);
    holder.saveddesc.setTag(ContactModel.contactId);
    holder.id = position;

    return convertview;
}

class ViewHolder{

    public int id;
    public CheckBox saveddesc;
    public TextView contactDesc;
    public TextView contactId;

}

FanFragment.class

public class FanFragment extends AbsFragment {

ContactsAdapter mFanContacts;
ArrayList<RantModel> mContactList;
String url;
ContactListService contactsWeb;
private ProgressDialog mProgressDialog = null;
private boolean checked[];

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    mContactList = new ArrayList<RantModel>();
    mProgressDialog = new ProgressDialog(getActivity());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

    mContactList.removeAll(mContactList);
    listView.setAdapter(null);
    url = "";
    contactsWeb = new ContactListService();
    contactsWeb.execute();

    mFanthemContacts = new ContactsAdapter(mActivity, mContactList);
    listView.setAdapter(mFanthemContacts);

    //
     ContactList.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {
     // TODO Auto-generated method stub

     }
     });

}

1 个答案:

答案 0 :(得分:0)

你可以这样做。为我工作。在片段中,单击按钮或触发器即可执行此操作。

  for (int i = 0; i < mContactList.getCount(); i++) {

                   View v = listView.getAdapter().getView(i, null, null);
            Viewholder holder = (ViewHolder) v.getTag();
//Check condition for checkbox and insert into new array
}

希望这会有所帮助。