0

我是安卓新手。我正在尝试将数据从一项活动传递到另一项活动。数据是列表类型。它被接收活动正确接收。但是我无法从中创建一个字符串数组。我究竟做错了什么?

这是传输活动

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 的每个元素都在循环中完美打印。一旦我退出循环,它会再次打印一个十六进制地址。

4

2 回答 2

1

Evgeny 的回答是正确的,因为 Java 没有为数组类型提供 toString。相反,您可以使用您从 Java 5Arrays实用程序类中选择的静态方法。例如deepToString()

final String imageUrlsString = Arrays.deepToString(imageUrls);
Toast.makeText(getApplicationContext(), "We have " + imageUrlsString, Toast.LENGTH_LONG).show();
于 2013-07-07T00:03:15.833 回答
1

Java 中的数组没有toString方法重载(数组是否是正确的对象值得商榷),但列表有,并且对于后者toString会产生一个有意义的字符串。您看到的 LogCat 输出是由 that 生成的toString。只需将您的代码替换为Toast.makeText(getApplicationContext(), "We have " + tokeo, Toast.LENGTH_LONG).show();

代码应如下所示。如果您不断遇到“问题”,请在此处发布堆栈跟踪。

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;
    }
}

}

另外,我不确定您为什么一直坚持将列表转换为数组。除非您有其他需要数组的代码,否则您可以只使用List,List.size()List.get(int).

于 2013-07-06T23:49:50.277 回答