0

这是我的 Orderofday.java,我在其中从 URL 获取数据,在 Stringprogressing_file 中我得到 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;
    }
}

这是我的 OrderList.java 类,我在其中设置来自 OderModel.java 的文本

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

    List<OrdersModel> list;
    Context c;

    public OrderList(List<OrdersModel> list, Context c) {

        this.list = list;
        this.c = c;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.orders_list, parent, false);

        return new MyViewHolder(itemView);
    }

    @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());
    }


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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name, type , session_title , sitting_date , proceeding_file;
        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);
        }
    }
}

这是我的 OrderModel.java,我在其中制作了 getter 和 setter

public class OrdersModel {

    String title_en;
    String YearID;
    String session_title_en;
    String sitting_date;
    String proceeding_file;


    public String gettitle_en() {
        return title_en;
    }
    public void settitle_en(String title_en) {
        this.title_en = title_en;
    }


    public String getYearID() {
        return YearID;
    }
    public void setYearID(String YearID) {
        this.YearID = YearID;
    }


    public String getsession_title_en() {
        return session_title_en;
    }
    public void setsession_title_en(String session_title_en) {
        this.session_title_en = session_title_en;
    }


    public String getsitting_date() {
        return sitting_date;
    }
    public void setsitting_date(String session) {
        this.sitting_date = sitting_date;
    }


    public String getproceeding_file() {
        return proceeding_file;
    }
    public void setproceeding_file(String date) {
        this.proceeding_file = proceeding_file;
    }

    String file;
}

现在我需要针对每个点击的卡片视图打开一个 pdf,我在 procedding_file(字符串)中获得了 pdf 的 url,但无法获得它。请帮助我实现这一目标,这里是 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>
4

3 回答 3

0

上面的答案是正确的。然而,更好的解决方案和最正确的解决方案是为 clicklistenet 创建一个接口并在旅游活动中实现它

于 2019-10-30T06:52:01.840 回答
0
  1. MyViewHolder 类中的初始化 view_pdf 按钮。像其他人一样。

  2. 像其他人一样解析进程文件〜movie.getproceeding_file()

  3. 在 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);
    
          }
       });
    
于 2019-10-30T07:49:33.083 回答
0

viewButton在 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);
    }
}

然后设置并onClickListener打开viewButtonPdf

@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

于 2019-10-30T06:39:03.320 回答