Android 使用If语句设置图像ListView

Android 使用If语句设置图像ListView,android,listview,Android,Listview,我有一个具有动态列表项长度的图像listview,我只有3个图像,但是如果状态值为OK、OFFLINE或PENDING,则必须在每个项目上设置图像,我如何使用IF-ELSE IF和ELSE在每个列表视图项中设置3个图像中的一个,下面是我的代码示例: Integer[] imageId = { R.drawable.round_green, R.drawable.round_red, R.drawable.round_grey

我有一个具有动态列表项长度的图像listview,我只有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
ladder,使用
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);
我可以使用IF-ELSE IF和ELSE设置每个列表中3个图像中的一个吗 查看项目

我们可以不使用
if-else
ladder,使用
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);

在adapter 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...
}

在adapter 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...
}

如果您使用的是自定义listview,则提供getView()方法的CodeEdit以显示getView()方法。您在哪里检查状态?我认为此代码将第一个图像放在第一个位置,第二个图像放在第二个位置,因此,如果您使用的是自定义listview,则提供getView()方法的CodeEdit以显示getView()方法你在哪里检查状态我想这段代码会把第一个图像放在第一个位置,第二个图像放在第二个位置,依此类推。甚至可以使用switch子句检查状态,并直接使用drawable。我不明白你为什么要在通过最终测试时使用数组。甚至可以使用switch子句检查状态,并直接使用drawable。我不明白在传递最终ID时为什么要使用数组