android– 使用onListItemClick()从ListFragment启动新活动

android– 使用onListItemClick()从ListFragment启动新活动,第1张

概述现在我有一个应用程序搜索并返回ListFragment中的项目.我希望能够使每个ListFragment可单击,以便在单击它时,新活动开始使用以下内容:publicvoidonListItemClick(ListViewlistView,Viewview,intposition,longid){Intenti=newIntent(this,PlaceView.class);

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

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的类如下:

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列表?

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

3)onListItemClick(),onItemClickListener()和其他任何适用的应该在哪里?

谢谢你的帮助.

编辑:

按照Pramod的建议,我创建了一个类:ListFragmentClickable扩展了ListFragment,并使用以下内容填充它:

@OverrIDepublic voID onListItemClick(ListVIEw ListVIEw, VIEw vIEw, int position, long ID) {    Intent i = new Intent(this, PlaceVIEw.class);    startActivity(i);}

Eclipse告诉我新的Intent(这个,PlaceVIEw.class)是不允许的,我只能说Intent i = new Intent();.错误是“构造函数Intent(ListFragmentClickable,Class)未定义.”我试着像Eclipse建议的那样实例化Intent,然后添加了i.setClass(这个,PlaceVIEw.class)和i.setClassname(这个,PlaceVIEw.class),但它没有工作,因为我得到了同样的错误.

1)如何解决此错误并按计划覆盖该方法?

2)如果我不能这样做,并且必须使用Intent i = new Intent();,我如何告诉我们我们甚至瞄准的是什么类?

解决方法:

从Listfragment扩展一个类,然后覆盖Listitemclick函数.

总结

以上是内存溢出为你收集整理的android – 使用onListItemClick()从ListFragment启动新活动全部内容,希望文章能够帮你解决android – 使用onListItemClick()从ListFragment启动新活动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1115558.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存