adapter.getItem()会返回什么?

时间:2014-01-19 11:56:40

标签: java android listview android-listview android-arrayadapter

我为自定义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.

}

然而,如果(((personH​​older)adapter.getItem(i))。isChecked()== true)我在行中出现错误,说它无法从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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您应该确定是否选中getView()覆盖中的复选框。

您不应在适配器外的任何位置使用personHolder类 - 它仅用于回收视图。