Listview a Textview on android

2

Developing a mobile app in I have a ListView and I want to get the event in which when I select an Item from the list I show a certain String in a TextView .

For example:

  • Selection in Item A presents in TextView1 the String1
  • Selection in Item B presents in TextView1 the String2
  • For now I can present the data but loading a Activity with a TexView for each Item.

    How can I make it work dynamically for all my Items?

        
    asked by Luis Ayala Ponce 26.06.2016 в 16:54
    source

    2 answers

    3

    Define the listener OnItemClickListener() to your ListView to detect the event when clicking on an item in the list, by selecting item A we can define which is the first and would have the position 0, and item B the second the position 1; according to the selection of the item, you add the desired text in your TextView :

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    
            switch (position) {
                case 0:  //Seleccion item A
                    miTextView1.setText("Texto String1");
                    break;
                case 1: //Seleccion item  B
                    miTextView1.setText("Texto String2");
                    break;
                default:
                    miTextView1.setText("");
            }
    
    
        }
    });
    
        
    answered by 27.06.2016 в 03:45
    0

    You could do it by assigning a new method to the ListView:

    listView.setOnItemClickListener

    With this method you get the object of the row and then using its attributes you can paint the text you want in any component.

    Good luck! : D

        
    answered by 15.07.2016 в 09:26