Android : How to return list item selected to parent activity - android

on Saturday, December 13, 2014


I'm learning android development. And I trying to do the following:


An wellcome activity, with a TextView with the following text: "Please Select"


This Textview, has an OnClick Listener setted.


My intent is, when the user click on this textview, one new activity with a listview must be opened.


This listview cointains some values like: Country 1, Country 2, Country 3 and so on;


So, when the user select one value, this value must be returned to the parent activity.


In my parent activity I have the following:



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

countrySamples = (TextView)findViewById(R.id.countrySamples);
countrySamples.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListCountrySelectedFragment whatKindOjectIsThis = new ListCountrySelectedFragment();
whatKindOjectIsThis.setListCountrySelectedActivityDelegate(new ListCountrySelectedFragment.ListCountrySelectedActivityDelegate() {
@Override
public void selectCountry(String name) {
selectItem(name);
}
});

}
});
}


[...]



public void selectItem(String name) {
int index = valuesArray.indexOf(name);
if (index != -1) {
countryButton.setText(name);
}
}


And I've created an blank fragment with a list.


And added the following code:



public static interface ListCountrySelectedActivityDelegate {
public abstract void selectCountry(String name);
}

private ListCountrySelectedActivityDelegate delegate;


[...]


But, my fragment is never started.. The true is.. I have to create a Fragment to this? Or, must by an activity? Or I'm totally wrong?


Thanks


0 comments:

Post a Comment