Starting new activity from ListFragment using onListItemClick()

ぃ、小莉子 提交于 2019-12-22 10:12:01

问题


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?


回答1:


Extend a class from listfragment then override the listitemclick function.




回答2:


Just try this,

Intent intent = new Intent(getActivity().getApplicationContext(), YourClassName.class);
startActivity(Intent intent);

Where "YourClassName" is the type of the class you wish to call into.




回答3:


Intent i = new Intent((context),class) 

where class is a recognized type that works with intent. Specifically this is Activity, Service, etc. Also context should be coming from your application not from the encapsulated listview, which you are achieving through using this. Use getApplicationContext() or grab it from your application class, you need valid context.

If PlaceView is not a recognized type then you cannot do what you're trying to do.




回答4:


you can easily get context by using using this "getActivity().getApplicationContext()"



来源:https://stackoverflow.com/questions/15514831/starting-new-activity-from-listfragment-using-onlistitemclick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!