我为我的自定义ListView创建了一个自定义数组适配器,该适配器具有TextViews和一个Checkbox。在我的customadapter中,我创建了一个名为isChecked()的方法-

    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.

}


但是我在if((((personH​​older)adapter.getItem(i))。isChecked()== true)行上遇到错误,说它无法从PeopleDetails转换为personH​​older。 PeopleDetails是我传递给自定义适配器的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;
        }
    }
}

最佳答案

您应该采用逻辑来确定是否选中getView()替代中的复选框。

您不应该在适配器之外的任何地方使用personHolder类,因为它仅在回收视图中使用。

10-07 19:33