PDF下载或意图

时间:2019-02-22 16:38:13

标签: java android firebase google-cloud-firestore

我用RecyclerView制作了一个CardViews,它从Cloud Firestore检索了两个TextViews,并且每当我单击存储卡时,都根据存储的pdfUrl想要它在我的Firestore数据库中,可以使用pdf打开活动或将pdf下载到手机中。但是我不知道该怎么实现。

Here's my firestore structure

我的代码:

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

}

1 个答案:

答案 0 :(得分:0)

  

每当我单击该卡时,都需要根据我的Firestore数据库中存储的pdfUrl进行操作,以使用pdf打开活动或将pdf下载到手机中

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

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

url是存储在数据库中的实际URL。就是这样!