将TreeMap从一个活动传递给另一个活动

时间:2014-01-14 22:23:25

标签: android android-intent treemap

我有一个

TreeMap<String, ArrayList<Video>> mShows = new TreeMap<String, ArrayList<Video>>();

我试图从一个活动转到另一个活动。我试过了

&#34; putExtra treeMap returns HashMap cannot be cast to TreeMap android&#34;

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     Intent i = getIntent();
     Bundle bun = i.getBundleExtra("lascategorias");
     mCategories = bun.getStringArray("categories");
    mShows = new TreeMap<String, ArrayList<Video>>(Map<String,ArrayList<Video>> getIntent().getExtras().get("map"));         
}  

但它说Map,String和ArrayList无法解析为我所拥有的变量(Map&gt;)

这就是我添加它的方式

protected void onPostExecute(final String json) {
        Log.i(LOG_TAG, "decoding...");
        if (json == null)
            return;
        try {
            final URI base = new URI(mUrl);
            final JSONObject jso = new JSONObject(json);
            final JSONArray ja = jso.getJSONArray("categories");
            for (int i = 0; i < ja.length(); i++) {
                final JSONObject list = ja.getJSONObject(i);
                final ArrayList<Video> vidList = new ArrayList<Video>(10);
                mShows.put(list.getString("name"), vidList);
                final JSONArray videos = list.getJSONArray("videos");
                for (int j = 0; j < videos.length(); j++) {
                    final JSONObject vids = videos.getJSONObject(j);
                    final JSONArray sources = vids.getJSONArray("sources");
                    try {
                        final Video v = new Video(base.resolve(vids.getString("thumb")),
                                base.resolve(sources.getString(0)), vids.getString("title"),
                                vids.getString("subtitle"), vids.getString("description"));
                        vidList.add(v);

                        // The rare case that I've got more than one
                        for (int k = 1; k < sources.length(); k++) {
                            v.mSource.add(base.resolve(sources.getString(k)));
                        }
                    } catch (IllegalArgumentException expected) {
                        Log.e(LOG_TAG, "Bad Json.");
                    }
                }
            }
        } catch (final JSONException je) {
            Log.e(LOG_TAG, "An error in the JSON." + je.getMessage());
        } catch (final URISyntaxException se) {
            Log.e(LOG_TAG, "URI syntax error" + se.getMessage());
        }
        //onDataComplete();
        mCategories = getCategories();
        Bundle b = new Bundle();
        b.putStringArray("categories", mCategories);
        //b.p
        Intent i = new Intent("com.video.tv.MainActivity" );
        i.putExtra("las Categorias", b);
        i.putExtra("map", mShows);
        startActivity(i);
        finish();
    }
}

I declared mShows earlier
public TreeMap<String, ArrayList<Video>> mShows = new TreeMap<String, ArrayList<Video>>();

1 个答案:

答案 0 :(得分:0)

在getIntent()之前缺少一个结束括号。正确的形式:

mShows = new TreeMap<String, ArrayList<Video>>((TreeMap<String, ArrayList<Video>>) getIntent().getExtras().get("map"));