I got a byte arraylist(List<byte[]> barray= new ArrayList<byte[]>();).And i need to pass it to another activity.How i can do it with intent.putextra method or something else ?
Something like this to populate the intent:
Intent intent = new Intent(...);
intent.putExtra("barray_size", barray.size());
for (int i = 0; i < barray.size(); i++) {
intent.putExtra("barray"+i, barray.get(i));
}
Then to get them out in the other activity:
Intent intent = getIntent();
ArrayList<byte[]> barray = new ArrayList<>();
int size = intent.getIntExtra("barray_size", 0);
for (int i = 0; i < size; i++) {
barray.add(intent.getByteArrayExtra("barray"+i));
}
May you can use a JSON serializer to accomplish this task. So you can convert from any (List in your case) to string and this is easy to serialize.
Related
I have data from JSON like
5,7,9
and i want to save in arrayList as
"","","","","",5,"",7,"",9
what should i do?
try to change code like below in main forloop
Change your weeklyDataList like below
ArrayList<int[]>weeklyDataList = new ArrayList<int[]>();
Code in Forloop
int[] numbers = new int[characters.length()];
for (int j = 0; j < characters.length(); j++) {
numbers[j] = Integer.parseInt(characters.getString(j));
}
weeklyDataList.add(i,numbers);
I have this array list and i parsing list item from json like this ,
List<String> imageUrls;
imageUrls = new ArrayList<>();
JSONArray imageArray = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey);
for (int j = 0; j < imageArray.length(); j++) {
String imageList = imageArray.getJSONObject(j).getString(labelKey).toString();
imageUrls.add(imageList);
}
appShowModule.setAllimage(imageUrls);
then i try to do this in another activity ,
intent.putStringArrayListExtra("list", appShowModule.getAllimage());
but "appShowModule.getAllimage()" is error ! and how i can received it ?
You should implement Parcelable in your class.
Check this best link for easy understanding.
Thanks
Add Your Array list to intent from which you are calling another activity
intent.putStringArrayListExtra("list", yourarraylist);
startActivity(intent);
To get the array list in activity.
ArrayList list= new ArrayList();
list= getIntent().getStringArrayListExtra("list");
Hope this will help you.
You can use this method in Intent:
public Intent putExtra(String name, CharSequence[] value)
change List imageUrls to ArrayList imageUrls and
intent.putStringArrayListExtra("key",imageUrls);
Send your list like this
Intent i = new Intent(this, YourDesiredActiity.class);
i.putStringArrayListExtra("arrayListToSend",imageUrls);
startActivity(i);
And then in your desired activity , receive the list like this
ArrayList<String> urlList = new ArrayList<String>();
Bundle extras = getIntent().getExtras();
if (extras != null) {
urlList.addAll(extras.getStringArrayList("arrayListToSend"));
}
Then you can get your desired arrayList in "urlList"
Data is not binding in array list as it came from the Service, as like first option which added in ArrayList is Lead, second is Qualified, and third is test. When i check list on first position it shows Qualified, Lead, test. But i want as i bind my list as it show in that sequence.
public static HashMap<String,ArrayList<LeadData>> LeadDataMap= new HashMap<String,ArrayList<LeadData>>();
public static ArrayList<String> aList = new ArrayList<String>();
for (int i = 0; i < LeadListsJSONArray.length(); i++) {
JSONObject Leadsobj = LeadListsJSONArray.getJSONObject(i);
//get stage name for hashmap key value..
String StageNameString = Leadsobj.getString("StageName");
String StageIdString = Leadsobj.getString("StageId");
System.out.println("stage id........................"+StageIdString);
//get new leads list..
JSONArray jaarr2 = new JSONArray(Leadsobj.getString("Leads"));
ArrayList<LeadData> leadDataList = new ArrayList<LeadData>();
for (int j = 0; j < jaarr2.length(); j++) {
LeadData ld = new LeadData();
JSONObject obj3 = jaarr2.getJSONObject(j);
ld.setLeadCompanyName(obj3.getString("LeadCompanyName"));
ld.setLeadId(obj3.getString("LeadId"));
ld.setTitle(obj3.getString("Title"));
leadDataList.add(ld);
}
//here we are puttin the leaddatalist inot map with respect to stage name...
LeadDataMap.put(StageNameString.trim().toString(), leadDataList);
//LeadDataMap.put(StageIdString, leadDataList);
}
In LeadDataMap data in not in that sequence in that i have put. This is the problem.
I get solution by using LinkedHashmap.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
i spend hole day on looking what is going on. In one class I've got simple listview with multiple choice. At the end each choice is put to String array and share to next class.
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
int position = checked.keyAt(i);
// Add sport if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
selectedItems.add(adapter.getItem(position));
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
Intent intent = new Intent(getApplicationContext(),
ReadComments.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
// Add the bundle to the intent.
intent.putExtras(b);
// start the ResultActivity
startActivity(intent);
In ReadComments class i've made simple method:
public String[] tablica(){
Bundle b = getIntent().getExtras();
String[] resultArr = b.getStringArray("selectedItems");
return resultArr;
}
which bring back my data. When I put it to another method in the same class:
String resultArr[] = tablica();
int x = 0;
for (Map<String, String> mListaMar : mListaMarketow) {
lat = mListaMar.get(TAG_SZER);
longi = mListaMar.get(TAG_DLUG);
name = mListaMar.get(TAG_MARKET);
x=0;
for (x = 0; x < resultArr.length; x++) {
if (resultArr[x] == name) {
ustawMape();
}}}
UstawMape() method is responsible for show marker on google map. My problem is a logical problem because everything is working fine but ustawMape() method should show many markers and right now it show only one (construcion of ustawMape() is ok because without for loop it's work ok). The clue is very simple first loop for bring data from JSON and the second one should filter and show only this which user choose in listView. PLZ help my somebody!!
if (resultArr[x] == name)
Use equals() for string comparisons instead of ==:
if (name.equals(resultArr[x]))
== compares object references which won't be the same unless the strings are interned. equals() compares the string values.
I'm trying to pass an array of Address objects to another Activity through an Intent object.
As the Address class implements the Parcelable interface I try to do the following. I got a List Address object from a Geocoder object, which I convert into a array of Address objects. Then I put this array into the Intent and call the activity.
final Address[] addresses = addresseList.toArray(new Address[addresseList.size()]);
final Intent intent = new Intent(this, SelectAddress.class);
intent.putExtra(SelectAddress.INTENT_EXTRA_ADDRESSES, startAddresses);
startActivityForResult(intent, REQUEST_CODE_ACTIVITY_SELECT_ADDRESSES);
On the other activity I try to retrieve the Address[] from the Intent with the following piece of code. But the call of the last line ends with a ClassCastException Landroid.os.Parcelable.
Bundle bundle = getIntent().getExtras();
Address[] addresses = (Address[]) bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);
What am I doing wrong? How do I have to retrieve the Address[].
The problem is the casting. try:
Bundle bundle = getIntent().getExtras();
Parcelable[] parcels = bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);
Address[] addresses = new Address[parcels.length];
for (Parcelable par : parcels){
addresses.add((Address) par);
}
or on java1.6:
Parcelable[] x = bundle.getParcelableArray(KEY);
addresses = Arrays.copyOf(x, x.length, Address[].class);
The #LiorZ answer is completely true. I just merged his answer and this other in this handy function.
#SuppressWarnings("unchecked")
private static <T extends Parcelable> T[] castParcelableArray(Class<T> clazz, Parcelable[] parcelableArray) {
final int length = parcelableArray.length;
final T[] array = (T[]) Array.newInstance(clazz, length);
for (int i = 0; i < length; i++) {
array[i] = (T) parcelableArray[i];
}
return array;
}