【问题标题】:How to use Button click to open url from JSON feed?如何使用按钮单击从 JSON 提要打开 url?
【发布时间】:2018-06-23 19:42:47
【问题描述】:

我正在使用 Volley 获取 JSON 提要,其中包含名称、评级和 URL。我在 Android 上有 textview,它将显示 Fetched Name、Rating 和所有内容。我有一个 Button,但它直接显示 URL,因为我将 Text 设置为 Fetched URL。

目前,我得到了这个:

我需要将按钮的文本设置为注册,如果单击按钮,它应该打开从 JSON 提要中获取的 URL。

附上代码

    @Override
    public void onBindViewHolder(CourseViewHolder holder, int position) {

        Course course = courseList.get(position);

        holder.textViewCoursename.setText(course.getCoursename());
        holder.textViewcoursedescshort.setText(course.getCoursedescshort());
        //holder.textViewcourseurl.setText(course.getCourseurl());
        holder.textViewcourserating.setText(course.getCourserating());

        Glide.with(mCtx)
                .load(course.getCourseimg())
                .into(holder.imageView);

        Button button = (Button) button.findViewById(R.id.textViewcourseurl);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //Do stuff here
            }
        });
    }

【问题讨论】:

  • 你会在浏览器或网页视图中打开 url 吗?
  • 我正在尝试在 Browser Buddy 中打开 :)
  • 我已经回答了这个问题,请检查:)

标签: java android json


【解决方案1】:

您可以使用Intent 完成此任务。

//Just put Intent on your button click
button.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(course.getCourseurl()));
        mCtx.startActivity(i);
        //This will open url in browser if you have application in your device.
    }
});

如果您想在WebView 中加载网址,请查看以下链接

How to load external webpage inside WebView

【讨论】:

  • 我必须在哪个文件中使用这个?
  • @MCNaveen 在适配器中你已经为按钮制作了点击监听器
  • 尝试用您的代码替换我的代码。现在我收到以下错误...错误:(64、17)错误:ContextCompat 类中的方法 startActivity 不能应用于给定类型;必需:找到上下文、意图、捆绑包:意图原因:实际参数列表和形式参数列表的长度不同
  • 编译安装成功,但是打开后App老是闪退。这是 Logcat:pastebin.com/naAvZ7Z8
  • @MCNaveen 按钮按钮 = (Button) button.findViewById(R.id.textViewcourseurl);这段代码在适配器的 ViewHolder 中,就像您为 TextViews 完成 findviewbyid 一样。比使用 holder.btn.setOnCliccklistner...
【解决方案2】:

我需要将按钮的文本设置为注册,如果单击按钮,它应该打开从 JSON 提要中获取的 URL

您应该加载带有指定操作的 Intent 的 URL。 例如加载 URL:

String url = //get your url from the JSON feed ;

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

基本上,上面的代码将完成查找可以加载 URL 的应用程序并为您加载 URL 的工作。

在将 URL 传递给意图之前,不要忘记检查 URL 是否为空

【讨论】:

    【解决方案3】:

    在您声明按钮的适配器中,您需要实现按钮的onClick 方法:

    Button button = (Button) button.findViewById(R.id.textViewcourseurl);
    button.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            Uri uri = Uri.parse("http://www.google.com"); 
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        }
    });
    

    这将帮助您在可用浏览器中打开特定网址。 如果您想在 webview 中打开您的网址,请点击此链接。 How to open a url in webview on new screen based on button click

    【讨论】:

      【解决方案4】:

      在您的适配器onBindViewHolder() 方法中,它会是这样的:

      // get the course url
      String courserURL = course.getCourseurl();
      button.setOnClickListener(new Button.OnClickListener() {
           public void onClick(View v) {
               // make sure the course url value is not empty
               if(!courserURL.isEmpty()){
                   // assign an ActionView to run that URL
                   Intent i = new Intent(Intent.ACTION_VIEW);
                   i.setData(Uri.parse(courserURL));
                   holder.button.getContext().startActivity(i);
               }
           }
       });
      

      【讨论】:

      • 返回错误Error:(69, 21) error: method startActivity in class ContextCompat cannot be applied to given types; required: Context,Intent,Bundle found: Intent reason: actual and formal argument lists differ in length
      • @MCNaveen 肯定会,因为您必须使用上下文启动Activity,请尝试我更新的答案,您也可以在这里查看stackoverflow.com/a/32137097/1638739
      【解决方案5】:

      要更改底部名称,您需要使用ArrayList<String>打开底部的链接:

      Intent browserIntent = new Intent(Intent.ACTION_VIEW);
      browserIntent.setData("YourLinkHere);
      context.startActivity(browserIntent);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-02
        • 1970-01-01
        • 1970-01-01
        • 2017-11-27
        • 2018-09-08
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多