简体   繁体   English

如何在Android中将哈希映射数组从一个活动发送到另一个活动

[英]How to send array of Hashmap from one activity to another activity in android

I have a list view in Main activity, I have fill some values in it by using Array Adapter 我在Main活动中有一个列表视图,使用数组适配器在其中填充了一些值

String[] arr = new String[]{"Android ListView Item 1", "Android ListView Item 2",
        "Simple List View In Android", "List View onClick Event","Android List View OnItemClickListener",
        "Open New Activity When ListView item Clicked", "List View onClick Source Code", "List View Array Adapter Item Click",
};

Now I want to set onclickListener to each item of the list. 现在,我想将onclickListener设置为列表的每个项目。 I am requesting an api which gives me jsonresponse and I want to iterate over jsonresponse and send required data to another activity when click item of the list. 我正在请求一个给我jsonresponse的API,并且我想遍历jsonresponse并在单击列表项时将所需的数据发送到另一个活动。

I tried with following: 我尝试以下:

ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(this, 
R.layout.activity_main2, R.id.textview, arr);

    listview.setAdapter(arrayadapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            String textToPass = "hello";
            if (position == 0) {
                Bundle passData = new Bundle();
                passData.putString("data",textToPass);
                Intent myIntent = new Intent(view.getContext(), Main2Activity.class);

                myIntent.putExtras(passData);
                startActivity(myIntent);
              }
         }
    });
}

private String jsonParse(){

    String url = "//testing/api.php"; //get json response locally
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            JSONArray jsonArray = null;
            try {
                jsonArray = response.getJSONArray("Item");

                for(int i=0; i<=jsonArray.length();i++){

                    HashMap<String,String> dict = new HashMap<String,String>();

                    JSONObject tv = jsonArray.getJSONObject(i);
                    String sid;

                    dict.put("cid", tv.getString("sid") );

                   // String cid = tv.getString("sid");
                    String categoryName =tv.getString("category_name");
                    String baseUrl = "//testing/";
                    String imageUrl = baseUrl + tv.getString("category_image");


                    //textView.append(cid + "," + categoryName + "," + imageUrl + "\n\n");

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            assert jsonArray != null;
            Log.d("Main",jsonArray.toString());

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(error.getMessage(), "utf-8");
        }
    });



    requestQueue.add(jsonObjectRequest);

Please help 请帮忙

If I understood properly then I think you want to send HashMap dic to another activity. 如果我理解正确,那么我认为您想将HashMap dic发送到另一个活动。 HashMap are serializable this means you can easily pass it in intent, moreover both key and value are string which are also serializable HashMap是可序列化的,这意味着您可以轻松地按意传递它,而且键和值都是字符串,它们也可以序列化

intent.putExtra("dic.key", dic);

And in your next activity retrieve it with typecasting.. simple 并在下一个活动中使用类型转换来检索它。

HashMap<String, String> hashMap = (HashMap<String, String>) intent.getSerializableExtra("hashMap");

I have not test this yet, but give it a try: 我尚未对此进行测试,请尝试一下:
At sender activity 发件人活动时

HashMap<String, String> dict = new HashMap<>();

// Fill your data to dict 
...

Bundle data = new Bundle();

// Save hash map size
data.putInt("size", dict.size());

// Save each entry (String, String)
int i = 0;
for (Map.Entry<String, String> entry : dict.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    data.putString("key" + i, key);
    data.putString("value" + i, value);
    ++i;
}

// Put data to intent starting receiver activity
...

At receiver activity 在接收者活动时

// Get bundle data from intent
Bundle data = getIntent().getExtras();

// Create a new map
HashMap<String, String> map = new HashMap<>();

// Get hash map size
int size = data.getInt("size");

// Get each entry from bundle then put to hash map
for (int i = 0; i < size; ++i){
    String key = data.getString("key" + i);
    String value = data.getString("value" + i);
    map.put(key, value);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想将整数,字符串,对象,双精度,位图,哈希图从一个活动发送到android中的另一个活动 - I want to send integer,String,Object,double,Bitmap,HashMap from one activity to another in android 如何在Kitkat Android中将图像从一个活动发送到另一个活动? - How to send image from one activity to another in kitkat android? 如何在Android中将图像从一个活动发送到另一个活动 - How to send image from one activity to another in android 如何在Android中将数据从一个活动发送到另一个活动? - how to send data from one activity to another in android? Android:如何将接口从一个活动发送到另一个活动 - Android: How to send interface from one activity to another 如何在android中将Calendar对象从一个活动发送到另一个活动? - How to send a Calendar object from one activity to another in android? 如何在Android中将一个活动的对象列表发送到另一个活动? - How to send a list of objects from one activity to another in android? 如何在Android中将TCP套接字从一个活动发送到另一个活动? - How to send TCP Sockets from one Activity to another in android? 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 如何从一个活动转移到活动之外的另一个活动 - how to move from an activity to another one from outside an activity android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM