当前位置: 代码迷 >> java >> PDF下载或意图
  详细解决方案

PDF下载或意图

热度:18   发布时间:2023-08-04 09:12:50.0

我制作了一个带有CardViewsRecyclerView ,它从Cloud Firestore检索了两个TextViews ,每当我单击该卡时,我都希望基于Firestore数据库中存储的pdfUrl进行操作,以使用pdf打开活动或将pdf下载到电话。 但我不知道该如何实现。

我的代码:

public class RecylerViewTestsActivity extends AppCompatActivity {

RecyclerView recyclerView;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private FirestoreRecyclerAdapter<uploadTests, TestesViewHolder> adapter;



@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recyler_view_tests);

    RecyclerView recyclerView = findViewById(R.id.recyclerViewTests);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
    Query query = rootRef.collection("FilterPDFUploads");


    FirestoreRecyclerOptions<uploadTests> options = new FirestoreRecyclerOptions.Builder<uploadTests>()
            .setQuery(query, uploadTests.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<uploadTests,TestesViewHolder >(options) {
        @Override
        protected void onBindViewHolder(@NonNull TestesViewHolder holder, int position, @NonNull uploadTests model) {
            holder.setTestesDesc(model.getDescription());
            holder.setTestesName(model.getUsername());

        }

        @NonNull
        @Override
        public TestesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pdf_tests_items, parent, false);
            return new TestesViewHolder(view);
        }
    };
    recyclerView.setAdapter(adapter);


}

//////////////////////////VIEW HOLDER////////////////////////////////////////////

private class TestesViewHolder extends RecyclerView.ViewHolder {
    private View view;
    private CardView itemsCard;

    TestesViewHolder(final View itemView) {
        super(itemView);
        view = itemView;

    }

    void setTestesDesc (String descri) {
        TextView desc = view.findViewById(R.id.teste_description);
        desc.setText(descri);
    }
    void setTestesName (String username) {
        TextView user = view.findViewById(R.id.name_id);
        user.setText(username);
    }
    void setPdfUrl(final String url){

    }
}

//////////////////////////VIEW HOLDER////////////////////////////////////////////

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();

    if (adapter != null) {
        adapter.stopListening();
    }
}

}

每当我单击该卡时,我都希望根据存储在我的Firestore数据库中的pdfUrl将该活动与pdf打开或将pdf下载到手机中

要解决此问题,您应该在TestesViewHolder对象上使用setOnClickListener() 在点击监听器中,请添加以下代码行:

Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

其中url是存储在数据库中的实际URL。 而已!

  相关解决方案