oncreate()中的onclick listview

时间:2014-07-25 07:04:58

标签: android listview onclick

我正在获取值并在我的异步任务的onpostexecute()上设置适配器,当我点击oncraete()中列表视图的项目时它没有显示任何内容,我试图显示一个toast来测试它。

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.list);
    new Ftpclient().execute();
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            if (position == 0) {
                Toast.makeText(getApplicationContext(), "clicked on", 500).show;
                 String f = "FILE54.pdf";
                 File file = new File(Environment
                 .getExternalStorageDirectory()
                 + "/FtpFiles"
                 + f);

                 if (file.exists()) {
                 Uri filepath = Uri.fromFile(file);
                 Intent intent = new Intent(Intent.ACTION_VIEW);
                 intent.setDataAndType(filepath, "application/pdf");
                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                 try {
                 startActivity(intent);
                 } catch (Exception e) {
                 Toast.makeText(getApplicationContext(),
                 "file not found", 500);
                 Log.e("error", "" + e);
                 }
             }

        }
    });

       class Ftpclient extends AsyncTask<String, Void, ArrayList<String>> {
    ArrayList<String> temparrlist = new ArrayList<String>();
    ProgressDialog dialog;

    protected void onPreExecute() {
        dialog = ProgressDialog.show(MainActivity.this, "Connecting",
                "please wait");
    }

    protected ArrayList<String> doInBackground(String... connection) {
        temparrlist = listftpitems();
        return temparrlist;

    }

    protected void onPostExecute(ArrayList<String> result) {
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1,
                result);
        lv.setAdapter(arrayAdapter);
        dialog.dismiss();
    }
}

总是感谢帮助,谢谢

  

修改

ArrayList<String> temparrlist = new ArrayList<String>();
            temparrlist = listftpitems();

            String uri = temparrlist.get(position).toString();
         File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/FtpFiles" + "/" + uri);

            if (uri.endsWith(".pdf") || uri.endsWith(".txt")) {

                Intent target = new Intent(Intent.ACTION_VIEW);
                target.setDataAndType(Uri.fromFile(file), "application/pdf");
                target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                Intent intent = Intent.createChooser(target, "Open File");

                try {
                    startActivity(intent);
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),
                            "file not found", 500).show();
                    Log.e("error", "" + e);
                }

2 个答案:

答案 0 :(得分:2)

此代码......

Toast.makeText(getApplicationContext(), "clicked on", 500);

应该是这样......

Toast.makeText(getApplicationContext(), "clicked on", Toast.LENGTH_SHORT).show();

您必须使用show()方法来显示吐司通知.....

编辑:

使用指定时间您可以使用以下代码..........

final Toast toast = Toast.makeText(getApplicationContext(), "clicked on", Toast.LENGTH_SHORT);
    toast.show();

    Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               toast.cancel(); 
           }
    }, 500);

编辑:

打开PDF使用以下代码...对我有用...........但请确保您在自己的手机中有PDF阅读器............

btn_open.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            name = edt_filename.getText().toString(); // name of selected file... 
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/documents" +"/"+ name+".pdf"); // going to directory

            Intent target = new Intent(Intent.ACTION_VIEW);
            target.setDataAndType(Uri.fromFile(file),"application/pdf");
            target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

            Intent intent = Intent.createChooser(target, "Open File");
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                // Instruct the user to install a PDF reader here, or something
            }  

        }
    });

答案 1 :(得分:0)

改变你的吐司:Toast.makeText(getApplicationContext(),&#34;点击&#34; +位置,500).show();