2014-01-16 57 views
2

我想添加圖標到我的ActionBar(v7)。我創建了一個自定義ArrayAdapter自定義ArrayList適配器 - 顯示在ActionBar列表中的圖標

我想在列表項旁邊顯示相應的圖標。但是,我得到在種選擇正確的圖標,而不是在列表本身

Menu non-functioning

public class ObjectsArrayAdapter extends ArrayAdapter<String> { 


private final Context context; 
private final String[] values; 


public ObjectsArrayAdapter(Context context, String[] values) { 
    super(context, R.layout.objects_type_list, R.id.label, values); 

    this.context = context; 
    this.values = values; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View rowView = inflater.inflate(R.layout.objects_type_list, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.label); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); 
    textView.setText(values[position]); 

    // Change icon based on name 
    String s = values[position]; 

    if (s.equals("Wall")) { 
     imageView.setImageResource(R.drawable.ic_action_email); 
     Log.d("IMG", "Found Wall"); 
    } else if (s.equals("Door")) { 
     imageView.setImageResource(R.drawable.ic_drawer); 
     Log.d("IMG", "Found Door"); 
    } else if (s.equals("Stairs")) { 
     Log.d("IMG", "Found Stairs"); 
     imageView.setImageResource(R.drawable.ic_action_email); 
    } else { 
     Log.d("IMG", "Default"); 
     imageView.setImageResource(R.drawable.ic_action_email); 
    } 

    return rowView; 
} 

而且列表佈局如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="5dp" > 

<ImageView 
    android:id="@+id/logo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
> 
</TextView> 

</LinearLayout> 

如何設置實際的列表項包含正確的圖標?

更新:

必須有一些做的事實,getView不叫的OnCreate?

actionBar.setListNavigationCallbacks(
      new ObjectsArrayAdapter(actionBar.getThemedContext(), 
                     Ipsum.Types), this); 

回答

0

創建array.xml文件並添加您的圖像,就像這樣:

<string-array name="random_imgs"> 
    <item>@drawable/car_01</item> 
    <item>@drawable/balloon_random_02</item> 
    <item>@drawable/dog_03</item> 
</string-array> 

然後在y中添加此代碼我們的活動:

TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs); 
//get resourceid by index 
imgs.getResourceId(i, -1) 
// or set you ImageView's resource to the id 
mImgView1.setImageResource(imgs.getResourceId(i, -1)); 
</resources> 

Source

相關問題