我在AsyncTask中使用ArrayAdapter填充ListView。 但每次它只填充一个项目。
有人能解释我为什么吗?
的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 (看看Log.v(“字符串”,详细信息);在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
手机中的图片
答案 0 :(得分:2)
您正在使用当前代码执行所有操作,例如您尝试使用runOnUiThread
中的doInBackground
。将您的代码更改为:
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("");
}
<小时/> 并且plz从以下网站阅读了一些基本的Android:
答案 1 :(得分: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;
}
答案 2 :(得分:0)
只有一个
"V/string(23249): test6"
log,表示你的循环只运行一次。检查是否
ftpClient.listFiles();
返回多个文件。