【问题标题】:Adding jsonarray from recyclview从 recyclerview 添加 json 数组
【发布时间】:2019-05-21 07:02:09
【问题描述】:

每当我点击 recyerview 项目时,我都会向 jsonarray 添加数据。但是,如果我向下滚动并且上一页不可见,则会重置 jsonarray。

@Override
public void onBindViewHolder(final productAdapter.productViewHolder holder, final int position) {

    jsonObject=new JSONObject();
    wrapObject=new JSONObject();
    jsonArray = new JSONArray();

    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, position+" "+productItem.getName(), Toast.LENGTH_SHORT).show();

            long now = System.currentTimeMillis();
            Date date = new Date(now);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String getTime = sdf.format(date);

            SharedPreferences profilePref = context.getSharedPreferences("myprofile", MODE_PRIVATE);
            idPf = profilePref.getString("id", "");
            genderPf = profilePref.getString("gender", "");
            ageGroupPf = profilePref.getString("ageGroup", "");

            try {
                jsonObject.put("id",idPf);
                jsonObject.put("gender",genderPf);
                jsonObject.put("ageGroup",ageGroupPf);
                jsonObject.put("category",productItem.getCategory());
                jsonObject.put("timeStamp",getTime);
                jsonObject.put("product",productItem.getName());
                jsonObject.put("view",ANDROID_VIEW);
                jsonObject.put("status",PURCHASE_STATUS);
                jsonArray.put(jsonObject);

                System.out.println("@@jsonArray.lenth: "+jsonArray.length()+" jsonArray: "+jsonArray);

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

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(productItem.getProductLink()));
            context.startActivity(intent);
        }
    });
}

我想继续向 jsonarray 添加数据,不管滚动。

【问题讨论】:

  • 能否请您更详细地解释一下,以便更好地理解。

标签: java android json android-recyclerview


【解决方案1】:

为此,您必须在 onBindViewHolder 方法之外初始化您的 JsonArray,例如在适配器类的顶部初始化它

public Adapter {
    JSONArray jsonArray = new JSONArray();

/////further code here
}

【讨论】:

    猜你喜欢
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多