我的自定义ListView
的{{1}}包含RowItem
和TextView
。
ImageView
将动态添加。
我只是想知道如何将RowItems
添加到ImageView
中的特定文字。
因此,如果TextView
代表“Title1”,TextView
“R.drawable.ic_title1”应添加到ImageView
。
这是我的代码
RowItem
我使用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;
}
}
语句与Switch
进行了尝试,但只将position
设置为ImageView
,该RowItem
位于case
所写的位置上}。
答案 0 :(得分:1)
您可以根据switch-case
的文字制作TextView
声明,如下所示
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
答案 1 :(得分:1)
这样的事情怎么样。
image.setImageResource(getImageId(this, "ic_" + tv.getText().toString().toLowerCase());
所以基本上你从文本视图中获取标题,操纵它的格式是你存储你的图像并调用下面的帮助来获得正确的图像ID。
public static int getImageId(Context context, String imageName) {
return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
}
答案 2 :(得分:1)
此实例中的解决方案是添加用于获取和设置ListView
对象的各个字段的方法。例如,由于您的行项中有TextView
和ImageView
,因此我们假设您的行对象如下所示:
public class ListObject
{
public ListObject (String textView, String imageView)
{
super();
this.textView = textView;
this.imageView = imageView;
}
private String textView;
private String imageView;
public String getTextView ()
{
return textView;
}
public void setTextView (String pTextView)
{
this.textView = pTextView;
}
public String getImageView ()
{
return imageView;
}
public void setImageView (String pImageView)
{
this.imageView = pImageView;
}
}
实施这些功能后,您可以根据ImageView
轻松设置TextView
,如下所示:
// As an example, let us use the first list item
ListObject listObject = (ListObject) parent.getItemAtPosition(0);
if (listObject.getTextView == "Title1")
{
listObject.setImageView("NameOfResource");
}
// so on and so forth