我是Android开发的新手,目前使用listview。我的所有项目都正确显示,但现在我想根据内容从我的drawable文件夹中添加一个图像。
ListView lv=(ListView)findViewById(R.id.listView_event);
ListAdapter adapter = new SimpleAdapter(
this, eventList,
R.layout.list_item_event, new String[] { "name", "date" },
new int[] {R.id.name, R.id.date});
lv.setAdapter( adapter );
我的eventList包含更多字段,例如" type"它只有三种不同的价值观。现在万一它包含type1我希望它从可绘制文件夹(R.drawable)中取出png图像type1。 有没有人这么善良,请给我一个提示?
答案 0 :(得分:0)
试用本教程:
http://webdeveloperpadawan.blogspot.co.uk/2014/09/android-listview-with-differing-rows.html
简而言之,在getView()内部,你需要检查类型并根据它设置图像。
答案 1 :(得分:0)
只需制作一个可绘制图像的整数数组,如{R.drawable.image1,R.drawable.image2}; 并根据数据来自array.you必须映射这样的整数值。
答案 2 :(得分:0)
基本上,为了将图片包含到列表视图中,您需要有一个自定义适配器。
为此,创建一个扩展Adapter的类(或者在此示例中为ArrayAdapter):
public class ParticipantAdapter extends ArrayAdapter<Participant> {
private final Context context;
private final List<Participant> values;
private final String urlToProfilePics;
public ParticipantAdapter(Context context, int resource,
List<Participant> objects) {
super(context, resource, objects);
this.context = context;
this.values = objects;
}
public ParticipantAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
this.values = new ArrayList<Participant>();
// Obtain the external cache directory
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View participantView;
participantView = inflater.inflate(R.layout.participant_item, parent, false);
TextView textView = (TextView) participantView.findViewById(R.id.participantName);
textView.setText(getItem(position).getName());
ImageView imageView = (ImageView) participantView.findViewById(R.id.participantImage);
//you can add varibles to the class that represents your items and use it like so to determine the image displayed
int someContext = getItem(position).someContextOfTheCurrentItem;
switch(someContext) {
case DOG:
imageView.setImageResource(R.drawable.dog);
break;
case CAT:
imageView.setImageResource(R.drawable.cat);
break;
}
//here are some other things you can do on the image based on the context of your item
if(!getItem(position).someBoolean) {
imageView.setImageAlpha(80);
int lightGrey = context.getResources().getColor(R.color.light_grey);
textView.setTextColor(lightGrey);
} else {
int black = context.getResources().getColor(R.color.black);
textView.setTextColor(black);
}
return participantView;
}
public List<Participant> getValues() {
return this.values;
}
注意getView
方法。它的作用是获取包含图像的布局的XML文件,并根据每个项目的上下文对其进行操作。
在这个例子中,我有一个名为Participant
的单独类,它包含一个整数和一个布尔值,我在getView
方法中进行测试,并根据它们的值我可以更改返回的View
将代表什么。
这是我在这个例子中使用的布局的xml文件(它只是一张图片,右边是文本视图):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/participantImage"
android:layout_width="52dp"
android:layout_height="52dp"
android:contentDescription="@string/contentDescriptionParticipantImage"
android:maxWidth="@dimen/tom_max_width_participant_image"
android:src="@drawable/profile_placeholder" />
<TextView
android:id="@+id/participantName"
android:layout_width="match_parent"
android:layout_height="52dp"
android:padding="@dimen/tom_small_text_padding"
android:text="TextView"
android:textColorHint="@color/black"
android:textSize="@dimen/tom_participant_item_text_size" />
答案 3 :(得分:0)
感谢大家的回答。 最后,我可以按照新课程
的建议进行管理public class EventAdapter extends SimpleAdapter {
private final Activity context;
private final String[] string_event;
private final int[] int_event;
private final ArrayList<HashMap<String, String>> items;
public EventAdapter(Activity context, ArrayList<HashMap<String, String>> items,
String[] string_event, int[] int_event) {
super(context, items, R.layout.list_item_event, string_event, int_event);
this.context = context;
this.string_event = string_event;
this.int_event = int_event;
this.items = items;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_item_event, null, true);
System.out.println("Items position: " + items.get(position));
EventClass currentEvent = new EventClass(items.get(position).get("name").toString(), items.get(position).get("location"), items.get(position).get("date")
, items.get(position).get("type"), items.get(position).get("theme"),items.get(position).get("link"));
TextView txtName = (TextView) rowView.findViewById(int_event[0]);
TextView txtDate = (TextView) rowView.findViewById(int_event[1]);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView_type);
txtName.setText(currentEvent.getEvent_name());
txtDate.setText(android.text.format.DateFormat.format("dd MMMM yyyy",currentEvent.getDate()));
if(currentEvent.getType().equals("triathlon")) {
imageView.setImageResource(R.drawable.triathlon_70);
}
if(currentEvent.getType().equals("run")) {
imageView.setImageResource(R.drawable.ic_run_70);
}
if(currentEvent.getType().equals("cycling")) {
imageView.setImageResource(R.drawable.ic_cycle_70);
}
return rowView;
}
}
我真的假设它首先会更容易:) 再次感谢