在onResponse凌空中初始化后我无法使用变量

时间:2016-05-19 11:58:20

标签: java android json

为了确定直接链接,我应该解析

适配器中的json。在onBindViewHolder按钮的onClicklistener`中

叫。我得到直接下载链接。

  

这是My Adapter的OnBinderViewHolder,其中解析了json

 @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
       ===> String DownloadUrl;

        holder.txtTitle.setText(questionha.get(position).getQuestionTitle());
        holder.txtDesc.setText(questionha.get(position).getQuestionDesc());
        holder.txtCntDown.setText(questionha.get(position).getQuestionDownCnt());
        holder.txtAuthorName.setText(questionha.get(position).getQuestionAuthorName());
        Glide.with(holder.itemView.getContext()).load(questionha.get(position).getQuestionAuthorPic()).into(holder.imgAuthorPic);
        holder.txtDate.setText(questionha.get(position).getQuestionDate());
        holder.btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                JsonObjectRequest volleyRequest = new JsonObjectRequest(questionha.get(position).getQuestionDownLink(), new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null || response.length() > 0) {
                            try {
                              ===>DownloadUrl = response.getString("link");
                                Toast.makeText(context, DownloadUrl, Toast.LENGTH_SHORT).show();

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

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });
                AppController.getInstance().addToRequestQueue(volleyRequest);


                //Toast.makeText(context, DownloadUrl, Toast.LENGTH_SHORT).show();
                DownloadManager downloadManager = (DownloadManager) context.getSystemService (Context.DOWNLOAD_SERVICE);
               ===>DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadUrl));;
                request.setTitle(questionha.get(position).getQuestionTitle())
                        .setDescription("در حال دانلود")
                        .setDestinationInExternalFilesDir(context, "question", questionha.get(position).getQuestionTitle());
                long enqueueId = downloadManager.enqueue(request);

            }
        });

    }

现在,当我想将DownloadUrl变量用于DownloadManger的url时,我在volley onResponse中遇到了这样的错误:

  

变量“DownloadUrl”是从内部类访问的,需要   最终

当我设定最终Donwloadurl时,它会说:

  

无法将值分配给最终变量“DownloadUrl”

感谢。

0 个答案:

没有答案