I have a class with a ListView. Once the user makes a selection I want to pass that selection to the button on the previous class. This is what I have so far:
public class SetupNewCourse extends Activity {
String[] tees;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_new_course);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(SetupNewCourse.this, selectTee.class);
startActivity(i);
// following code is the hang up I know its in the wrong place
selectTee buttonText = new selectTee();
buttonText.returnTeeSelection();
b.setText((CharSequence) buttonText);
};
});
}
}
Following is my ListView class
public class selectTee extends ListActivity {
String[] tees_list;
String selectedText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tees_list = getResources().getStringArray(R.array.tees_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view,
tees_list));
final ListView teelist = getListView();
teelist.setChoiceMode(1);
teelist.setTextFilterEnabled(false);
teelist.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> teeAdapter, View arg1, int selectedInt, long selectedLong) {
selectedText = (String) (teelist.getItemAtPosition(selectedInt));
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
System.out.println(selectedText);
finish();
}
});
}
public String returnTeeSelection() {
return selectedText;
}
}
Any help would be appreciated.
Thanks in advance.
Use
startActivityForResult()insteadstartActivity().There is explanation of using this method in the documentation of the
Activityclass.