如何从Android中的Fragment调用ArrayAdapter构造函数 [英] how to call ArrayAdapter constructer from Fragment in Android

查看:1321
本文介绍了如何从Android中的Fragment调用ArrayAdapter构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两列ListView添加到我的Android应用程序中。当我创建项目时,我选择了Fragment选项,它通过向左和向右滑动创建了这个漂亮的导航。所以我的 MainActivity 扩展 FragmentActivity

I am trying to add a two-column ListView to my android app. When I created the project, I selected the option Fragment which creates that beautiful navigation by sliding to left and right. So my MainActivity extends FragmentActivity.

我的问题是当我试图将AddayAdapter添加到ListView时,ArrayAdapter的construstor会查找一个上下文对象,我不知道要传递它。

my problem is when I am trying to add AddayAdapter to ListView, the construstor of ArrayAdapter looks for a context object and I dont know to pass that.

这里是我得到错误的代码

here is the code where I get error

StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.row , list);
listview.setAdapter(adapter);

当我传递this时,它会传递FragmentActivity,因为活动会扩展FragmentActivity。但是构造函数需要一个Context对象。

When I pass "this", it passes FragmentActivity because the activity extends FragmentActivity. but constructor need a "Context" object.

它给出了这个错误信息

"The constructor MainActivity.StableArrayAdapter(MainActivity.DummySectionFragment, int, ArrayList<String>) is undefined"

如何传递Context对象?

How can I pass Context object?

推荐答案

您正在片段中设置适配器。使用

You are setting the adapter inside a fragment. Use

StableArrayAdapter adapter = new StableArrayAdapter(this.getActivity(), R.layout.row , list);

这篇关于如何从Android中的Fragment调用ArrayAdapter构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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