I have an XML
with two ListView
, one with a list of clients filled by a select query (lv_cli
) and the other with the details of the client selected (lv_cli_det
). I would like to keep the client selected in the lv_cli
while the lv_cli_det
show the details.
XML:
<ListView android:id="@+id/cli_lista" android:layout_width="512dp" android:layout_height="wrap_content" android:fadeScrollbars="false" > </ListView> <ListView android:id="@+id/cli_lista_det" android:layout_width="512dp" android:layout_height="wrap_content" android:layout_toRightOf="@+id/cli_lista" android:fadeScrollbars="false" > </ListView>
Java:
Cursor cursor = db.rawQuery("Select NrCl||';'||Nome From Clientes", null); final ListView t = (ListView)findViewById(R.id.cli_lista); ArrayAdapter<String> myarrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listItems); t.setAdapter(myarrayAdapter); final ListView td = (ListView)findViewById(R.id.cli_lista_detalhe); final ArrayAdapter<String> myarrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listItems2); t.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String item = ((TextView)view).getText().toString(); String[] strArray = item.split("\\;"); cli.load(strArray[0].toString()); td.setAdapter(myarrayAdapter2); listItems2.clear(); listItems2.add("Nome: " + cli.getNome()); listItems2.add("Morada: " + cli.getMorada()); listItems2.add("Localidade: " + cli.getLoca()); listItems2.add("Código Postal: " + cli.getCp()); listItems2.add("Pais: " + cli.getPais()); listItems2.add("Nif: " + cli.getNif()); listItems2.add("Tel: " + cli.getTel()); listItems2.add("Tlm: " + cli.getTlm()); listItems2.add("Tipo Preço: " + cli.getTipoPvn()); listItems2.add("Cond. Pagamento: " + cli.getCpg()); listItems2.add("Obs: " + cli.getObs()); td.setAdapter(myarrayAdapter2); myarrayAdapter2.notifyDataSetChanged(); } });
I found the proper way. It's very simple. In resource describe following:
android:choiceMode="singleChoice" android:listSelector="#666666"
(or you may specify a resource link instead of color value)
Programmatical:
listView.setSelector(Drawable selector); listView.setSelector(int resourceId); listView.setChoiceMode(int mode);
mode can be one of these: AbsListView.CHOICE_MODE_SINGLE, AbsListView.CHOICE_MODE_MULTIPLE, AbsListView.CHOICE_MODE_NONE (default)
(AbsListView is the abstract ancestor for the ListView class)
P.S. manipulations with onItemClick and changing view background are bankrupt, because a view itself is a temporary object. Hence you must not to track a view.
If our list is long enough, the views associated with scrolled out items will be removed from hierarchy, and will be recreated when those items will shown again (with cached display options, such as background). So, the view we have tracked is now not an actual view of the item, and changing its background does nothing to the actual item view. As a result we have multiple items selected.
To hold the color of listview item when you press it, include the following line in your listview item layout:
android:background="@drawable/bg_key"
Then define bg_key.xml
in drawable
folder like this:
<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@color/pressed_color"/> <item android:drawable="@color/default_color" /> </selector>
Finally, include this in your ListView
onClickListener
:
listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) { view.setSelected(true); ... // Anything } });
This way, only one item will be color-selected at any time. You can define your color values in res/values/colors.xml
with something like this:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="pressed_color">#4d90fe</color> <color name="default_color">#ffffff</color> </resources>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With