如何添加那些依赖于TextView的文本在ListView的ImageView [英] How to add ImageView's that are depending on the text of the TextView in a ListView
问题描述
我有一个自定义的的ListView
与 RowItem
包含的TextView
和的ImageView
。
在 RowItems
将被动态添加。
I have a custom ListView
with a RowItem
that contains TextView
and a ImageView
.
The RowItems
will be added dynamically.
我只是想知道如何添加的ImageView
来在一个特定的文本的TextView
。
I just want to know how to add the ImageView
to a specific text in the TextView
.
所以,如果在的TextView
表示标题1时,的ImageView
R.drawable.ic_title1应该是添加到 RowItem
。
So if in the TextView
stands "Title1" ,the ImageView
"R.drawable.ic_title1" should be added to the RowItem
.
下面是我的code
class SessionItemAdapter extends ArrayAdapter<Map> {
final ArrayList<Map> values;
Context context;
SessionItemAdapter(Context context, ArrayList<Map> values) {
super(context, R.layout.session_list_layout_row, values);
this.values = values;
this.context = context;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.session_list_layout_row, parent, false);
TextView tv = (TextView) rowView.findViewById(R.id.stepTime);
final int rowId = (Integer) values.get(position).get("id");
int beepStringId = getResources().getIdentifier("exercise_typ_" + values.get(position).get("type"), "string", getPackageName());
rowView.setTag(rowId);
tv.setText(getResources().getString(beepStringId));
ImageView image =(ImageView) rowView.findViewById(R.id.imageView2);
switch(position){
case 1 : image.setImageResource(R.drawable.ic_title1);
break;
case 2 : image.setImageResource(R.drawable.ic_title2);
break;
}
ImageButton deleteSession = (ImageButton) rowView.findViewById(R.id.session_item_del);
deleteSession.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*if (!startStopToggle.isChecked()) {*/
sessionAdapter.remove(values.get(position));
sessionAdapter.notifyDataSetChanged();
}
});
return rowView;
}
//in your adapter getAllPlayers() would be something like this
public List<String> getAllPlayers() {
List<String> list = new ArrayList<String>();
for (Map item : values) {
int beepStringId = context.getResources().getIdentifier("exercise_typ_"
+ item.get("type"), "string", context.getPackageName());
String str = context.getResources().getString(beepStringId);
list.add(str);
}
return list;
}
}
我的切换
语句试图将其与位置
但只设置的ImageView
到 RowItem
是上的位置多数民众赞成写在情况
。
I tried it with the Switch
statement with position
but that only set the ImageView
to the RowItem
that is on the position thats written in the case
.
推荐答案
您可以根据的文本中的switch-case
语句的TextView
像下面
You can make a switch-case
statement based on the text of the TextView
like below
switch(tv.getText().toString()){
case "Title1" : image.setImageResource(R.drawable.ic_title1);
break;
case "Title2" : image.setImageResource(R.drawable.ic_title2);
break;
default:
//Default image here, if no case found
break;
}
注:上面的回答需要JRE 1.7符合
Note: above answer requires JRE 1.7 compliance
这篇关于如何添加那些依赖于TextView的文本在ListView的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!