我正在从main.java调用名为list_item的自定义列表,但是当它在模拟器上加载时,它会为其余项目显示相同的图像。
main.java代码:
public class Main extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new MyAdapter(this, R.id.textView1,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.names)));
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.names);
ImageView iv = (ImageView) row.findViewById (R.id.imageView1);
TextView tv = (TextView) row.findViewById (R.id.textView1);
tv.setText(items[position]);
if (items[position].equals("rahman")) {
iv.setImageDrawable(R.drawable.rahman_two);
}
else if (items[position].equals("raheem")) {
iv.setImageResource(R.drawable.rahim_three);
}
else if (items[position].equals("malik")) {
iv.setImageResource(R.drawable.malik_four);
}
else if (items[position].equals("quddus")) {
iv.setImageResource(R.drawable.quddus_five);
}
return row;
}
}
答案 0 :(得分:0)
在这种情况下,position
是什么?你通过物品循环吗?
如果是这种情况,则ImageView应显示与项目中最后一个元素对应的图像。
@ Techno23您是否尝试执行以下操作:tv.getText().toString.equals("rahman");
答案 1 :(得分:0)
您确定要进入这些条件来设置图像吗?字符串匹配是否正在发生?如果是这样,将选择XML中指向的图像。