2014-12-02 3 views
0

Layout:Android пользовательский список SimpleAdapter - выбрал значок строки

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="@android:color/black"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/icon"/> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView android:id="@+id/line_a" 
      android:textSize="14dp" 
      android:textColor="@android:color/white" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold"/> 

     <TextView android:id="@+id/line_b" 
      android:textSize="10dp" 
      android:textColor="@android:color/darker_gray" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 

    </LinearLayout> 

</LinearLayout> 

Java:

SimpleAdapter sa; 
    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); 
    HashMap<String,String> item; 

    for(int i = 0; i < j; i++){ 
     item = new HashMap<String,String>(); 
     item.put("line1", ListData[i][0]); 
     item.put("line2", ListData[i][1]); 
     item.put("type", ListData[i][2]); 
     list.add(item); 
    } 

    sa = new SimpleAdapter(this, list, 
      R.layout.two_lines_list, 
      new String[] { "line1","line2" }, 
      new int[] {R.id.line_a, R.id.line_b}); 
         // can you add the setImageView here? If yes, how? 
    setListAdapter(sa); 
} 

Как я могу сделать значок, чтобы получить изображение из Рез/drawable ... в зависимости от типа (ListData [i] [2], вставленного в HashMap)? например. если тип «1» установлен icon1.png в начале строки 2 строки

ответ

0

Вы должны переопределить метод SimpleAdapter.getView(int position, View convertView, ViewGroup parent) и получить элемент списка с текущей позицией (первый параметр в этом методе), затем проверьте установленное значение установленного значка.

ПРИМЕЧАНИЕ: вам лучше использовать ViewHolder образец в getView метод для работы.

 Смежные вопросы

  • Нет связанных вопросов^_^