- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含项目类别的 ListView 。当我按下列表中的类别标题时,它将显示另一个 ListView ,其中包含所选类别中的项目。
我正在使用 ListFragment 执行此操作。我是否必须开始一个新 Activity 并在 Intent 中一起传递类别 ID?
这是我的 ListFragment
类别:
public class CategoryFragment extends ListFragment implements
OnItemClickListener {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// what to do here
}
}
我要在 onItemClick
中添加什么以转到项目列表?我需要使用另一个 ListFragment 还是只使用常规的 ListActivity?
如何从解析的 JSON 中检索类别 ID 并将其传递给列表项?
编辑:我使用 Volley 解析了 JSON。我正在考虑在类别 ID 的布局中创建一个不可见的 TextView,以便我可以从那里提取它。这可能吗?
最佳答案
您应该使用Interface
,这是将值传递给Fragment
的“官方”方式。在此处查看此文档:
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
那么你需要做什么:
fragment A --> Activity --> fragment B
不可见的 TextView
绝对不是正确的解决方案...
编辑更详细的解决方案:
您定义一个接口(interface)(在单独的文件中或在您的 FragmentA 类中):
public interface MyFragmentListener {
public void onFragmentItemSelected(String url);
}
在你的 FragmentA 类中:
MyFragmentListener myListener;
public void setMyFragmentListener(MyFragmentListener listener) {
myListener = listener;
}
// here I used the onClick method but you can call the listener whereever you like.
public void onClick(View view) {
if (myListener != null) {
myListener.onFragmentItemSelected(url);
}
}
声明你的 FragmentB 类:
public class FragmentB extends Fragment implements MyFragmentListener {
public void onFragmentItemSelected(String url) {
// do what you want to do
}
}
在您的 Activity 中:
// you tell your fragmentA that his listener is the FragmentB. And because you implemented the listener in FragmentB, everything is allright;
fragmentA.setMyFragmentListener(fragmentB));
关于java - Android - 将值从 ListFragment 传递到另一个 ListFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21040664/
我有一个包含项目类别的 ListView 。当我按下列表中的类别标题时,它将显示另一个 ListView ,其中包含所选类别中的项目。 我正在使用 ListFragment 执行此操作。我是否必须开始
首先我应该提到我使用 ActionBarSherlock 库来实现向后兼容性。 我有一个添加 ListFragment 的 Activity 当它第一次启动时。我有一个自定义 Loader我实现并遵循
谁能告诉我一个使用 CursorLoader 查询 SQLite 数据库并填充 ListFragment 的简单示例?下面的代码可以编译,但是当我运行它时,LogCat 告诉我“ListFrag”不能
我是 Android 开发新手,我正在尝试创建一个 ListFragment,它可以从 3 个 ArrayList 对象创建一个 ListView,每个对象包含 30 个字符串。每行将包含每个数组中的
我有一个具有两个 fragment (权重 3 和 2)线性布局的 Activity 。第一个 fragment 是一个列表 fragment 。它最初具有指定的权重高度,但是当列表变大时,它会变得更
我试图让我的 ListFragment 显示在我的 View 中,但是我对这段代码遇到了无穷无尽的问题,无法让它显示我的解析器的结果。我相信这很可能是我的 xml 文件的问题,我的代码中没有收到任何错
我有一个像这样的列表 fragment : private ArrayListimages; private View view; @Override public void onCreate(Bun
我已经向 ListFragment 添加了一个选项菜单,我需要在用户单击它时显示一个新的 ListFragment。这有什么问题吗?我对开始 fragment 的正确方法有点困惑.. 最佳答案 将事务
我是 android api 的新手,我在 .. 基础知识方面遇到了一些问题。我试图让列表正常工作,但列表显示了错误的文本: 每一行有两个 TextView(标题,副标题),这里是 xml:
我已经用头撞墙了大约一天了。我希望有人能提供帮助。 我的程序在异步任务中将一些 Json 加载并解析到对象数组中。这个异步任务是从我的 ListFragment 类调用的,数据在 onPostExec
我可以设法制作具有相同图标的列表 fragment 。但似乎我无法用不同的图标制作该列表。因此列表结果对于不同的项目列表将是不同的图标。我试试看。当我尝试构建它时。它没有显示错误。但是当我运行它时。它
我是 Android 开发的新手。我在使用 ListFragment 和自定义适配器时遇到问题。我的列表中没有对象。你能检查我的代码吗?如果我滚动到底部,您会看到一个对象,但随后会崩溃。 ListFr
我使用 Listfragmen,您的适配器在其中输出图像。 我需要在两列中显示图像。 我尝试放入 listView fragment ,但数据未显示。 我需要有两列来显示图像。
我正在尝试将一些数据加载到 ListFragment 中,但出现此错误: 05-07 00:05:30.533 625-625/com.myapp.android E/AndroidRuntime
我检查了有关 ListFragments 异步更新的其他答案,发现 notifyDataSetChanged() 方法不起作用的最常见问题是,开发人员倾向于覆盖初始适配器源,导致适配器丢失引用,因此不
我的应用程序中有两个列表 fragment 和一个 fragment 。我给你看第一个 fragment 。 应用程序启动,Asynctask 检索数据并将它们放入 arrayNews 中。我认为问题
我有一个 ListFragment,其中包含使用自定义 ArrayAdapter 填充的 Earthquake 对象列表。每个元素要么“突出显示”,要么不“突出显示”,具体取决于地震的震级是否高于某个
我是第一次使用 ListFragments。我想显示来自 sqlite 数据库的数据。 我的问题:我无法检索上下文。 我在扩展 ListFragment 的类中尝试了 this.getActivity
我有一个带有 ArrayAdapter 的 ListFragment 来显示对象列表。我想刷新 ListFragment 的内容。这些项目本身稍微改变了它们的文本,也改变了它们的顺序。我有一百多个项目
我有一个列表 fragment 显示我的自定义列表项,它由包含图像和文本等的相对布局组成。
我是一名优秀的程序员,十分优秀!