Home  >  Article  >  Java  >  How to Keep a Selected Item Highlighted in an Android ListView?

How to Keep a Selected Item Highlighted in an Android ListView?

Barbara Streisand
Barbara StreisandOriginal
2024-11-24 00:15:18934browse

How to Keep a Selected Item Highlighted in an Android ListView?

Android ListView keeps selected item highlighted

In this thread, you will see how to keep the selected item in a ListView highlighted, even when the details of the selected item are displayed in another ListView.

First, let's define the XML layout:

<ListView
    android:id="@+id/cli_lista"
    android:layout_width="512dp"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:listSelector="#666666"
    />

<ListView
    android:id="@+id/cli_lista_det"
    android:layout_width="512dp"
    android:layout_height="wrap_content"
    android:fadeScrollbars="false"
    />

Now, let's look at the Java code:

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

In the XML layout, we specify:

  • android:choiceMode="singleChoice": This ensures that only one item can be selected at a time.
  • android:listSelector="#666666": This specifies a background color for the selected item.

In the Java code, we handle the item click event and update the details ListView accordingly.

By following this approach, you can keep the selected item in the first ListView highlighted while displaying the details of the selected item in the second ListView.

The above is the detailed content of How to Keep a Selected Item Highlighted in an Android ListView?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn