如何创建listview下载pdf文件?

时间:2017-04-24 17:49:50

标签: android listview

我想制作简单的listview,点击它下载pdf文件。 我试了很长时间才这样做但却失败了。

这是我的应用程序的xml布局,所以如果有错误请帮助我。我是android studio的新手

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.asus.myapplication.develop1_list_progrme">
    <include layout="@layout/toolbar"
        android:id="@+id/my_toolbar"></include>
    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="match_parent"
        android:layout_height="260sp"
        android:background="#C3E2DC"
        app:srcCompat="@drawable/progra_2"
        android:layout_below="@+id/my_toolbar"
        android:layout_alignParentStart="true"
        tools:ignore="ContentDescription" />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/imageView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="ScrollViewSize">



    <ListView
        android:id="@+id/list_pro"
        android:layout_width="300sp"
        android:layout_height="380sp"
        android:nestedScrollingEnabled="true"
        android:layout_marginTop="13dp"
        android:layout_marginRight="40sp"
        android:scrollbarSize="20sp"
        android:layout_marginLeft="10sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        tools:ignore="NestedScrolling,RtlHardcoded" />


</RelativeLayout>
    </ScrollView>
</RelativeLayout>
package com.example.asus.myapplication;

import android.app.DownloadManager;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class develop1_list_progrme extends AppCompatActivity {
    DownloadManager downloadManager;
    ListView mListView_pro;
    private String [] pro ={"Chapter 1: Introduction ", "Chapter 2: Basic definitions",  "Chapter 3: Numerical conversions","Chapter 4: Logical gates"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_develop1_list_progrme);
        ListView mListView_pro = (ListView)findViewById(R.id.list_pro);
        Toolbar my_toolbar= (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(my_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Semester(2)");
        getSupportActionBar().setSubtitle("أساسيات برمجة");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, pro){
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                /// Get the Item from ListView
                View view = super.getView(position, convertView, parent);

                TextView tv = (TextView) view.findViewById(android.R.id.text1);
                tv.setHeight(200);
                tv.setMinimumHeight(200);
                // Set the text size 25 dip for ListView each item
                tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP,25);

                // Return the view
                return view;
            }
        };

        mListView_pro.setAdapter(adapter);
        mListView_pro
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {


                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if(position == 0) {


                                downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                                Uri uri = Uri.parse("https://letterless-pulls.000webhostapp.com/itro_pro.pdf");
                                DownloadManager.Request request = new DownloadManager.Request(uri);
                                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                Long reference = downloadManager.enqueue(request);

                        }

请帮帮我

1 个答案:

答案 0 :(得分:0)

首先,由于缺乏代码,这个答案的某些部分基本上是猜测。

在您的清单中,添加以下行:

vm.SelectedContainerDefinition = ContainerDefinitions[0]

如果这没有帮助,请在问题中添加您的清单和堆栈跟踪(如果存在/如果发生崩溃)

有关下载文件的详细说明,请参阅CommonsWare的this github repo