綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 急!!!!!!ListView顯示不出檔案清單

[複製連結] 查看: 1605|回覆: 2|好評: 0
跳轉到指定樓層
樓主
gn02526721 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2013-10-29 10:48
40碎鑽
本帖最後由 gn02526721 於 2013-10-29 10:49 編輯

我要用ListView顯示檔案清單
在除錯也沒問題,進入也不會閃退的情況下
就還是顯示不出來(完完全全是空的)
附件的檔案麻煩請各位幫我看一下哪邊有問題
package com.example.LockInBrain;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class addlist extends Activity {
        
                private View editlist;
                protected AlertDialog EL;
                ArrayList itemname;
                ListView listtoadd;
                String itemintent;
               
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.addchoose);
               
                ImageButton list,additem;
                list=(ImageButton)findViewById(R.id.addlist);
                additem=(ImageButton)findViewById(R.id.additem);
                //新增空白清單-------------------------------------
                list.setOnClickListener(new OnClickListener() {
                        
                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                EL.show();
                        }
                });
               
                LayoutInflater ET=LayoutInflater.from(this);
                editlist = ET.inflate(R.layout.edittext, null);
                EL = new AlertDialog.Builder(this).create();
                EL.setView(editlist);
               
                ((Button)editlist.findViewById(R.id.listnewin)).setOnClickListener(new OnClickListener() {
                                       
                                        @Override
                                        public void onClick(View v) {
                                                // TODO Auto-generated method stub
                                                String ln=((EditText)editlist.findViewById(R.id.newlistname)).getText().toString();
                                                String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/data/data/"+ln+".txt";
                                        File file = new File(filePath);
                                        if(!file.exists())
                                        {
                                                file.getParentFile().mkdirs();
                                                try {
                                                        file.createNewFile();
                                                } catch (IOException e) {
                                                        // TODO Auto-generated catch block
                                                        e.printStackTrace();
                                                }
                                        }
                                        EL.dismiss();
                                        EditText gg=((EditText)editlist.findViewById(R.id.newlistname));
                                        gg.setText("");
                                        }
                                });
               
                EL.setOnKeyListener(new OnKeyListener(){

                                        @Override
                                        public boolean onKey(DialogInterface dialog, int keyCode,
                                                        KeyEvent event) {
                                                // TODO Auto-generated method stub
                                                if(KeyEvent.KEYCODE_BACK==keyCode)
                                    EL.dismiss();
                                                return false;
                                        }
                });
                //新增空白清單-------------------------------------
                //顯示清單----------------------------------------
               
                listtoadd=(ListView)findViewById(R.id.addchooselist);
                String bigpath;
                itemname = new ArrayList();
                ArrayAdapter<String> listAdapter;
                if (Environment.getExternalStorageState().equals(  
                        Environment.MEDIA_MOUNTED)) {  
                    //File path = Environment.getExternalStorageDirectory();   
                    File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/data/data");   
                    File[] files = path.listFiles();
                    getFileName(files);
                }  
                //String ListPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/data/data/";
                SimpleAdapter adapter = new SimpleAdapter(this, itemname, R.layout.showfiles,  
                        new String[] { "Name" }, new int[] { R.id.text_path });  
                listtoadd.setAdapter(adapter);  
                for (int i = 0; i < itemname.size(); i++) {  
                    Log.i("zeng", "list.  name:  " + itemname.get(i));  
                }  
                             
                listtoadd.setOnItemClickListener(new OnItemClickListener(){

                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view,
                                                        int position, long id) {
                                                // TODO Auto-generated method stub
                                                itemintent=listtoadd.getItemAtPosition(position).toString();
                                        }}
                );
               
                additem.setOnClickListener(new OnClickListener(){

                                        @Override
                                        public void onClick(View v) {
                                                // TODO Auto-generated method stub
                                                Intent intent=new Intent();
                                                intent.setClass(addlist.this, additem.class);
                                                intent.putExtra("itemname", itemintent);
                                        }});
               
        }

        private void getFileName(File[] files) {  
            if (files != null) {
                for (File file : files) {  
                    if (file.isDirectory()) {  
                        Log.i("zeng", "若是文件目錄,繼續讀1" + file.getName().toString()  
                                + file.getPath().toString());  
      
                        getFileName(file.listFiles());  
                        Log.i("zeng", "若是文件目錄,繼續讀2" + file.getName().toString()  
                                + file.getPath().toString());  
                    } else {  
                        String fileName = file.getName();  
                        if (fileName.endsWith(".txt")) {  
                            HashMap map = new HashMap();  
                            String s = fileName.substring(0,  
                                    fileName.lastIndexOf(".")).toString();  
                            Log.i("zeng", "文件名txt::   " + s);  
                            map.put("Name", fileName.substring(0,  
                                    fileName.lastIndexOf(".")));  
                            itemname.add(map);  
                        }  
                    }  
                }  
            }  
        }  

        //@Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.title, menu);
                return true;
        }

}

addlist.java

6.9 KB, 下載次數: 1, 下載積分: 碎鑽 -1 個

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
whl | 收聽TA | 只看該作者
發表於 2013-10-29 11:00
沒很仔細看,檢查一下 /sdcard/data/data 有沒有資料。
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

板凳
 樓主| gn02526721 | 收聽TA | 只看該作者
發表於 2013-10-29 11:35
本帖最後由 gn02526721 於 2013-10-29 11:37 編輯


我在設定裡面是那裏面的資料夾不會放置資料
但是可以新增檔案
還是說我新增檔案那邊是有問題的?
順便問一個問題
就是我在新增檔案的時候可以自定義名稱嗎
剛好這個程式碼有新增物品的
在AlertDialog那邊
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則