Java 安卓-调整微调器,使其不选择任何内容或在第一选择时不执行任何操作

Java 安卓-调整微调器,使其不选择任何内容或在第一选择时不执行任何操作,java,android,spinner,Java,Android,Spinner,如何使微调器不自动启动?我在这里看到了一些解决方案,但它们把我弄糊涂了。当活动开始时,下载立即开始,而这不是需要发生的。如何允许用户进行选择,而不是自动下载第一个项目 代码如下: public class SpinnerActivity extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private ProgressDialog mProgressDialog; Spinner spDownlo

如何使微调器不自动启动?我在这里看到了一些解决方案,但它们把我弄糊涂了。当活动开始时,下载立即开始,而这不是需要发生的。如何允许用户进行选择,而不是自动下载第一个项目

代码如下:

public class SpinnerActivity extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

Spinner spDownloadFrom;
private ArrayAdapter<CharSequence> spinnerArrayAdapter;
String url[] = {
        "http://www.becker.cl/bases.pdf",
        "http://www.pitt.edu/documents/campusmap0607.pdf",
        "http://www.aara.ca/reg3317/web_page_doc.pdf",
        "http://www.dataprotection.ie/documents/guidance/GuidanceFinance.pdf",
        "http://www.fmbb2012.com/JumpingQualifica1.pdf",
        "http://www.consulatdumaroc.ca/coloniefh22012.pdf",
        "http://www.rgrdlaw.com/media/cases/140_Complaint.pdf" };
String name[] = {"bases.pdf", "campusmap0607.pdf", "web_page_doc.pdf",
        "GuidanceFinance.pdf", "JumpingQualifica1.pdf",
        "coloniefh22012.pdf", "140_Complaint.pdf", };

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mProgressDialog = new ProgressDialog(SpinnerActivity.this);
    mProgressDialog.setMessage("Please be patient, file downloading...");
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setMax(100);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    spDownloadFrom = (Spinner) findViewById(R.id.Spinner01);

    spinnerArrayAdapter = new ArrayAdapter<CharSequence>(this,
            android.R.layout.simple_spinner_item, name);
    spinnerArrayAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spDownloadFrom.setAdapter(spinnerArrayAdapter);

    spDownloadFrom.setOnItemSelectedListener(new SpinnerListener(
            spDownloadFrom));
}

public class SpinnerListener implements OnItemSelectedListener {
    Spinner sp;

    public SpinnerListener(View v) {
        sp = (Spinner) findViewById(v.getId());
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int arg2,
            long arg3) {
        // Call to download class
        startDownload(arg2);

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

private void startDownload(int position) {
    DownloadFile downloadFile = new DownloadFile(position);
    downloadFile.execute(url[position]);
}

class DownloadFile extends AsyncTask<String, Integer, String> { // put your
                                                                // download
                                                                // code
    private int position;

    public DownloadFile(int position) {
        this.position = position;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected String doInBackground(String... aurl) {
        try {

            URL url = new URL(aurl[0]);
            URLConnection connection = url.openConnection();

            connection.connect();
            int fileLength = connection.getContentLength();
            int tickSize = 2 * fileLength / 100;
            int nextProgress = tickSize;

            Log.d(

            "ANDRO_ASYNC", "Lenght of file: " + fileLength);

            InputStream input = new BufferedInputStream(url.openStream());

            String path = Environment.getExternalStorageDirectory()
                    + "/Android/Data/"
                    + getApplicationContext().getPackageName() + "/files/";
            File file = new File(path);
            file.mkdirs();
            File outputFile = new File(file, name[position]);

            OutputStream output = new FileOutputStream(outputFile);

            byte data[] = new byte[1024 * 1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                if (total >= nextProgress) {
                    nextProgress = (int) ((total / tickSize + 1) * tickSize);
                    this.publishProgress((int) (total * 100 / fileLength));
                }
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
            mProgressDialog.dismiss();

        } catch (Exception e) {
            Log.e("Spinner", "exception", e);
        }
        return null;
    }

    protected void onProgressUpdate(String... progress) {
        Log.d("Downloading", progress[0]);

    }

    @Override
    protected void onPostExecute(String unused) {

        mProgressDialog.dismiss();

        File file = new File(Environment.getExternalStorageDirectory()
                + "/Android/Data/"
                + getApplicationContext().getPackageName() + "/files/"
                + name[position]);
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType("application/pdf");
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(SpinnerActivity.this,
                    "No Application Available to View PDF",
                    Toast.LENGTH_LONG).show();
        }
    }
}
}
公共类SpinnerActivity扩展活动{
公共静态最终整型对话框\u下载\u进度=0;
private ProgressDialog mProgressDialog;
纺纱机;
专用阵列适配器spinnerayadapter;
字符串url[]={
"http://www.becker.cl/bases.pdf",
"http://www.pitt.edu/documents/campusmap0607.pdf",
"http://www.aara.ca/reg3317/web_page_doc.pdf",
"http://www.dataprotection.ie/documents/guidance/GuidanceFinance.pdf",
"http://www.fmbb2012.com/JumpingQualifica1.pdf",
"http://www.consulatdumaroc.ca/coloniefh22012.pdf",
"http://www.rgrdlaw.com/media/cases/140_Complaint.pdf" };
字符串名称[]={“bases.pdf”、“campusmap0607.pdf”、“web\u page\u doc.pdf”,
“GuidanceFinance.pdf”、“JumpingQualifica1.pdf”,
“coloniefh22012.pdf”,“140_Complaint.pdf”,};
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mProgressDialog=新建ProgressDialog(spinneActivity.this);
设置消息(“请耐心等待,文件下载…”);
mProgressDialog.setUndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_水平);
spDownloadFrom=(微调器)findViewById(R.id.Spinner01);
SpinNearrayAdapter=新阵列适配器(此,
android.R.layout.simple\u微调器(项目、名称);
纺纱机
.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
spDownloadFrom.setAdapter(spinnerrayadapter);
spDownloadFrom.setOnItemSelectedListener(新SpinnerListener(
spDownloadFrom));
}
公共类SpinnerListener实现了OnItemSelectedListener{
纺纱机;
公共SpinnerListener(视图v){
sp=(微调器)findViewById(v.getId());
}
@凌驾
已选择公共视图(AdapterView父视图、视图v、内部arg2、,
长arg3){
//调用下载类
开始下载(arg2);
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
}
专用void startDownload(内部位置){
DownloadFile DownloadFile=新下载文件(位置);
downloadFile.execute(url[position]);
}
类DownloadFile扩展了AsyncTask{//将
//下载
//代码
私人职位;
公共下载文件(int位置){
这个位置=位置;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
mProgressDialog.show();
}
@凌驾
受保护的void onProgressUpdate(整数…进度){
super.onProgressUpdate(进度);
mProgressDialog.setProgress(progress[0]);
}
@凌驾
受保护的字符串背景(字符串…aurl){
试一试{
URL=新URL(aurl[0]);
URLConnection=url.openConnection();
connection.connect();
int fileLength=connection.getContentLength();
int tickSize=2*fileLength/100;
int-nextProgress=tickSize;
对数d(
“ANDRO_ASYNC”,“文件长度:”+fileLength);
InputStream输入=新的BufferedInputStream(url.openStream());
String path=Environment.getExternalStorageDirectory()
+“/Android/Data/”
+getApplicationContext().getPackageName()+“/files/”;
文件=新文件(路径);
mkdirs()文件;
File outputFile=新文件(文件,名称[位置]);
OutputStream output=新文件OutputStream(outputFile);
字节数据[]=新字节[1024*1024];
长总计=0;
整数计数;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
如果(总计>=下一步){
nextProgress=(int)((总计/tickSize+1)*tickSize);
此.publishProgress((int)(总计*100/文件长度));
}
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
mProgressDialog.disclose();
}捕获(例外e){
Log.e(“微调器”、“异常”,e);
}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
Log.d(“下载”,进度[0]);
}
@凌驾
受保护的void onPostExecute(字符串未使用){
mProgressDialog.disclose();
File File=新文件(Environment.getExternalStorageDirectory()
+“/Android/Data/”
+getApplicationContext().getPackageName()+“/files/”
+姓名[职务];
Intent testIntent=新的意图(Intent.ACTION\u视图);
testIntent.setType(“application/pdf”);
意图=新意图();
intent.setAction(intent.ACTION\u视图);
Uri=Uri.fromFile(文件);
setDataAndType(uri,“应用程序/pdf”);
试一试{
星触觉(意向);
}捕获(ActivityNotFounde异常){
Toast.makeText(SpinnerActivity.this,
“没有可用于查看PDF的应用程序”,
Toast.LENGTH_LONG).show();
}
}
}
}
试试这段代码

public class SpinnerActivity extends Activity {

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    private ProgressDialog mProgressDialog;

    Spinner spDownloadFrom;
    private ArrayAdapter<CharSequence> spinnerArrayAdapter;
    String url[] = {
            "http://www.becker.cl/bases.pdf",
            "http://www.pitt.edu/documents/campusmap0607.pdf",
            "http://www.aara.ca/reg3317/web_page_doc.pdf",
            "http://www.dataprotection.ie/documents/guidance/GuidanceFinance.pdf",
            "http://www.fmbb2012.com/JumpingQualifica1.pdf",
            "http://www.consulatdumaroc.ca/coloniefh22012.pdf",
            "http://www.rgrdlaw.com/media/cases/140_Complaint.pdf" };
    String name[] = { "bases.pdf", "campusmap0607.pdf", "web_page_doc.pdf",
            "GuidanceFinance.pdf", "JumpingQualifica1.pdf",
            "coloniefh22012.pdf", "140_Complaint.pdf", };
    private boolean checkFlag = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mProgressDialog = new ProgressDialog(SpinnerActivity.this);
        mProgressDialog.setMessage("Please be patient, file downloading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        spDownloadFrom = (Spinner) findViewById(R.id.Spinner01);

        spinnerArrayAdapter = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_spinner_item, name);
        spinnerArrayAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spDownloadFrom.setAdapter(spinnerArrayAdapter);

        SpinnerListener spListener = new SpinnerListener();
        spDownloadFrom.setOnItemSelectedListener(spListener);
    }

    public class SpinnerListener implements OnItemSelectedListener {
        public SpinnerListener() {
        }

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            if(checkFlag){
                startDownload(position);
            }
            checkFlag = true;

        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    }

    private void startDownload(int position) {
        DownloadFile downloadFile = new DownloadFile(position);
        downloadFile.execute(url[position]);
    }

    class DownloadFile extends AsyncTask<String, Integer, String> { // put your
                                                                    // download
                                                                    // code
        private int position;

        public DownloadFile(int position) {
            this.position = position;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            mProgressDialog.setProgress(progress[0]);
        }

        @Override
        protected String doInBackground(String... aurl) {
            try {

                URL url = new URL(aurl[0]);
                URLConnection connection = url.openConnection();

                connection.connect();
                int fileLength = connection.getContentLength();
                int tickSize = 2 * fileLength / 100;
                int nextProgress = tickSize;

                Log.d(

                "ANDRO_ASYNC", "Lenght of file: " + fileLength);

                InputStream input = new BufferedInputStream(url.openStream());

                String path = Environment.getExternalStorageDirectory()
                        + "/Android/Data/"
                        + getApplicationContext().getPackageName() + "/files/";
                File file = new File(path);
                file.mkdirs();
                File outputFile = new File(file, name[position]);

                OutputStream output = new FileOutputStream(outputFile);

                byte data[] = new byte[1024 * 1024];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    if (total >= nextProgress) {
                        nextProgress = (int) ((total / tickSize + 1) * tickSize);
                        this.publishProgress((int) (total * 100 / fileLength));
                    }
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
                mProgressDialog.dismiss();

            } catch (Exception e) {
                Log.e("Spinner", "exception", e);
            }
            return null;
        }

        protected void onProgressUpdate(String... progress) {
            Log.d("Downloading", progress[0]);

        }

        @Override
        protected void onPostExecute(String unused) {

            mProgressDialog.dismiss();

            File file = new File(Environment.getExternalStorageDirectory()
                    + "/Android/Data/"
                    + getApplicationContext().getPackageName() + "/files/"
                    + name[position]);
            Intent testIntent = new Intent(Intent.ACTION_VIEW);
            testIntent.setType("application/pdf");
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(SpinnerActivity.this,
                        "No Application Available to View PDF",
                        Toast.LENGTH_LONG).show();
            }
        }
    }
}
公共类SpinnerActivity扩展活动{
公共静态最终整数对话框\u DOWNLOA