How to select a particular java listview line

Asked

Viewed 926 times

0

How do I get the line id of a listview? The idea is this: before picking up the selected line, I’ll work with all the lines I’m trying.

aList = new ArrayList<HashMap<String,String>>();        

for(int i=0;i<countries.length;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
    hm.put("txt", getString(countries[i]));

    aList.add(hm);        
}

String[] from = { "txt" };


int[] to = { R.id.txt};        


SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);       

        setListAdapter(adapter);

...

OnItemClickListener listener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {



                for(int i=0;i< arg0.getCount();i++){

                não sei fazer o setText(); em todos os 'R.id.txt'...    

            }

If anyone has an idea how to do I appreciate the help.

1 answer

2


First check your event logic, you are using an event to select an item from the list, did not understand why change the rest of the items.

Before changing the list item I need to know what type of items it has, or you created a simple list with String array, or else you may have loaded it with objects( and customized the Listview items).

EXAMPLE WITH STRINGS:

Declare a global string arraylist and Arrayadapter in the class

ArrayList<String> arrayItens;
ArrayAdapter<String> adapter;

//chame esse método no inicio da atividade, para carregar a lista.
private void carregaLista(){
        arrayItens = new ArrayList<String>();
        arrayItens.add("um");
        arrayItens.add("dois");
        arrayItens.add("tres");
        //Cria um adapter e o referencia com o arrayItens
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayItens);
        //seta sua ListView com o adapter que está contendo seu array
        lvLista.setAdapter(adapter);
        //uma vez setado o adapter do seu ListView, 
        //você não precisa mais trabalhar diretamente com o ListView para manipular seus dados, 
        //você trabalha diretamente na fonte de dados que seria o arrayItens e o adapter. 
    }

In the Onitemclicklistener event change the following lines.

@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
        //modifica a string na posicao exata do arrayList que esta referenciado pelo adapter
        arrayItens.set(position, "esse numero foi selecionado"); //esse texto vai aparecer na tela no lugar do numero.
        //com o arrayList modificado o adapter manda atualizar todas suas referencias com algum componente gráfico que no caso é o ListView.
        adapter.notifyDataSetChanged(); //atualiza o ListView
}

If you need to make the modification on all items, you can make a for in the array. and after the for don’t forget to call the notifyDataSetChanged method (call this method out of the for loop) to update the listview on the screen.

  • Thank you for your reply @Juarez. I forgot to mention that I am using a layout in the listview that has a Textview with id 'txt' and it is in it that I want to set the text.

  • I assume you’ve created an Itemlistview class and an Adapter class that extends from Basedapter, is that it? but still it will be almost the same logic of this example that I did

  • if yes, enter the code of those classes in the question.

  • Okay! Reworked question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.