3

我在 AsyncTask 中用 ArrayAdapter 填充 ListView。但每次它只填写一项。

有人可以解释我为什么吗?

创建

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connected_download);
    Log.v("string", "test6");

    runOnUiThread(new Runnable() {
        public void run() {
            str_profil = getIntent().getStringExtra("profil");
            str_server = getIntent().getStringExtra("server");
            str_port = getIntent().getStringExtra("port");
            str_user = getIntent().getStringExtra("user");
            str_password = getIntent().getStringExtra("pw");
            new connection_test().execute();
        }
    });
}

异步任务

    private class connection_test extends AsyncTask<String, Void, String> {

    protected void onPreExecute(){
        dialog = ProgressDialog.show(connected_download.this, null, "Erstelle Vorschau. Bitte warten...");
    }

    protected String doInBackground(String... connection) {
                listftpitems();
            return null;
    }

    protected void onPostExecute(String result) {
        dialog.dismiss();
    }
}

列表文件

    public void listftpitems(){
    try {
        ftpClient.setConnectTimeout(15000);
        Integer int_port = Integer.parseInt(str_port);
        ftpClient.connect(InetAddress.getByName(str_server), int_port);
        ftpClient.login(str_user, str_password);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        System.out.println("status :: " + ftpClient.getStatus());
        System.out.println("FTPClient : " + "Offen");

    } catch (Exception e) {
        e.printStackTrace();
    } 

    final ArrayList<String> your_array_list = new ArrayList<String>();
    final ListView lv = (ListView) findViewById(R.id.list_download);
    final FTPFile[] files;
    try {

        files = ftpClient.listFiles();

        for (FTPFile file : files) {
            String details = file.getName();
            your_array_list.add(details);
            Log.v("string", details);
        }

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(connected_download.this,android.R.layout.simple_list_item_1, your_array_list);

        runOnUiThread(new Runnable() {
            public void run() {
            lv.setAdapter(arrayAdapter); 
            }
        });



    }catch (IOException e1) {
        e1.printStackTrace();
    }

}

LogCat(查看 Log.v("string", details); on listftpitems)所以它是正确的

12-05 11:34:24.650: D/AbsListView(23249): Get MotionRecognitionManager
12-05 11:34:24.650: V/string(23249): test6
12-05 11:34:24.695: D/dalvikvm(23249): GC_FOR_ALLOC freed 64K, 9% free 13420K/14599K, paused 13ms, total 15ms
12-05 11:34:24.945: D/dalvikvm(23249): GC_CONCURRENT freed 29K, 8% free 13813K/14983K, paused 11ms+5ms, total 28ms
12-05 11:34:25.480: I/System.out(23249): status :: 211-Status of 'ProFTPD'
12-05 11:34:25.480: I/System.out(23249):  Connected from 213.55.184.193 (213.55.184.193)
12-05 11:34:25.480: I/System.out(23249):  Logged in as ftp031220
12-05 11:34:25.480: I/System.out(23249):  TYPE: BINARY, STRUcture: File, Mode: Stream
12-05 11:34:25.480: I/System.out(23249):  No data connection
12-05 11:34:25.480: I/System.out(23249): 211 End of status
12-05 11:34:25.480: I/System.out(23249): FTPClient : Offen
12-05 11:34:26.095: V/string(23249): anon_ftp
12-05 11:34:26.095: V/string(23249): bin
12-05 11:34:26.095: V/string(23249): cgi-bin
12-05 11:34:26.095: V/string(23249): conf
12-05 11:34:26.095: V/string(23249): error_docs
12-05 11:34:26.095: V/string(23249): httpdocs
12-05 11:34:26.095: V/string(23249): httpsdocs
12-05 11:34:26.095: V/string(23249): mivadata
12-05 11:34:26.095: V/string(23249): pd
12-05 11:34:26.095: V/string(23249): private
12-05 11:34:26.095: V/string(23249): statistics
12-05 11:34:26.095: V/string(23249): subdomains
12-05 11:34:26.095: V/string(23249): vault_scripts
12-05 11:34:26.095: V/string(23249): web_users

我手机里的照片

在此处输入图像描述

4

3 回答 3

2

您正在以错误的方式在当前代码中执行所有操作,就像您尝试使用runOnUiThreadfrom doInBackground.change 您的代码一样:


将 listftpitems 更改为:

public ArrayList<String> listftpitems(){
try {
        ArrayList<String> your_array_list = new ArrayList<String>();
        ftpClient.setConnectTimeout(15000);
        Integer int_port = Integer.parseInt(str_port);
        ftpClient.connect(InetAddress.getByName(str_server), int_port);
        ftpClient.login(str_user, str_password);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        System.out.println("status :: " + ftpClient.getStatus());
        System.out.println("FTPClient : " + "Offen");

    } catch (Exception e) {
        e.printStackTrace();
    } 
    final FTPFile[] files;
    try {
        files = ftpClient.listFiles();
        for (FTPFile file : files) {
            String details = file.getName();
            your_array_list.add(details);
            Log.v("string", details);
        }
    }catch (IOException e1) {
        e1.printStackTrace();
    }

return your_array_list;
}


将 AsyncTask 更改为:

private class connection_test extends AsyncTask<String, Void, String> {
   ArrayList<String> temparrlist = new ArrayList<String>();
    protected void onPreExecute(){
        dialog = ProgressDialog.show(connected_download.this, null, 
                                 "Erstelle Vorschau. Bitte warten...");
    }

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

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


将您的活动 onCreate 更改为:

ListView lv ;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connected_download);
    Log.v("string", "test6");

            str_profil = getIntent().getStringExtra("profil");
            str_server = getIntent().getStringExtra("server");
            str_port = getIntent().getStringExtra("port");
            str_user = getIntent().getStringExtra("user");
            str_password = getIntent().getStringExtra("pw");
        lv= (ListView) findViewById(R.id.list_download);
            new connection_test().execute("");
}


并请从以下位置阅读一些 Android 基础知识:

http://developer.android.com/guide/components/index.html

于 2012-12-05T10:53:44.823 回答
1

您不必创建ArrayList内部AsyncTask,而是在内部onPostExecute()方法中添加项目。

试试下面的代码片段。

 private class connection_test extends AsyncTask<String, Void, String> {

    protected void onPreExecute(){
        dialog = ProgressDialog.show(connected_download.this, null, "Erstelle Vorschau. Bitte warten...");
    }

    protected String doInBackground(String... connection) {
                //Here you will get your arraylist with the Method listftpitems()
                your_array_list= listftpitems();
            return null;
    }

    protected void onPostExecute(String result) {
        dialog.dismiss();
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(connected_download.this,android.R.layout.simple_list_item_1, your_array_list);
   setListAdapter(arrayAdapter);
    }
}

这是你的方法。

public ArrayList<String> listftpitems(){
try {
        ArrayList<String> your_array_list = new ArrayList<String>();
        ftpClient.setConnectTimeout(15000);
        Integer int_port = Integer.parseInt(str_port);
        ftpClient.connect(InetAddress.getByName(str_server), int_port);
        ftpClient.login(str_user, str_password);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        System.out.println("status :: " + ftpClient.getStatus());
        System.out.println("FTPClient : " + "Offen");

    } catch (Exception e) {
        e.printStackTrace();
    } 
    final FTPFile[] files;
    try {
        files = ftpClient.listFiles();
        for (FTPFile file : files) {
            String details = file.getName();
            your_array_list.add(details);
            Log.v("string", details);
        }
    }catch (IOException e1) {
        e1.printStackTrace();
    }

return your_array_list;
}
于 2012-12-05T11:00:40.637 回答
0

只有一个

"V/string(23249): test6" 

log,这意味着你的循环只运行一次。检查是否

ftpClient.listFiles();

返回多个文件。

于 2012-12-05T10:51:00.773 回答