Android 根据列表视图中的条件对行进行排序

Android 根据列表视图中的条件对行进行排序,android,Android,我已经使用setAdapter填充了列表视图,如下所示 cursor = manifest_helper.GetDeliveriesAgainstManifest(pkManifest); cursor.moveToFirst(); for (int i = 0; i < cursor.getCount(); i++) { map = new HashMap<String, String>(); map.put("D

我已经使用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);
cursor=manifest\u helper.getDeliveriesAgainsManifest(pkManifest);
cursor.moveToFirst();
对于(int i=0;i
问题是我有一个字段“IsCustomerPick”,它的值为C或O。
我想用不同的颜色来区分每一行…有人能帮我吗…请

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

您可以使用listview的自定义适配器来实现这一点。关于如何在网络上实现这一点,有无数的教程。e、 g:

我认为这不适用于Simpel适配器。 我认为您必须在自定义适配器上编写,并创建自己的listitem布局。 作为列表项的根,采用线性布局。 使您自己的适配器覆盖getView()方法

以下是一些片段: 在布局文件中创建listview。 然后做一个 自定义_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>
我还没有测试过这个适配器,但我认为您可以使用它,并根据需要调整它

希望这有帮助

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;
    }

}