使用onListItemClick()从ListFragment启动新活动 [英] Starting new activity from ListFragment using onListItemClick()

查看:106
本文介绍了使用onListItemClick()从ListFragment启动新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个可以搜索并返回ListFragment中项目的应用程序.我希望能够使每个ListFragment都可单击,以便在单击它时,新的活动开始使用如下形式:

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...
}

启动需要单击的ListFragment的类如下:

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);
    }

所以我的问题是:

1)如何设置onListItemClick()以使其与正在使用的ListFragment list一起使用?

1) How do I set up the onListItemClick() to work with the ListFragment list that I am working with?

2)ListView没有任何与onClick等相关的XML属性.没关系吗?

2) ListView doesn't have any XML attributes that relate to onClick or the like. Does that not matter?

3)onListItemClick()onItemClickListener()和其他适用内容应该在哪里?

3) Where should the onListItemClick(), onItemClickListener() and anything else applicable be?

感谢您的帮助.

按照Pramod的建议,我做了一个课:ListFragmentClickable extends ListFragment,并用以下内容填充了它:

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告诉我不允许使用new Intent(this, PlaceView.class),而我只能说Intent i = new Intent();.错误是未定义构造函数Intent(ListFragmentClickable,Class)."我尝试按照Eclipse的建议实例化Intent,然后添加了i.setClass(this, PlaceView.class)i.setClassName(this, PlaceView.class),但是由于出现相同的错误,因此无法正常工作.

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)如何解决此错误并按计划覆盖方法?

1) How do I get around this error and override the method as planned?

2)如果我不能这样做,而必须使用Intent i = new Intent();,我如何告诉意图我们甚至要针对的是哪一类?

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?

推荐答案

从listfragment扩展一个类,然后覆盖listitemclick函数.

Extend a class from listfragment then override the listitemclick function.

这篇关于使用onListItemClick()从ListFragment启动新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆