0

Here is my Orderofday.java where i am getting data from URL and in String proceeding_file i get pdf link. i have shown the data present on every card but could not achieve to open a pdf link on card button clicked.

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;
    }
}

Here is my OrderList.java class where i am setting the text from 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);
        }
    }
}

And here is my OrderModel.java where i made getters and 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;
}

Now i need to open a pdf against every card view clicked i got url of pdf in procedding_file (String) but couldn't get it. Kindly help me out to achieve this and here is 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>
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46

3 Answers3

0

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

Then set onClickListener on viewButton and openPdf

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

Suggestion: You should move your PdfViewer in another activity and redirect user to that activity when click viewButton.

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

Above answer is correct. However a better solution and the rightest one is to create an interface for the clicklistenet and implement it in tour activity

james04
  • 1,580
  • 2
  • 20
  • 46
0
  1. initialise view_pdf button in MyViewHolder class. like others.

  2. parse proceeding file like others ~ movie.getproceeding_file()

  3. under the onBindViewHolder put below snippet of code:

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