【问题标题】:Send Json data from listview in one activity to another activity将 Json 数据从一个活动中的列表视图发送到另一个活动
【发布时间】:2013-08-26 05:52:27
【问题描述】:

我正在尝试将 json 对象数据从一个活动发送到另一个活动。我有一个从 json 数组成功填充的列表视图,但是当我单击列表项时,它没有响应。单击项目时未执行任何操作。我看过一些教程,但没有帮助。我需要将列表项的 json 数据发送到另一个活动。任何帮助将不胜感激。

使用 Json 填充的列表视图的活动。

       try{

         jArray = new JSONArray(result);
         ListView listView=(ListView)findViewById(R.id.listtour_cat);
         listView.setAdapter(new ListViewAdapter(JSONUseActivity.this,jArray));
         listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                try{
                    if(!jArray.isNull(arg2)){
                           Intent intent = new Intent(getApplicationContext(),TourDetailActivity.class);  
                           intent.putExtra("id", jArray.optJSONObject(arg2).getString("tour_id"));
                           startActivity(intent);    
                     }

                }
                catch(JSONException e){
                       Log.e("log_tag", "Error parsing data "+e.toString());
               }

            }
        });
       }
       catch(JSONException e){
               Log.e("log_tag", "Error parsing data "+e.toString());
       }

  }
        catch (Exception e) {
          Log.e("log_tag","Error in http connection!!" + e.toString());     
  }

目标活动:

Intent intent = getIntent();
String rcvid = intent.getStringExtra("id");
Toast.makeText(getApplicationContext(), rcvid, Toast.LENGTH_LONG).show();

【问题讨论】:

  • @Divyang-请显示您的 logcat 错误以了解问题-
  • 你确定没有调用 onItemClick 吗?或者你能检查一下你是否在 onItemClick 块中遇到任何异常?
  • @FarhaSameer786 - 它没有任何错误..它只是在单击列表项时没有做任何事情。

标签: android json listview android-intent


【解决方案1】:

将 JsonArray 转换为字符串,然后将其附加到 Intent 并发送。

    JSONObject jObject = new JSONObject("Your Json Response");

    Intent obj_intent = new Intent(Main.this, Main1.class);
    Bundle b = new Bundle();                
    b.putString("Array",jObject4.toString());
    obj_intent.putExtras(b);

其中 jObject4 是 JSON 对象。

进入下一页:

        Intent b = getIntent().getExtras();
        String Array=b.getString("Array");

【讨论】:

    【解决方案2】:

    试试这条线..

    Intent intent = new Intent(getApplicationContext(),TourDetailActivity.class);  
    intent.putExtra("id", jArray.getJSONObject(arg2).getString("tour_id"));
    startActivity(intent);    
    

    【讨论】:

      【解决方案3】:

      将此代码用于在第二类中获取价值..

      String rcvid = getIntent().getExtras().getString("id").toString();
      

      并从 First class 传递值,意图如下:

      intent.putExtra("id", jArray.optJSONObject(arg2));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-31
        • 2017-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多