Right now I have an app that searches and returns items in a ListFragment. I want to be able to make each ListFragment clickable, such that when it is clicked, a new activity starts using something like this:
public void onListItemClick(ListView listView, View view, int position, long id) {
Intent i = new Intent(this, PlaceView.class);
startActivity(i);
//eventually data from the List will be passed to the new Activity...
}
The class that starts the ListFragment that I need to be clickable is as follows:
public class ResultsView extends FragmentActivity {
private ArrayAdapter<String> mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results_view);
//Receive searchTerm from MainActivity
Intent intent = getIntent();
String searchTerm = intent.getStringExtra(MainActivity.SEARCH_TERM);
mAdapter = new ArrayAdapter<String>(this, R.layout.item_label_list);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ListFragment list = new ListFragment();
ft.add(R.id.fragment_content, list);
// Let's set our list adapter to a simple ArrayAdapter.
list.setListAdapter(mAdapter);
FactualResponderFragment responder = (FactualResponderFragment) fm.findFragmentByTag("RESTResponder");
if (responder == null) {
responder = new FactualResponderFragment();
ft.add(responder, "RESTResponder");
}
Bundle bundle = new Bundle();
bundle.putString("search_term", searchTerm);
responder.setArguments(bundle);
ft.commit();
}
public ArrayAdapter<String> getArrayAdapter() {
return mAdapter;
}
public void onListItemClick(ListView listView, View view, int position, long id) {
Intent i = new Intent(this, PlaceView.class);
startActivity(i);
}
So my questions are:
1) How do I set up the onListItemClick()
to work with the ListFragment list
that I am working with?
2) ListView
doesn't have any XML attributes that relate to onClick
or the like. Does that not matter?
3) Where should the onListItemClick()
, onItemClickListener()
and anything else applicable be?
Thanks for your help.
EDIT:
Following Pramod's advice, I made a class: ListFragmentClickable extends ListFragment
, and populated it with the following:
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
Intent i = new Intent(this, PlaceView.class);
startActivity(i);
}
Eclipse tells me that new Intent(this, PlaceView.class)
is not allowed, and that I only can say Intent i = new Intent();
. The error is "The constructor Intent(ListFragmentClickable, Class) is undefined." I tried instantiating the Intent as Eclipse suggested, and then added i.setClass(this, PlaceView.class)
and i.setClassName(this, PlaceView.class)
but it didn't work as I got the same error.
1) How do I get around this error and override the method as planned?
2) If I can't do that, and have to use Intent i = new Intent();
, how do I tell the intent what class we're even aiming at?