gpt4 book ai didi

java - adapter.getItem() 将返回什么?

转载 作者:行者123 更新时间:2023-12-01 13:36:43 25 4
gpt4 key购买 nike

我为自定义 ListView 创建了一个自定义数组适配器,其中包含 TextView 和复选框。在我的自定义适配器中,我创建了一个名为 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;
}
}
}

好的。我知道使用一个循环来遍历我的 View ,如下所示:

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 是我传递给 TextView 中数据的自定义适配器的类型。我做错了什么?

问题 - 该行应该是什么才能正确获得我想要的结果?感谢您的帮助!!

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() 覆盖中。

您不应在适配器之外的任何地方使用 personH​​older 类 - 它仅用于回收 View 。

关于java - adapter.getItem() 将返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21216420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com