【问题标题】:Intent passing from adapter to activity从适配器传递到活动的意图
【发布时间】:2017-11-10 08:31:43
【问题描述】:

对不起,不清楚的问题。我是 android 和 stackover 流的新手

我有一个适配器,其中包含来自服务器的图像和负载列表。 在图像的 Onclick 上,我需要打开另一个显示单击项目的完整图像的活动。 那时我需要将图像 url 作为意图传递给下一个活动。 我在传递意图方面需要帮助,并请澄清我将意图中的图像 url 从适配器传递到活动是否安全? 提前感谢您的帮助

我的适配器类

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
    List<Upload> list_of_messages;
    Context context;
    Activity mActivity;
    PhotoViewAttacher pAttacher;

    public MyAdapter(Activity activity,List<Upload> list_of_messages,Context context) {
        this.mActivity = activity;
        this.list_of_messages = list_of_messages;
        this.context=context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_promise_list,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyAdapter.ViewHolder holder, final int position) {
        final Upload upload = list_of_messages.get(position);
        holder.text_date.setText(upload.getName());
        Glide.with(context).load(upload.getUrl()).into(holder.text_promise);

        final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

        holder.text_promise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mActivity,ImageViewZoom.class);
                intent.putExtra("image_url",upload.getUrl());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list_of_messages.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView text_date;
        ImageView text_promise;
        public ViewHolder(View itemView) {
            super(itemView);
            text_promise=itemView.findViewById(R.id.text_promise_text);
            text_date=itemView.findViewById(R.id.text_promise_date);
        }
    }

    public void onImageClicked(Activity a, String url) {
        a.startActivity(new Intent(a, FullScreenImageActivity.class).putExtra("image_url", url));
        a.overridePendingTransition(android.R.anim.fade_in, 1);
    }
}

点击 holder.text_promise(Image view) 我需要导航到 ImageViewZoom 活动,我需要从适配器加载图像。

我感到困惑并寻求帮助。

【问题讨论】:

  • 请添加您尝试过的代码

标签: android android-intent


【解决方案1】:

我在传递意图方面需要帮助

您可以将 URI 传递给 ShowImage 活动。在您用于启动活动的意图中:

Intent intent = new Intent(context, ShowImage.class);
intent.putExtra("URI", yourURI);
startActivity(intent);

在下一个活动中访问该意图

String s = getIntent().getStringExtra("URI");

请澄清我是否可以安全地从 活动适配器?

实际上这是实现相同目标的最佳方式。

【讨论】:

  • 谢谢。谢谢大家的建议。
猜你喜欢
  • 2018-08-14
  • 1970-01-01
  • 2019-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
相关资源
最近更新 更多