Note that there are some explanatory texts on larger screens.

plurals
  1. POHighlight and keep highlighted the selected item in Listview
    primarykey
    data
    text
    <p>I have a listview that populates the data from the cursor. The listview has a customadapter. I want to highlight the selected item in the listview</p> <pre><code>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&lt;?&gt; 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; } } } </code></pre> <p>The layout I use is... this is the main layout. </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;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" /&gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;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" /&gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;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" &gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>The other layout contains 4 textboxes which is used for the Listview. </p> <p>Now I want to highlight the pressed item in the Listview. How can I achieve that. I tried using the selector but no effect. I want that the item is highlighted till next item is selected . </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload