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

Displaying dynamic text in Android
Facebook: Share URL from mobile browser to Facebook app
Why does'nt calling Finish() immediately close my activity?
settransform() method in Android
How to auto suggest edittext in android
how to get Android platform code-names like Honeycomb
Android: show a progress dialog then dismiss properly
Android Activity force closing when a new Intent is called
How to PASS ON proximity change event
Null pointer exception on doing webview.loadUrl()
can not delete from database
Android Service Multiple Threads and startForeground()
Android Jellybean app encryption works on other versions?
how to send image from one activity to another, fetching records in listview using xml parser
What are the correct Android manifest settings for an Adobe AIR application to restrict device availability?
When is app published on facebook?

Categories

HOME
search
google-cloud-platform
delphi-7
case
apache-spark-mllib
camera
error-handling
appcelerator
settings
google-cloud-datalab
jtextpane
vert.x
title
frequency-distribution
renjin
mongodb-csharp
bitnami
entitymanager
searchview
xmonad
sigma.js
solrcloud
lilypond
poedit
postscript
flask-sqlalchemy
interpreter
dynamically-generated
xunit
data.stackexchange.com
datastax-enterprise-graph
cppreference
python-2.6
record
r-grid
fstream
alphabetical
protein-database
bc
rfc3339
jide
dotnet-httpclient
hammer.js
vugen
insight
mongotemplate
lightning
remedy
trojan
stdmap
boxing
opengl-4
edit
google-photos
choice
materialized-path-pattern
stripe-connect
usart
microstation
preprocessor
neon
macaulay2
ng-grid
gcloud-ruby
thread-sleep
ascii-art
relativelayout
maybe
directdraw
boomerang
android-launcher
sony-lifelog-api
dulwich
keyup
botan
shopizer
uncaught-typeerror
ghcjs
crowd
teaspoon
named-parameters
lambda-architecture
codeplex
draw2d-js
paste
sbcl
ecslidingviewcontroller
gtm-oauth2
modular
unit-of-work
rolify
gpars
poker
apache-abdera
android-loadermanager
frameworkelementfactory
midlet
faye
thttpd
httponly
apache-commons-dbutils
google-maps-mobile
ruby-1.8
message-passing
request-headers
data-mapping
wcf-instancing
application-planning

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App