0

這是我的ContactsAdapter類,從中顯示帶有複選框的列表。在這裏我可以得到檢查項目的標籤。還有另一個類FanFragment,其中點擊一個按鈕(ContactList),我必須得到所檢查項目數組中標籤的所有值。在android listview中獲取檢查項數組中的標記值

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 

    } 
    }); 

} 

回答

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 
} 

希望這會有所幫助。

+0

那是什麼學生出勤登入 – Munchoo 2014-09-11 09:32:57

相關問題