为每行设置不同的图标

时间:2014-09-05 02:58:07

标签: android listview

main screen

以上是我的应用程序的主屏幕,其中包含listview的自定义适配器。如何为列表视图中的每一行添加不同的图标?我尝试在{xml}中添加ImageView,但它会覆盖整个屏幕,只显示一个图标。

MainAcitivty xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/hp_color"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/cityText"
    style="?android:attr/textAppearanceMedium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:textSize="28sp" />

<ImageView
    android:id="@+id/condIcon"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/cityText"
    android:contentDescription="weather icon" />

<TextView
    android:id="@+id/condDescr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/condIcon"
    android:textSize="20sp" />

<ListView
    android:id="@+id/mainListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

<ImageButton
    android:id="@+id/btnPicturePage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="picturePage"
    android:src="@drawable/photo" />

自定义适配器:

public class WeatherAdapter extends ArrayAdapter<String> {
private final Activity context;
private ArrayList<String> weatherData;

static class ViewHolder {
    public TextView text;
    public ImageView image;
}

public WeatherAdapter(Activity context, ArrayList<String> weatherData) {
    super(context, R.layout.list, weatherData);
    this.context = context;
    this.weatherData = weatherData;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    // reuse views
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.list, null);
        // configure view holder
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);      

        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    ArrayList<String> s = new ArrayList<String>(weatherData);
    String []sa = new String [s.size()];
    sa = s.toArray(sa);

    holder.text.setText(sa[position]);

    if (sa[position].startsWith("Air pressure")) {
        rowView.setBackgroundResource(R.color.skyblue) ;

    }   

    return rowView;
}

列出xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="@color/white" >

</TextView>

2 个答案:

答案 0 :(得分:1)

你的list.xml应该与此类似(取自我的一个文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:gravity="center_vertical" android:orientation="horizontal" 
    android:background="@drawable/xml_pressed_bg" android:clickable="true" android:layout_width="fill_parent" 
    android:layout_height="54dip"
  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ttshow="http://schemas.android.com/apk/res-auto">
    <ImageView android:id="@+id/imgThumb" android:layout_width="42.0dip" android:layout_height="42.0dip" android:scaleType="fitXY"  
    android:src="@drawable/sample_dj" />
    <TextView android:textSize="14.0sp" android:textColor="#ff666666" android:id="@+id/txtName"
    android:layout_width="180dip" android:layout_height="wrap_content" 
    android:singleLine="true" android:shadowColor="#99ffffff" style="@style/TextShadowLight" 
    android:text="Hello" android:paddingLeft="10dip"/>        
</LinearLayout>

每个行项目都有一个图像(imgThumb)。这需要根据您的需要进行更改或设置为静态图像。这是在适配器的getView()方法中完成的。

imgThumb.setImageResource(R.drawable.my_image);

答案 1 :(得分:0)

Private TypedArray img;

img=getResource.obtainTypedArray(R.array.imgIcon)

  if (sa[position].startsWith("Air pressure")) {
    rowView.setBackgroundResource(R.color.skyblue) ;

   holder.image.setImageResource(img.gerResourceId[0]);


}else if (sa[position].startsWith("Air Condition")) {

   holder.image.setImageResource(img.gerResourceId[1]);


}