依赖于上下文的图像的列表视图

问题描述:

我是android开发新手,目前有一个listview体验。我的所有项目都显示正确,但现在我想从我的可绘制文件夹中添加一个图像,具体取决于内容。依赖于上下文的图像的列表视图

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,我希望它将png图像type1从可绘制文件夹(R.drawable)中取出。 有没有人如此善良,请给我一个提示?

+0

尽量让你自定义适配器。 – 2014-09-30 10:19:01

试试这个教程:

http://webdeveloperpadawan.blogspot.co.uk/2014/09/android-listview-with-differing-rows.html

总之,内部getView(),您需要检查的类型和设置基础上的图像。

只需绘制一个整数数组的可绘制图像{R.drawable.image1,R.drawable.image2}; 并根据数据来从array中取出相应的整数值。你必须这样映射。

基本上,为了将图片包含到列表视图中,您需要有一个自定义适配器。

为了做到这一点,创建可扩展适配器类(或在此实例中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" /> 

感谢大家对他们的答案。 最后我能得到处理了它与一个新的类

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; 
} 

}推荐

我真的在首位以为这将是很容易:) 再次感谢