我有一个具有动态列表项长度的图像列表视图,我只有3个图像,但必须仅在每个项上设置图像。如果状态值为OK、OFFLINE或PENDING,我如何使用IF ELSE IF和ELSE在每个列表视图项中设置3个图像中的一个,我的代码示例如下:
Integer[] imageId = {
R.drawable.round_green,
R.drawable.round_red,
R.drawable.round_grey,
};
ContactsAdapter adapter = new ContactsAdapter(this, itemTitle, itemStatus, imageId);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row=convertView;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.activity_item, parent, false);
}
TextView tvTitle = (TextView) row.findViewById(R.id.Title);
TextView tvStatus = (TextView) row.findViewById(R.id.Status);
ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
tvTitle.setText(Title.get(position));
tvStatus.setText(Status.get(position));
imageView.setImageResource(imgid[position]); //this must set either image A or B or C depending on value of text view status which can either be OK, OFFLINE or PENDING
return row;
}
我可以使用IF ELSE IF和ELSE设置每个列表中的3个图像中的一个吗查看项目
我们可以在不使用if-else
梯形图的情况下使用HashMap
。
在HashMap:中使用OK、OFFLINE或PENDING作为键,使用drawable
id作为值
Map<String, Integer> drawableMap = new HashMap<String, Integer>();
drawableMap.put("ok",R.drawable.round_green);
drawableMap.put("pending",R.drawable. round_grey);
drawableMap.put("offline",R.drawable. round_red);
现在使用Status.get(position)
作为检索可提取id:的密钥
imageView.setImageResource(drawableMap.get(Status.get(position).toLowerCase()));
在适配器中ContactsAdapter进入getView方法内部,并使条件类似
if(value.equal("OK"))
{
//set your image...
}
else if(value.equal("OFFLINE"))
{
//set your image...
}
else if(value.equals(PENDING))
{
//set your image...
}