我编写了一个代码,用于从服务器检索数据并将其显示在一个简单的列表中,并且它可以成功运行,但是当我使用自定义列表视图“其中每行包含textviews和checkbox”时,它不起作用..
这是我的课程:
public class ItemInList {
private String name;
private float Description;
private boolean selected;
public ItemInList(String name, float Description) {
this.name = name;
this.Description =Description;
selected = false;
}
public String getName() {
return name;
}
public float getDescription() {
return Description;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(float Description) {
this.Description = Description;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
这是适配器类
public class DataAdapter extends ArrayAdapter<ItemInList> {
public ArrayList<ItemInList> list;
public Activity context;
public LayoutInflater inflater;
public DataAdapter(Activity context,int x,ArrayList<ItemInList> list) {
super(context, 0, list);
this.context = context;
this.list = list;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
protected TextView name,Description;
protected CheckBox checkbox;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public ItemInList getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row1, null);
holder.name = (TextView) convertView.findViewById(R.id.food_title);
holder.name.setTextColor(Color.BLACK);
holder.Description = (TextView) convertView.findViewById(R.id.food_description);
holder.Description.setTextColor(Color.GRAY);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.add_food_item);
//viewHolder.checkbox
//.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//@Override
//public void onCheckedChanged(CompoundButton buttonView,
//boolean isChecked) {
//ItemInList element = (ItemInList) viewHolder.checkbox.getTag();
//element.setSelected(buttonView.isChecked());
//System.out.println("Checked : " + element.getName());
//}
//});
convertView.setTag(holder);
} else {
holder=(ViewHolder)convertView.getTag();
ItemInList bean = (ItemInList) list.get(position);
holder.name.setText( bean.getName());
holder.Description.setText( bean.getDescription()+"");
holder.checkbox.setChecked( bean.isSelected());
return convertView;
}
holder.name.setText(list.get(position).getName());
holder.Description.setText(list.get(position).getDescription()+"");
holder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
答案 0 :(得分:1)
你没有发起
ItemInList item;
在doInBackground()
中设置值之前item.setName(foodName);
//...
答案 1 :(得分:0)
如果从LoadData.doInBackground()中删除“return null”,它是否有效?毕竟,它是一个无效函数,不应该返回任何东西。