突出显示并在Listview中突出显示所选项目

时间:2013-12-05 16:44:37

标签: android listview android-listview

我有一个listview,用于填充游标中的数据。 listview有一个customadapter。我想在列表视图中突出显示所选项目

public class BillOfSaleActivity extends Activity {

    ListView lv1;
    CustomAdapter listAdapter;

    DbHandler dbHandler;
    Cursor c;

    TextView salesman;
    TextView customer;
    TextView TC;

    String userName;
    String custName;

    Float TotalCost = 0f;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bos_main);

        Intent i = getIntent();
        userName = i.getStringExtra("userName");
        custName = i.getStringExtra("custName");

        salesman = (TextView) findViewById(R.id.tv_bosSalesman);
        customer = (TextView) findViewById(R.id.tv_boscust);
        TC = (TextView) findViewById(R.id.textView_bos_total);

        salesman.setText(userName);
        customer.setText(custName);

        dbHandler = new DbHandler(getApplicationContext());
        c = dbHandler.getBosList();

        if (c != null) {
            if (c.moveToFirst())
                do {
                    Float qty = Float.parseFloat(c.getString(4));
                    Float price = Float.parseFloat(c.getString(5)) * qty;
                    TotalCost = TotalCost + price;
                } while (c.moveToNext());
        }

        c.moveToFirst();
        lv1 = (ListView) findViewById(R.id.listView_bos);
        lv1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv1.setSelector(R.drawable.list_selector);

        // String []from = new String []
        // {DbHandler.Bos_List_col5,DbHandler.Bos_List_col6};
        listAdapter = new CustomAdapter(getApplicationContext(), c);
        lv1.setAdapter(listAdapter);

        TC.setText("$" + TotalCost);

        lv1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                    long arg3) {
                view.getFocusables(position);               
                view.setSelected(true);                 
            }
        });
    }

    public class CustomAdapter extends BaseAdapter {
        Cursor cursor;
        Context context;

        public CustomAdapter(Context cont, Cursor c) {

            this.cursor = c;
            this.context = cont;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return c.getCount();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int position, View view, ViewGroup arg2) {

            Log.i("dhiraj", "In getView");

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.activity_bos_list, null);

            cursor.moveToPosition(position);

            TextView textView1 = (TextView) view
                    .findViewById(R.id.textView1_bos);
            TextView textView2 = (TextView) view
                    .findViewById(R.id.textView2_bos);
            TextView textView3 = (TextView) view
                    .findViewById(R.id.textView3_bos);
            TextView textView4 = (TextView) view
                    .findViewById(R.id.textView4_bos);

            textView1.setText(cursor.getString(3));
            textView2.setText(cursor.getString(2));
            textView3.setText(cursor.getString(4) + " x  $"
                    + cursor.getString(5));

            Float qty = Float.parseFloat(cursor.getString(4));
            Float price = Float.parseFloat(cursor.getString(5)) * qty;

            textView4.setText("$ " + price);

            return view;
        }

    }
}

我使用的布局是......这是主要的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:weightSum="2" >

        <TextView
            android:id="@+id/tv_bosSalesman"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="20dp"
            android:text="Salesman"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/tv_boscust"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Customer"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/lightBlue" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_bos_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linz"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="Nota de Venta"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_bos_header"
        android:weightSum="4" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Cant"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Cod"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Nombre"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Total"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>

    <ListView
        android:id="@+id/listView_bos"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/linz12"
        android:layout_below="@+id/lin"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:dividerHeight="1dp"
        android:drawSelectorOnTop="true" />

    <LinearLayout
        android:id="@+id/linz234"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:weightSum="3" >

        <Button
            android:id="@+id/button_bos_eliminar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Eliminar"
            android:textColorHint="@color/red" />

        <Button
            android:id="@+id/button_bos_agregar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Agregar"
            android:textColorHint="@color/green" />

        <Button
            android:id="@+id/button_bos_finalizar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Finalizar"
            android:textColorHint="@color/blue" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linz12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linz234"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView_bos_total"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</RelativeLayout>

另一个布局包含4个用于Listview的文本框。

现在我要突出显示Listview中的按下项目。我怎样才能做到这一点。我尝试使用选择器但没有效果。我想要突出显示该项目,直到选择下一个项目。

4 个答案:

答案 0 :(得分:2)

如果您想要在列表视图中突出显示所有选定的项目,

   **@Override
     public void onItemClick(AdapterView<?> adapterView, View view,
                    int position, long arg3) {
              view.setBackgroundColor(getResources().getColor(
                        R.color.pressed_color)); 
            //change the background color or image of the selected item
         }**

答案 1 :(得分:0)

默认情况下,ListViews没有设置choiceMode,因此默认情况下不会指示当前选择。

要更改此设置,您只需将ListView的choiceMode属性设置为singleChoice即可。 要显示列表中的选定项,还应设置listSelector属性。

示例:

<ListView android:id="@+id/my_list"
        android:choiceMode="singleChoice" 
        android:listSelector="@android:color/darker_gray" />

答案 2 :(得分:0)

继续Rashi的回答,这是一个对我有用的简单方法 - 只需跟踪最后选择的项目,以便在打开当前所选项目之前知道从前一次点击中关闭哪一项。

public class SomeActivity extends Activity {

    View previouslySelectedItem = null;

    ...
    protected void onCreate(Bundle savedInstanceState) {

        ListView myListView = (ListView)findViewById(R.id.some_list);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (previouslySelectedItem != null)
                {
                    previouslySelectedItem.setBackgroundColor(
                            getResources().getColor(R.color.transparent));
                }

                view.setBackgroundColor(
                        getResources().getColor(R.color.orange));

                previouslySelectedItem = view;
            }
        });

答案 3 :(得分:0)

看一下对我有帮助的this answer

在那里,您将找到如何突出显示listView中的项目,并在选择其他项目之前将其保持突出显示。