这可能是一个愚蠢的问题,但我有点难以让它发挥作用。
我有一个自定义列表视图适配器,其中包含一些textview和imageview。 但是当我使用自定义适配器更新listview时,它只更新最后一个值。这是我的代码
适配器代码
public class InfoListAdapter extends ArrayAdapter<aSProperty> {
Context mContext;
List<aSProperty> values= null;
public InfoListAdapter(Context mContext, List<aSProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data.;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_row, null);
signal = (ImageView) convertView.findViewById(R.id.images);
type = (TextView) convertView.findViewById(R.id.type);
info = (TextView) convertView.findViewById(R.id.info);
image.setBackgroundResource(R.drawable.abc);
Log.d(TAG, "List Size"+values.size());
for(int i = 0; i < values.size(); i++){
Log.d(TAG, "DATA"+values.get(i).type);
type.setText(values.get(i).type);
}
}
ImageView image;
TextView type;
TextView info;
static String TAG= "InfoListAdapter";
return convertView;
}
}
填充适配器的代码
info_panel = new InfoListAdapter(esActivity.this, allData);
list.setAdapter(info_panel);
但我不知道为什么,它只显示值列表中的最后一个值。另外我想根据条件和数据内部值更改图像,这是做任何好方法。
答案 0 :(得分:2)
use following code
public class InfoListAdapter extends ArrayAdapter<aSProperty> {
Context mContext;
List<aSProperty> values= null;
public InfoListAdapter(Context mContext, List<aSProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data.;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_row, null);
signal = (ImageView) convertView.findViewById(R.id.images);
type = (TextView) convertView.findViewById(R.id.type);
info = (TextView) convertView.findViewById(R.id.info);
image.setBackgroundResource(R.drawable.abc);
Log.d(TAG, "List Size"+values.size());
type.setText(values.get(position).type);
}
ImageView image;
TextView type;
TextView info;
static String TAG= "InfoListAdapter";
return convertView;
}
}
答案 1 :(得分:0)
首先,您必须覆盖适配器的getCount()方法,如此
@Override
public int getCount() {
return values .size();
}
然后在你的getView()方法中你必须做这样的事情
@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.mycartslistcontents,
null);
holder.yourImageView= (ImageView) convertView
.findViewById(R.id.image);
holder.yourTextView= (TextView) convertView
.findViewById(R.id.textview);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
// here set your textView text and image resource
return convertView;
}
别忘了像这样在顶部采用全局变量layoutInflater
private LayoutInflater layoutInflater;
并以这种方式在构造函数中初始化它
layoutInflater = LayoutInflater.from(yourContext);
这是您的ViewHolder类
static class ViewHolder {
ImageView yourImageView;
TextView yourTextView;
}