简体   繁体   English

如何根据上一个活动中的操作填充ListView?

[英]How to populate a ListView based on the action in previous activity?

Currently there are three buttons in my main activity. 目前,我的主要活动中有三个按钮。 Each button opens a new activity with a ListView containing different photos. 每个按钮将打开一个新的活动,其中包含包含不同照片的ListView Each ListView has a different size. 每个ListView都有不同的大小。

I've created a schema of my question: 我已经创建了一个问题的模式: 图式

My question is: How to create only one activity that contains a ListView and populate it with different photos depending on which button is clicked? 我的问题是:如何仅创建一个包含ListView活动,并根据单击哪个按钮用不同的照片填充该活动?

I suppose I have to use intent.putExtra to define button number and use this number in if statement to add images to ListView . 我想我必须使用intent.putExtra定义按钮号,并在if语句中使用此数字将图像添加到ListView But I'm not sure in this solution. 但是我不确定这种解决方案。

Create 3 Fragments and on each button press just load required Fragment . 创建3个Fragments然后在每个按钮上按,只需加载所需的Fragment Each of those Fragments should have ListView/RecyclerView in them and use different data sources. 这些Fragments每个Fragments都应具有ListView/RecyclerView ,并使用不同的数据源。 That is it :) This, of course, can be implemented in several ways, but that is how I would approach this. 就是这样:)当然,可以用几种方法来实现,但这就是我要解决的方法。


Good luck :) 祝好运 :)

Why aren't you sure about this solution? 您为什么不确定此解决方案?
If the layout of the activity to be opened is the same in all 3 cases and the only difference is the contents of the list, then it is the simplest and best solution. 如果要打开的活动的布局在所有3种情况下都是相同的,并且唯一的不同是列表的内容,则它是最简单和最佳的解决方案。
Just pass an integer value to the intent and check it in the onCreate() of the activity that is opened. 只需将一个整数值传递给该意图,然后在打开的活动的onCreate()中对其进行检查。 Depending on the value create and set the adapter for the list and you' re done. 根据值创建并设置列表的适配器,然后完成。
This is the most efficient way and less resources consuming. 这是最有效的方法,消耗的资源更少。

Create 1 custom adapter which takes Arraylist of intergers(ie Resource id's) and set those id to imageview in adapter. 创建1个自定义适配器,它使用整数的Arraylist(即资源ID)并将这些ID设置为适配器中的imageview。

in activity onclick of button add the related id's to Arraylist and pass via intent to next activit. 在活动的onclick按钮上,将相关ID添加到Arraylist中,并通过意图传递给下一个激活。 You can pass and get arraylist from intent like below 您可以像下面这样从intent传递并获取arraylist

1) intent.putStringArrayListExtra("array_list", array_list); 1)intent.putStringArrayListExtra(“ array_list”,array_list); 2) intent.getStringArrayListExtra("array_list"); 2)intent.getStringArrayListExtra(“ array_list”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM