• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Extracting string from listviews in adroidstudio

I have 2 listviews in 1 activity (1 on the left 1 on the right of screen). I want 2 strings extracted from both list views and use them else where in the code (either in the same activity but different class or different activity). I have tried assigning clicked items to a public variable and then posting them on a text view but I see nothing. Please help or suggest a better way. Here is a sample of my code:

public class Tabs extends Activity {

String CF =""; //Convert To

String CT =""; //Convert From

populateListView(); //function that populates my listview (not shown here)


ListView listMassFrom = (ListView) findViewById(R.id.ListViewMassFrom);

ListView listMassTo = (ListView) findViewById(R.id.ListViewMassTo);


listMassFrom.setOnItemClickListener(new AdapterView.OnItemClickListener(){

@override

public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {


CF = String.valueOf(parent.getItemAtPosition(position));

}

});




listMassTo.setOnItemClickListener(new AdapterView.OnItemClickListener(){


@override

public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {



CT = String.valueOf(parent.getItemAtPosition(position));

}

});



//Test to see if the two string were extracted from the onItemClick method:

TextView t1 = (TextView) findViewById(R.id.test1);

t1.setText(CT);


TextView t2 = (TextView) findViewById(R.id.test2);

t2.setText(CF);

}

}
 
Just FYI, we have a code thing that you could use. For example, the code you posted would look like this:

Code:
public class Tabs extends Activity {

String CF =""; //Convert To

String CT =""; //Convert From

populateListView(); //function that populates my listview (not shown here)


ListView listMassFrom = (ListView) findViewById(R.id.ListViewMassFrom);

ListView listMassTo = (ListView) findViewById(R.id.ListViewMassTo);


listMassFrom.setOnItemClickListener(new AdapterView.OnItemClickListener(){

  @override

  public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {


  CF = String.valueOf(parent.getItemAtPosition(position));

  }

  });




listMassTo.setOnItemClickListener(new AdapterView.OnItemClickListener(){


  @override

  public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {



  CT = String.valueOf(parent.getItemAtPosition(position));

  }

  });



//Test to see if the two string were extracted from the onItemClick method:

  TextView t1 = (TextView) findViewById(R.id.test1);

  t1.setText(CT);


  TextView t2 = (TextView) findViewById(R.id.test2);

  t2.setText(CF);

  }

}
 
Back
Top Bottom