0

我爲自定義ListView創建了一個自定義數組適配器,它具有TextView和複選框。在我的customadapter中,我創建了一個名爲isChecked()的方法 -adapter.getItem()返回什麼?

static class personHolder 
    { 
     TextView txtName; 
     TextView txtNumber; 
     CheckBox checkbox; 
     public boolean isChecked(){ 
      boolean isChecked_boolean = false; ///this holds the return value, of whether the checkbox is checked or not. 
      if(checkbox.isChecked()){ 
       isChecked_boolean = true; 
      } 
      else{ 
       isChecked_boolean = false; 
      } 
      return isChecked_boolean; 
     } 
    } 
} 

好的。我知道使用循環通過我的觀點迭代像這樣:

public ArrayList<PeopleDetails> checkbox_SMS(ListAdapter adapter){ 
    ArrayList<PeopleDetails> people_SMS = new ArrayList<PeopleDetails>(); 
    for(int i = 0; i < adapter.getCount(); i++){ 
     if(((personHolder) adapter.getItem(i)).isChecked() == true){ 
      people_SMS.add((PeopleDetails) people_details.toArray()[i]); 
      ///adds the corresponing people_details item to the arraylist of PeopleDetails, to send SMS messages to. 
     } 
    } 
    return people_SMS; ///returns the list. 

} 

不過,我在該行得到一個錯誤,如果(((personH​​older)adapter.getItem(I))器isChecked()==真)說它不能從PeopleDetails投射到personH​​older。 PeopleDetails是我傳遞給我的customadapter用於TextViews中的數據的類型。我做錯了什麼?

問題 - 應該怎樣才能正確地得到我想要的結果?謝謝您的幫助!!

package com.example.partyorganiser; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class peoplelist_customadapter extends ArrayAdapter<PeopleDetails>{ 

    Context context; 
    int layoutResourceId;  
    ArrayList<PeopleDetails> data = null; 

    public peoplelist_customadapter(Context context, int layoutResourceId, ArrayList<PeopleDetails> data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     personHolder holder = null; 
     PeopleDetails[] people_array = data.toArray(new PeopleDetails[data.size()]); 


     if(row == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      holder = new personHolder(); 
      holder.txtName = (TextView)row.findViewById(R.id.name_txt); 
      holder.txtNumber = (TextView)row.findViewById(R.id.number_txt); 
      holder.checkbox = (CheckBox) row.findViewById(R.id.checkBox); 
      row.setTag(holder); 
     } 
     else 
     { 
      holder = (personHolder)row.getTag(); 
     } 

     PeopleDetails person = people_array[position]; 
     holder.txtName.setText(person.name); 
     holder.txtNumber.setText(person.number); 

     ///CheckBox mCheckBox = (CheckBox) mView.findViewById(R.id.checkBox); 


     return row; 
    } 
    public boolean getView_IsChecked(int position){ 

    } 



    static class personHolder 
    { 
     TextView txtName; 
     TextView txtNumber; 
     CheckBox checkbox; 
     public boolean isChecked(){ 
      boolean isChecked_boolean = false; ///this holds the return value, of whether the checkbox is checked or not. 
      if(checkbox.isChecked()){ 
       isChecked_boolean = true; 
      } 
      else{ 
       isChecked_boolean = false; 
      } 
      return isChecked_boolean; 
     } 
    } 
} 
+0

你可以發佈適配器的代碼嗎? getItem()應該返回任何你實現它返回 – MikeHelland

回答

1

您應該確定是否檢查getView()重寫中的複選框。

您不應該在適配器外的任何地方使用personHolder類 - 它僅適用於回收視圖。

+0

getView()通常返回一個視圖,但我想要一個布爾值。我認爲getView()被用於誇大我的ListView。 –

+1

視圖將被檢查與否取決於你的PeopleDetails類是什麼,是嗎?在這種情況下,您只需調用getItem()來獲取PeopleDetails,並通過相同的邏輯知道該複選框是否已選中。如果用戶可以切換不同問題的複選框。 –

+0

用戶將切換複選框。獲取它是否被檢查的最簡單方法是什麼,然後將其添加到單獨的數組列表中?我想要將選中的項目添加到單獨的數組列表中,並忽略其他項目。 –