3

I am filling a ListView with ArrayAdapter in an AsyncTask. But everytime it only fills in one item.

Could someone explain me why?

onCreate

    @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();
        }
    });
}

AsyncTask

    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();
    }
}

listftpitems

    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 (take a look to the Log.v("string", details); on listftpitems) so its correct

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

picture from my phone

enter image description here

Marco Seiz
  • 944
  • 5
  • 19
  • 40

3 Answers3

2

you are doing everything in current code with wrong way like you are trying to Use runOnUiThread from doInBackground .change your code as:


Change listftpitems as:
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;
}


Change AsyncTask as:
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();
    }
}


Change your Activity onCreate as:
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("");
}


and plz read some basic of Android from :

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

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

you don't have to create the ArrayList inside AsyncTask but adding items to it inside onPostExecute() method.

try it the below code snippet.

 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);
    }
}

Here it your Method.

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;
}
Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
0

There is only one

"V/string(23249): test6" 

log, which means your loop runs only once. Check if

ftpClient.listFiles();

returns more than one file.

asloob
  • 1,308
  • 20
  • 34
  • no, my loop doesn't runs only one time... `Log.v("string", "test6");` is only run one time. you have to look to the `Log.v("string", details); ` on listftpitems function – Marco Seiz Dec 05 '12 at 10:54