RecyclerView URL重定向到WebView(WebScraping)

时间:2018-12-05 09:02:21

标签: java android android-recyclerview

我正在使用webScraping创建新闻应用程序,并使用RecyclerView包含新闻的行数据。

现在我的问题是,当用户单击新闻行之一(也许是RecyclerView onClick吗?)时,我不知道如何重定向新闻URL,因为使用网络抓取,每个新闻都有不同的URL。

[请注意,我无法弄清楚如何在onClickListener中实现URL Intent,因此我不对该部分进行编码]

任何帮助将不胜感激,谢谢:')

这是我的MainActivity.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/act_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

这是我的row_data.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/row_news_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/row_news_author"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"/>

        <TextView
            android:id="@+id/row_news_upload_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"/>

    </LinearLayout>

</android.support.v7.widget.CardView>

这是我的DataAdapter代码:

package com.example.user.webscraping2;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder> {

    private ArrayList<String> mNewsTitleList = new ArrayList<>();
    private ArrayList<String> mAuthorNameList = new ArrayList<>();
    private ArrayList<String> mNewsUploadDateList = new ArrayList<>();
    private Activity mActivity;
    private int lastPosition = -1;

    public DataAdapter(MainActivity activity, ArrayList<String> mNewsTitleList, ArrayList<String> mAuthorNameList, ArrayList<String> mNewsUploadDateList) {
        this.mActivity = activity;
        this.mNewsTitleList = mNewsTitleList;
        this.mAuthorNameList = mAuthorNameList;
        this.mNewsUploadDateList = mNewsUploadDateList;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView gamezone_news_title, gamezone_news_author, gamezone_upload_date;

        public MyViewHolder(View view) {
            super(view);
            gamezone_news_title = view.findViewById(R.id.row_news_title);
            gamezone_news_author = view.findViewById(R.id.row_news_author);
            gamezone_upload_date = view.findViewById(R.id.row_news_upload_date);
        }

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_data,parent,false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.gamezone_news_title.setText(mNewsTitleList.get(position));
        holder.gamezone_news_author.setText(mAuthorNameList.get(position));
        holder.gamezone_upload_date.setText(mNewsUploadDateList.get(position));
    }

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

}

这是我的MainActivity代码:

package com.example.user.webscraping2;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ProgressDialog mProgressDialog;
    private String url = "https://www.gamezone.com/news/";
    private ArrayList<String> mAuthorNameList = new ArrayList<>();
    private ArrayList<String> mNewsUploadDateList = new ArrayList<>();
    private ArrayList<String> mNewsTitleList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Description().execute();
    }

    private class Description extends AsyncTask<Void,Void,Void> {
        String desc;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(MainActivity.this);
            mProgressDialog.setTitle("Android Basic Jsoup Tutorial");
            mProgressDialog.setMessage("Loading ...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Document mNewsDocument = Jsoup.connect(url).get();
                Elements mElementDataSize = mNewsDocument.select("div[class=td_module_1 td_module_wrap td-animation-stack]");
                int mElementSize = mElementDataSize.size();

                for (int i=0;i<mElementSize;i++) {
                    Elements mElementAuthorName = mNewsDocument.select("span[class=td-post-author-name]").select("a").eq(i);
                    String mAuthorName = mElementAuthorName.text();

                    Elements mElementNewsUploadDate = mNewsDocument.select("span[class=td-post-date]").select("time").eq(i);
                    String mNewsUploadDate = mElementNewsUploadDate.text();

                    Elements mElementNewsTitle = mNewsDocument.select("h3[class=entry-title td-module-title]").select("a").eq(i);
                    String mNewsTitle = mElementNewsTitle.text();

                    mAuthorNameList.add(mAuthorName);
                    mNewsUploadDateList.add(mNewsUploadDate);
                    mNewsTitleList.add(mNewsTitle);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            RecyclerView mRecyclerView = findViewById(R.id.act_recyclerview);

            DataAdapter mDataAdapter = new DataAdapter(MainActivity.this, mNewsTitleList, mAuthorNameList, mNewsUploadDateList);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mDataAdapter);

            mProgressDialog.dismiss();

        }

    }

}

这是屏幕截图:

Image

1 个答案:

答案 0 :(得分:0)

尝试此代码,

holder.gamezone_news_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = mNewsTitleList.get(position);
                Intent mintent = new Intent(mActivity, "your webview load class name");
                mintent.putExtra("keytitle", title);
                startActivity(mintent);

            }
        });

在加载网址之前写入代码的下一次Webview加载活动。

String url = "https://www.gamezone.com/news/";
    Bundle mbundle = getIntent().getExtras();
     if(mbundle != null){
       String str = mbundle.getString("keytitle");
       url += str+"/";
       url = url.replaceAll(" ", "-");
    }