如何在Android中的arrayList中的文本旁边添加图像?

时间:2016-01-02 10:28:47

标签: java android xml android-studio

 private void initDrawerList() {
    // initialize drawer content
    // need to determine selected item according to the currently selected sensor type
    drawerItemList = new ArrayList();

    drawerItemList.add(new DrawerItem("*1st text", R.drawable.my_sensz_normal, R.drawable.my_sensz_selected, true));
    drawerItemList.add(new DrawerItem("*2nd text", R.drawable.friends_normal, R.drawable.friends_selected, false));
    drawerItemList.add(new DrawerItem("*3rd text", R.drawable.friends_normal, R.drawable.friends_selected, false));

    drawerAdapter = new DrawerAdapter(HomeActivity.this, drawerItemList);
    drawerListView = (ListView) findViewById(R.id.drawer);

    if (drawerListView != null)
        drawerListView.setAdapter(drawerAdapter);

    drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

我想用图像替换*,但我无法知道如何做到这一点。有人可以帮忙吗?谢谢! :)

1 个答案:

答案 0 :(得分:1)

首先,为自定义行

创建布局文件navigation_row.xml
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="55dp"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="10dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Icon"
        android:src="@drawable/order_64" />

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Example application"
        android:textColor="@color/whiteColor"
        android:paddingLeft="10dp"
        android:textSize="20sp" />

</LinearLayout>

在您的活动课程中 制作两个清单

int [] icon = {R.drawable.img1,
                   R.drawable.img2,
                   R.drawable.img3,
                   R.drawable.img4,
                   R.drawable.img5};

    String [] drawerTitle ={"1st text",
            "2 text",
            "3 text",
            "4 text",                               
            "5 text"};

创建一个Adapter类

public class NavigationAdapter extends ArrayAdapter <String> {
private final Context context;
private final String[] titles;
private final int[] icon;

public NavigationAdapter(Context context, int[] icon, String[] titles){
    super(context, R.layout.navigation_row,titles);
    this.context=context;
    this.icon=icon;
    this.titles=titles;
}

@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.navigation_row,parent,false);

    ImageView imageView=(ImageView)rowView.findViewById(R.id.icon);
    TextView textView = (TextView)rowView.findViewById(R.id.name);

    imageView.setImageResource(icon[position]);
    textView.setText(titles[position]);     

    return rowView;
}   

}

现在将此适配器对象设置为listview

 NavigationAdapter onAdapter=new NavigationAdapter(Navigation.this,icon,drawerTitle);
drawerListView.setAdapter(onAdapter);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());