我爲自定義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.
}
不過,我在該行得到一個錯誤,如果(((personHolder)adapter.getItem(I))器isChecked()==真)說它不能從PeopleDetails投射到personHolder。 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;
}
}
}
你可以發佈適配器的代碼嗎? getItem()應該返回任何你實現它返回 – MikeHelland