How do I make a ListView with components next to the text in each row?

5

Several current Androids systems have menus like the example below:

PleasenotethatanywhereItapmyblueareaunder"Access to my location" it turns the switch on and off and enables or disables the block under "Location Sources" and also does the blue effect in the clicked block.

WithoutknowingverywelltheoptionsthatAndroidoffersmeItriedtodosomethinglikethat,itwouldbethebasisofwhatIreallyintendtodo.Imadethefollowingcode:

activity.xml

<ListViewandroid:id="@+id/lstSegundaTela"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

MinhaActivity.java

lstSegundaTela = (ListView) findViewById(R.id.lstSegundaTela);
List<Map<String, String>> dados = new ArrayList<>();
String[] valores = new String[] {
        "Acesso a minha localização",
        "Satélite de GPS",
        "Local de rede móvel e Wi-Fi"};
String[] descricoes = new String[] {
        "Permitir que os aplicativos que solicitaram sua permissão " +
        "usem seus dados de localização",
        "Permitir que os aplicativos usem o GPS do telefone para " +
        "determinar sua posição",
        "Permitir que os aplicativos usem o serviço de localização " +
        "do Google para determinar seu local mais rapidamente. Dados " +
        "de localização anônimos serão coletados e enviados ao Google"};
for(int i=0; i<valores.length; i++) {
    Map<String, String> linha = new HashMap<>();
    dados.add(linha);
    linha.put("Titulo", valores[i]);
    linha.put("Subtitulo", descricoes[i]);
}
SimpleAdapter adapter = new SimpleAdapter(
        this,
        dados,
        android.R.layout.simple_list_item_2,
        new String[] {"Titulo", "Subtitulo"},
        new int[] {android.R.id.text1, android.R.id.text2});
lstSegundaTela.setAdapter(adapter); 

The result was as follows:

Myquestionsare:

  • HowtoputSwitchandCheckBoxcomponentsintheListViewrowasshownabove?

  • Howwasthe"Location Sources" block created? Or would it be another ListView with a title?

asked by anonymous 16.07.2014 / 15:51

1 answer

6

This is simple, just create an item in XML with a TextView and a Checkbox , and then use that item in ListView :

ItemListlayout:

<LinearLayoutandroid:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

       <CheckBox
            android:id="@+item/radioButton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:checked="false"
            android:gravity="center_vertical|center_horizontal" />

        <TextView
            android:id="@+item/descObs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|left"
            android:textColor="@color/White"
            android:textSize="20dp" 
            android:text="check"
            android:textIsSelectable="true"/>

</LinearLayout>

NOTE: Notice that TextView is also selectable:

android:textIsSelectable="true"

In Your Adapter just call the layout as any other:

public class TeuAdaptador extends ArrayAdapter<TeuObjeto> {

    private LayoutInflater mInflater;

    public TeuAdaptador(
            Context contexto,
            List<TeuObjeto> listObj) {

        super(contexto, R.layout.teu_item_xml, listObj);
        mInflater = LayoutInflater.from(contexto);

    }

    //os teus métodos, getView, etc
    ...
}


PS:

Now I advise you to have the string's all in res/values-pt/strings.xml as resource :

<resources>

    <string name="a_minha_string">Esta é a minha string</string>

and then use context.getString(R.string.a_minha_string) to fetch it.

    
16.07.2014 / 16:07