Getting values from listView in Android to textView or another Listview same activity

@ I'm Trying to populate a textView or Listview based on the selection from another list view. So far I have an array of Strings in The first ListView and when i click on an item in it, it displays the item i have selected in a textView. but each time i click another item it overides the last selection. I would like to display each item that is clicked in a textView or Listview. Hope that makes sense and any help is appreciated.

                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {



                    String Exercise = String.valueOf(adapterView.getItemAtPosition(i));
                    TextView textView3 = (TextView) findViewById(R.id.textView3);
                    textView3.setText(Exercise);

                }

Answers


You are overriding the value set in the TextView. You need to append the values the way you want. If you want it to be separated by new line you can do this way -

Initialize your TextView only once -

private String result;
    TextView textView3 = (TextView) findViewById(R.id.textView3);

And change your onItemClick() -

 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)     { 

                    String Exercise = String.valueOf(adapterView.getItemAtPosition(i));
                   result = (textView3.getText().toString() + "\n" + Exercise;
                    textView3.setText(result);

                }

The text is ment to be appended in your string, then you set it to your textView and the string and textView are ment to me instantiated only onece in your onCreate method

TextView tv = (TextView) findViewById(R.id.textView);
String string ="";

    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


string    += String.valueOf(adapterView.getItemAtPosition(i));
 tv.setText(textView3.getText().toString() + "\n"); }

I think textView.append() is the method you need.


Need Your Help

ASP.Net 5 project referencing old class library

reference asp.net-core repository-pattern asp.net-core-mvc class-library

I was trying repository pattern with ASP.Net 5 project and service project. I referenced my ".Service" class library project into my ".Web" project but something went wrong with the reference. I re...

Create VS2013 item templates for multiple files in multiple folders

visual-studio-2013 customization

I started this as an item template, but if that's not the appropriate solution, please let me know. I'm trying to automate the creation of several files (and possible several folders) when a new wi...