基于列表视图中的条件着色行

时间:2011-08-23 16:21:57

标签: android

我已经使用setAdapter填充了列表视图,如下所示

cursor = manifest_helper.GetDeliveriesAgainstManifest(pkManifest);





    cursor.moveToFirst();

    for (int i = 0; i < cursor.getCount(); i++) {

        map = new HashMap<String, String>();
        map.put("Deliv_Address",
                cursor.getString(cursor.getColumnIndex("Address")));
        map.put("Deliv_Time",
                cursor.getString(cursor.getColumnIndex("Time")));
        map.put("Deliv_Order",
                cursor.getString(cursor.getColumnIndex("DeliveryOrder")));
        map.put("Deliv_IsCustomerPickup",
                cursor.getString(cursor.getColumnIndex("IsCustomerPickup")));
        map.put("Deliv_FKDeliveryStatus",
                cursor.getString(cursor.getColumnIndex("FKDeliveryStatus")));
        map.put("Deliv_PKDelivery",
                cursor.getString(cursor.getColumnIndex("PKDelivery")));
        alist.add(map);
        cursor.moveToNext();


    }
    sd = new SimpleAdapter(this, alist, R.layout.deliveries_list_row,
            new String[] { "Deliv_Address", "Deliv_Time",
                    "Deliv_Order", "Deliv_IsCustomerPickup",
                    "Deliv_FKDeliveryStatus", "Deliv_PKDelivery" },
            new int[] { R.id.tv_Deliv_Address, R.id.tv_Deliv_Time,
                    R.id.tv_Deliv_Order,
                    R.id.tv_Deliv_IsCustomerPickup,
                    R.id.tv_Deliv_FKDeliveryStatus,
                    R.id.tv_Deliv_PKDelivery });
    //selectedAdapter = new SelectedAdapter(this, 0, alist);
    //selectedAdapter.setNotifyOnChange(true);

    ListDeliveries.setAdapter(sd);

问题是我有一个字段“IsCustomerPickup”,其值为C或O. 我希望通过不同的颜色区分每一行....任何人都会帮助我......请...

3 个答案:

答案 0 :(得分:0)

您需要将SimpleAdapter扩展到您自己的CustomAdapter类中,该类能够读取isCustomerPickup值并相应地设置rowView背景。

答案 1 :(得分:0)

您可以使用listview的自定义适配器来实现此目的。有关如何在网络上执行此操作的无尽教程。例如:Android Series: Custom ListView items and adapters

答案 2 :(得分:0)

我不认为这适用于Simpel适配器。 我认为你必须编写自定义适配器并制作自己的listitem布局。 作为listitem的根源采取线性布局。 使您自己的适配器覆盖getView()方法。

以下是一些代码段: 在布局文件中创建一个列表视图。 然后做一个 custom_listview_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_root_listview_row"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/tv_listview_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="your row text" />
</LinearLayout>

使用listview编写活动,并将自己的适配器设置为listview。您的适配器可能如下所示:

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MyListViewAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater = null;
    private Activity mActivity;

    public MyListViewAdapter(Activity activity){
        mActivity = activity;
        mLayoutInflater = (LayoutInflater) mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO OVERRIDE METHOD WITH YOUR DATABASE
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO OVERRIDE METHOD WITH YOUR DATABASE
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO OVERRIDE METHOD WITH YOUR DATABASE
        return 0;
    }

    public static class ViewHolder{
        public TextView rowTextView;
        public LinearLayout rowLinearLayout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;
        //If the view was never instantiated 
        if(convertView == null){
            vi = mLayoutInflater.inflate(R.layout.custom_listview_row.xml, null);
            holder.rowTextView = (TextView) vi.findViewById(R.id.tv_listview_row);
            holder.rowLinearLayout = (LinearLayout) vi.findViewById(R.id.ll_root_listview_row);
            vi.setTag(holder);
        }
        else {
            holder=(ViewHolder) vi.getTag();
        }

        /*
         * Here is your code to check which color the row should have
         * you can call it like 
         * if( /*row at position should be blue*) {
         * 
         *      holder.rowLinearLayout.setBackgroundColor(R.color.blue);
         * }
         * 
         */


        return vi;
    }

}

我没有测试过这款适配器,但我认为你可以使用它并根据你的需要进行调整。

希望这有帮助。