【问题标题】:Android passing values of JSON object to another activity on list item clickAndroid将JSON对象的值传递给列表项单击的另一个活动
【发布时间】:2015-08-29 01:30:46
【问题描述】:

我想将第一个活动中单击列表项上的正确 json 对象传递给第二个活动。如果我单击任何列表项,来自JSONArray 响应的 json 对象应该传递给意图,并且我的下一个活动必须得到它。 这是我的代码

JsonArrayRequest postReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Consumer user = new Consumer();
                                user.setTitle(obj.getString("name"));
                                user.setUserid(obj.getString("user"));
                                user.setThumbnailUrl(obj.getString("image"));
                                user.setAmountreq(obj.getString("price"));
                                user.setTime(obj.getString("date"));
                                user.setCounty(obj.getString("county"));

                                // adding request to consumer class
                                userList.add(user);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                         /*notifying list adapter about data changes
                         so that it renders the list view with updated data*/
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        hidePDialog();

                    }
                });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(postReq);
        // listening to single list item on click
           listView.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



                  if(!obj.isNull(position)){
                    //start new activity
                      Intent myIntent = new Intent(RequestFeedActivity.this,RequestFeed.class);  
                      myIntent.putExtra("name", obj.getJSONObject(position).toString());
                      startActivity(myIntent);                                     
                    }   

                      // ListView Clicked





              }
            });
    }

这里的另一个活动是我想要传递的变量的示例代码

TextView rname, rtitle,rprice, rdate, rdesc, rcounty;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.requestfeed);

    rtitle = (TextView)findViewById(R.id.IDRproduct);
    rprice = (TextView)findViewById(R.id.IDRquote);
    rdate = (TextView)findViewById(R.id.IDRdate);
    rdesc = (TextView)findViewById(R.id.IDRdesc);
    rname = (TextView)findViewById(R.id.IDRname);

     Intent myIntent = getIntent();
    //Assign values
    rname.setText(myIntent.getExtras().getString("name")); 
    rtitle.setText(myIntent.getExtras().getString("title"));
    rprice.setText(myIntent.getExtras().getString("pricetag"));
    rdate.setText(myIntent.getExtras().getString("timereq"));
    rdesc.setText(myIntent.getExtras().getString("description"));
    rcounty.setText(myIntent.getExtras().getString("county")); 




}

我试过这个answer 但没有用。非常感谢任何帮助

【问题讨论】:

  • James 我试过了,但它在 if(!obj.isNull(position)) 和 obj.getJSONObject(position).toString() 上给出了一个错误,说 JSONObject 类型中的方法是 null 不是适用于参数(int)。
  • 检查已编辑的答案
  • 为什么你使用 JSON 数据为什么你没有像在 onResponse 中那样拆分数据。
  • @haresh @james 感谢您的回答。我查看了那个答案并尝试了这个并且它有效:因为凌空响应仅在 onResponse(JSONArray response) 方法中可见,所以我初始化了一个变量 JSONArray 数组;并分配了值 array= response;在 setOnItemClickListener 上我添加了以下代码 if(!array.isNull(position)){ Intent.putExtra("request",array.optJSONObject(position).toString()); startActivity(intent); } 。在 RequestFeed 类Intent try { JSONObject obj = new JSONObject(myIntent.getStringExtra("request")); rname.setText(obj.getString("user"));

标签: android json


【解决方案1】:

尝试替换这段代码:

myIntent.getExtras().getString("name")

使用此代码:

myIntent.getStringExtra("name");

【讨论】:

  • 是的,Haresh 在此代码 if(!obj.isNull(position)) 和此 obj.getJSONObject(position).toString() 上给出错误。
【解决方案2】:

试试这个

Intent i =new Intent(this,ActivityA.class);
Bundle b =new Bundle();
i.putExtra("KEY","VALUE");
i.putExtras(b);
startActivity(i)

在接收端

Bundle b=getIntent().getExtras();
b.getInt("KEY") //useaccording to ur need

如果它不起作用,请检查此answer

【讨论】:

  • James 我看了那个答案并尝试了这个:由于凌空响应仅在 onResponse(JSONArray response) 方法中可见,因此我初始化了一个变量 JSONArray 数组;并分配值 array = response;
猜你喜欢
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2017-12-13
  • 1970-01-01
相关资源
最近更新 更多