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

用户将切换复选框。获取它是否被检查的最简单方法是什么,然后将其添加到单独的数组列表中?我想要将选中的项目添加到单独的数组列表中,并忽略其他项目。 –