Android 我需要打开一个Pdf文件时,卡查看按钮点击,每个卡都有自己的Pdf文件

Android 我需要打开一个Pdf文件时,卡查看按钮点击,每个卡都有自己的Pdf文件,android,android-volley,androidpdfviewer,Android,Android Volley,Androidpdfviewer,这是我的Orderofday.java,我从URL中获取数据,在字符串文件中获得pdf链接。我已经显示了每张卡片上的数据,但无法在点击的卡片按钮上打开pdf链接 public class Orderofday extends AppCompatActivity { private static final String TAG = Orderofday.class.getSimpleName(); private RecyclerView recyclerView;

这是我的Orderofday.java,我从URL中获取数据,在字符串文件中获得pdf链接。我已经显示了每张卡片上的数据,但无法在点击的卡片按钮上打开pdf链接

public class Orderofday extends AppCompatActivity {

    private static final String TAG = Orderofday.class.getSimpleName();

    private RecyclerView recyclerView;
    private OrderList mAdapter;

    ProgressDialog progess;

    List<OrdersModel> myList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderofday);
        progess = new ProgressDialog(Orderofday.this);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mAdapter = new OrderList(myList, Orderofday.this);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);
        getResult();
    }

    public void getResult() {
        String url = "http://senate.gov.pk/ne/getdata/order_of_day_json.php";

        //detect internet and show the data
        if (isNetworkStatusAvialable(getApplicationContext())) {
            progess.setMessage("Loading data....");
            progess.show();
            StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {


                @Override
                public void onResponse(String response) {
                    Log.d("response", response);
                    try {
                        JSONObject mainList = new JSONObject(response);
                        JSONArray result = mainList.getJSONArray("data");
                        if (result.length() > 0) {
                            for (int i = 0; i < result.length(); i++) {
                                mainList = result.getJSONObject(i);
                                OrdersModel model = new OrdersModel();
                                model.title_en = mainList.getString("title_en");
                                model.YearID = mainList.getString("YearID");
                                model.session_title_en = mainList.getString("session_title_en");
                                model.sitting_date = mainList.getString("sitting_date");
                                model.proceeding_file = mainList.getString("proceeding_file");
                                myList.add(model);
                            }
                            mAdapter.notifyDataSetChanged();
                            progess.dismiss();
                        } else {
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "Error: " + error.getMessage());
                    progess.dismiss();
                }
            });
            Log.d("params", strReq.getUrl() + " and " + strReq.getBodyContentType() + " abd " + strReq.getBodyContentType());
            // Adding String request to request queue
            AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, TAG);
        } else {
            Toast.makeText(getApplicationContext(), "Please check your Internet Connection", Toast.LENGTH_SHORT).show();
        }
    }

    //check internet connection
    public static boolean isNetworkStatusAvialable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
            if (netInfos != null) {
                return netInfos.isConnected();
            }
        }
        return false;
    }
}
现在我需要打开一个pdf对每一个卡的看法点击我得到了pdf的网址在procedding_文件(字符串),但无法得到它。请帮助我实现这一点,这里是xml

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_margin="@dimen/_5sdp"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:textStyle="bold"
            android:textSize="20sp"
            android:layout_margin="5dp"
            android:text="Title_ID"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/type"
            android:layout_below="@id/name"
            android:layout_margin="5dp"
            android:textSize="15sp"
            android:text="YearID"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/session_title"
            android:layout_below="@id/type"
            android:layout_margin="5dp"
            android:textSize="15sp"
            android:text="Session Title"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/sitting_date"
            android:layout_below="@id/session_title"
            android:layout_margin="5dp"
            android:textSize="15sp"
            android:text="Date "
            android:textColor="@android:color/black" />

        <Button
            android:id="@+id/view_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sitting_date"
            android:layout_alignParentRight="true"
            android:text="Click to View Pdf"
            android:textColor="#000000"
            android:textSize="@dimen/_10sdp">
        </Button>

        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

初始化ViewHolder中的查看按钮

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView name, type , session_title , sitting_date , proceeding_file;
    public Button viewButton;

    public MyViewHolder(View view) {
        super(view);
        name = view.findViewById(R.id.name);
        type = view.findViewById(R.id.type);
        session_title = view.findViewById(R.id.session_title);
        sitting_date = view.findViewById(R.id.sitting_date);
        viewButton = view.findViewById(R.id.view_button);
    }
}
然后在
viewButton
上设置
onClickListener
并打开PDF

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

    OrdersModel movie = list.get(position);
    holder.name.setText(movie.gettitle_en());
    holder.type.setText(movie.getYearID());
    holder.session_title.setText((movie.getsession_title_en()));
    holder.sitting_date.setText(movie.getsitting_date());

    holder.viewButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openPdf(movie.getproceeding_file())
        }
    });
}

建议:您应该在另一个活动中移动您的
PdfViewer
,并在单击
viewButton
时将用户重定向到该活动上面的答案是正确的。然而,一个更好、最正确的解决方案是为ClickListNet创建一个界面,并在tour活动中实现它

  • 在MyViewHolder类中初始化view_pdf按钮。像其他人一样

  • 像其他人一样解析正在进行的文件~movie.getProcessing_file()

  • 在onBindViewHolder下,放入以下代码片段:

      holder.proceeding_file.setOnClickListener(new View.OnClickListener() {
    
      @Override
      public void onClick(View view) {
    
            String pdf_url = movie.getproceeding_file();
            // do not forget to concate preceding url of the pdf
            Webview webView = (WebView) findViewById(R.id.webView1);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(pdf_url);
    
          }
       });
    

  • openPdf()方法在哪里?您必须根据自己的需求实现它吗?你想干什么?您将PdfViewer放在listitem中。如何在这个狭小的空间内显示pdf?检查我的建议,因此我建议您使用shxdos pdfviewer,因为它支持来自
    URL
    的pdf。检查:我有一个关于此活动的webview,当单击按钮时,它将导航到webview以打开PDFI。它是相同的活动还是不同的活动?也张贴那部分
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
    
        OrdersModel movie = list.get(position);
        holder.name.setText(movie.gettitle_en());
        holder.type.setText(movie.getYearID());
        holder.session_title.setText((movie.getsession_title_en()));
        holder.sitting_date.setText(movie.getsitting_date());
    
        holder.viewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openPdf(movie.getproceeding_file())
            }
        });
    }
    
      holder.proceeding_file.setOnClickListener(new View.OnClickListener() {
    
      @Override
      public void onClick(View view) {
    
            String pdf_url = movie.getproceeding_file();
            // do not forget to concate preceding url of the pdf
            Webview webView = (WebView) findViewById(R.id.webView1);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(pdf_url);
    
          }
       });