无法通过Android的意图传递到字符串数组列表数据转换 [英] Unable to convert List data transmitted through Android intent into String array

查看:118
本文介绍了无法通过Android的意图传递到字符串数组列表数据转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Android新手。我试图从一个活动传递到另一个数据。数据的类型是List。它是由recieving活动正确接收。但是我无法做一个字符串数组出来。我在做什么错了?

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?

这是发射活动

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.

满code为reciever如下。我用这与Android通用图像装载机。当我使用imageURLs String类型,我得到imageUrls.length&放一个问题; imageUrls [位置]。

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

完全在循环imageUrls打印的每个元件。有一次,我退出循环,再次打印一个十六进制地址。

Each element of imageUrls prints perfectly in the loop. Once I exit the loop, it prints a hex address again.

推荐答案

在Java中的数组没有的toString 重载的方法(这是值得商榷的阵列是否合适的对象)但列表有,对于后者的toString 产生一个有意义的字符串。您看到的LogCat中的输出是由的toString 生产。刚刚与 Toast.makeText取代你的code(getApplicationContext(),我们有+ tokeo,Toast.LENGTH_LONG).show();

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();

在code看起来应该如下图所示。如果你不断收到问题有了它,你能不能请在这里发表的堆栈跟踪。

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

}

另外,我不知道你为什么一直坚持在列表转换成数组。除非你有一些其他的code,需要的数组,你可以只使用列表则为list.size() List.get(INT)

这篇关于无法通过Android的意图传递到字符串数组列表数据转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆