Reputation: 2821
I'm an Android newbie. I'm trying to pass data from one activity to another. The data is of type List. It is received properly by the recieving activity. However I'm unable to make a string array out of it. What am I doing wrong?
This is the transmitting activity
List<String> where = new ArrayList<String>();
intent.putStringArrayListExtra("tokeo", (ArrayList<String>) where);
startActivity(intent);
The first log statement correctly prints what I expect it to. However the toast and the subsequent log message print a hex address value.
The full code for the reciever is as follows. I am using this with the Android Universal Image Loader. When I use type String for imageURLs, I get a problem with imageUrls.length & imageUrls[position].
public class PrimaryImageGridActivity extends AbsListViewBaseActivity {
DisplayImageOptions options;
String imageUrls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primary_image_grid);
Intent i = getIntent();
// getting attached intent data
ArrayList<String> tokeo = i.getStringArrayListExtra("tokeo");
Log.i("Shades", "We got " + tokeo);
//imageUrls = new String[tokeo.size()];
imageUrls = tokeo.toString();
//String[] result = i.getStringExtra("tokeo");
Toast.makeText(getApplicationContext(), "We have " + imageUrls,
Toast.LENGTH_LONG).show();
Log.i("Shades", "We got " + imageUrls);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
.cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(this));
listView = (GridView) findViewById(R.id.gridview);
((GridView) listView).setAdapter(new ImageAdapter());
}
/*
* @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
* menu; this adds items to the action bar if it is present.
* getMenuInflater().inflate(R.menu.primary_image_grid, menu); return true;
* }
*/
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrls[position], imageView, options);
return imageView;
}
}
}
Looking at some other posts on this site, I tried the following:
int s = tokeo.size();
imageUrls = new String[s];
for(int k=0; k < s; k++){
imageUrls[k] = tokeo.get(k);
Log.i("test", "We got " + tokeo.get(k));
Log.i("and", "We got " + imageUrls[k]);
}
Log.i("Shades", "We got " + imageUrls);
Each element of imageUrls prints perfectly in the loop. Once I exit the loop, it prints a hex address again.
Upvotes: 0
Views: 369
Reputation: 1194
Arrays in Java do not have toString
method overloaded (it is debatable whether arrays are proper Objects), but Lists have, and for the latter toString
produces a meaningful string. The LogCat output that you see is produced by that toString
. Just replace your code with Toast.makeText(getApplicationContext(), "We have " + tokeo, Toast.LENGTH_LONG).show();
The code should look as below. If you keep getting "problems" with it, could you please post the stack trace here.
public class PrimaryImageGridActivity extends AbsListViewBaseActivity {
DisplayImageOptions options;
String[] imageUrls;//ET
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primary_image_grid);
Intent i = getIntent();
// getting attached intent data
ArrayList<String> tokeo = i.getStringArrayListExtra("tokeo");
Log.i("Shades", "We got " + tokeo);
imageUrls = tokeo.toArray(new String[tokeo.size()]);//ET
//imageUrls = tokeo.toString();
//String[] result = i.getStringExtra("tokeo");
Toast.makeText(getApplicationContext(), "We have " + Arrays.deepToString(imageUrls),
Toast.LENGTH_LONG).show();//ET
Log.i("Shades", "We got " + Arrays.deepToString(imageUrls));//ET
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
.cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(this));
listView = (GridView) findViewById(R.id.gridview);
((GridView) listView).setAdapter(new ImageAdapter());
}
/*
* @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
* menu; this adds items to the action bar if it is present.
* getMenuInflater().inflate(R.menu.primary_image_grid, menu); return true;
* }
*/
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrls[position], imageView, options);
return imageView;
}
}
}
Also, I am not sure why you keep insisting on converting the list into the array. Unless you have some other code that requires the array, you could just use the List
, List.size()
and List.get(int)
.
Upvotes: 1
Reputation: 9933
Evgeny's answer is correct in that Java provides no toString for array types. Instead you can use the static method of your choice from Java 5's Arrays
utility class. For example, deepToString()
:
final String imageUrlsString = Arrays.deepToString(imageUrls);
Toast.makeText(getApplicationContext(), "We have " + imageUrlsString, Toast.LENGTH_LONG).show();
Upvotes: 1