我是安卓新手。我正在尝试将数据从一项活动传递到另一项活动。数据是列表类型。它被接收活动正确接收。但是我无法从中创建一个字符串数组。我究竟做错了什么?
这是传输活动
List<String> where = new ArrayList<String>();
intent.putStringArrayListExtra("tokeo", (ArrayList<String>) where);
startActivity(intent);
第一个日志语句正确地打印了我期望的内容。但是,toast 和随后的日志消息会打印一个十六进制地址值。
接收者的完整代码如下。我将它与 Android 通用图像加载器一起使用。当我对 imageURLs 使用 String 类型时,我遇到了 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;
}
}
}
查看该站点上的其他一些帖子,我尝试了以下方法:
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);
imageUrls 的每个元素都在循环中完美打印。一旦我退出循环,它会再次打印一个十六进制地址。