android


How to transfer Arraylist from one fragment to another


I'm new to Android and have a question:
I want to tranfer the the fav Arraylist from the FavListFragment to the ApplistFragment.
Can someone tell me how to do this? I tried to tranfser the data with Bundle and putStringArrayList but this didn't work. I hope someone can help me with this, i'm searching for hours now.
public class FavListFragment extends ListFragment {
ArrayList<String> list;
ArrayList<String> fav = new ArrayList<String>();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_dummy, container, false);
Button button = (Button) view.findViewById(R.id.button1);
Button button2 = (Button) view.findViewById(R.id.button2);
for(int i = 0; i < 5; i++)
list.add("item" + i);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapter2);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
});
button2.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
int cntChoice = getListView().getCount();
SparseBooleanArray sparseBooleanArray = getListView()
.getCheckedItemPositions();
for (int i = 0; i < cntChoice; i++) {
if (sparseBooleanArray.get(i)) {
String selected = "" +
getListView().getItemAtPosition(i).toString();
System.out.println(selected);
fav.add(selected);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
});
return view;
}
And her the other fragment
public class AppListFragment extends ListFragment {
ArrayList<String> list;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_dummy, container, false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list );
setListAdapter(adapter);
return view;
}
This ist what I tried:
AppListFragment fr = new AppListFragment();
Bundle value= new Bundle();
value.putStringArrayList("temp1", fav);
fr.setArguments(value);
Bundle bundle=getArguments();
ArrayList <String> myStrings = bundle.getStringArrayList("temp1");
Ok, you can do that through the associated Activity, watch this example:
Comunnicating with other fragments
Regards.

Related Links

android ssl without domain android HttpsURLConnection
Call a method that have throws IOException
Android canvas: drawText indents and wraps off rectangle
Why does a list and state list selector ripple animation not draw completely on API24?
SecurityException on LG when trying to get external storage ringtone title
In Android, how can I persist the information of receiving an intent?
How to Setup StandOut Library by Mark Wei for for api >=15
How to cast a button in a viewPager
Track a custom event after install The App from Facebook Ads with google analytics
Android Studio - How to have .addTextChangedListener work with two EditText
Getting error while doing linkedin login in my android app
How to cut generate time to generate QR Code using Zxing
Accessing Realm DB values inside a simple loop resulting [ ]
Chrome Custom Tabs - Load text/String
how can i encrypt a mp3 file and decrypt and use it?
Add a textview below recyclerview in drawer layout

Categories

HOME
httparty
fabricjs
string
google-spreadsheet
angular-formly
mstest
soa
ado.net
polling
vert.x
flume
bing-maps
fosrestbundle
32bit-64bit
endeca
mel
pega
project-intu
spring-social
favicon
linear-algebra
standards
delphi-xe5
swagger-2.0
pagespeed
direct3d11
data.stackexchange.com
bringtofront
realm-mobile-platform
scrollview
userdefaults
hibernate-validator
fstream
catch-unit-test
mailkit
hibernate-search
sim-toolkit
protein-database
filesystemwatcher
demo
signals-slots
xv6
portforwarding
chronicle-map
placeholder
pom.xml
jboss-4.2.x
openresty
libgphoto2
user-defined-fields
dbcontext
branch-prediction
javafx-css
reachability
stax
cda
dbamp
nullable
np-spring
false-sharing
file-move
computer-architecture
wiredep
ess
rate-limiting
batik
mod-proxy
sha512
liquid-layout
iiop
vbe
gae-quotas
multitasking
script#
traceability
virtual-pc
argouml
curb
veracity
jdb
multiplexing
kqueue
apache-commons-dbutils
urlrewriter
promotion-code
entitydatasource
drawtobitmap
timthumb
msr
qglwidget
maven-1
isapi-extension
strtod
flvplayback
technical-debt
soappy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App