2014-09-30 13 views
0

我是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

回答

0

只需繪製一個整數數組的可繪製圖像{R.drawable.image1,R.drawable.image2}; 並根據數據來從array中取出相應的整數值。你必須這樣映射。

0

基本上,爲了將圖片包含到列表視圖中,您需要有一個自定義適配器。

爲了做到這一點,創建可擴展適配器類(或在此實例中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" /> 

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

}推薦

我真的在首位以爲這將是很容易:) 再次感謝