Android:在AsyncTask中使用ArrayAdapter填充ListView->;仅显示一项
我正在异步任务中用ArrayAdapter填充ListView。 但每次只填写一项 有人能解释一下为什么吗 onCreateAndroid:在AsyncTask中使用ArrayAdapter填充ListView->;仅显示一项,android,listview,android-asynctask,android-arrayadapter,Android,Listview,Android Asynctask,Android Arrayadapter,我正在异步任务中用ArrayAdapter填充ListView。 但每次只填写一项 有人能解释一下为什么吗 onCreate @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.connected_download); Log.v("string", "test6");
@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();
}
}
我手机上的图片
@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();
}
});
}
只有一个
"V/string(23249): test6"
日志,这意味着您的循环只运行一次。检查是否
ftpClient.listFiles();
返回多个文件。您在当前代码中以错误的方式执行所有操作,就像您试图从
doInBackground
使用runOnUiThread
一样。将代码更改为:
将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;
}
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();
}
}
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的基本知识: 您不必在
AsyncTask
内创建ArrayList
,而是添加
在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);
}
}
私有类连接\u测试扩展了异步任务{
受保护的void onPreExecute(){
dialog=ProgressDialog.show(已连接的_download.this,null,“Erstelle Vorschau.bite warten…”);
}
受保护的字符串背景(字符串…连接){
//在这里,您将使用listftpitems()方法获得arraylist
您的_数组_list=listftpitems();
返回null;
}
受保护的void onPostExecute(字符串结果){
dialog.dismise();
final ArrayAdapter ArrayAdapter=新的ArrayAdapter(已连接的下载。这是android.R.layout.simple_list_项目1,您的_数组_列表);
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;
}
public ArrayList listftpitems(){
试一试{
ArrayList您的_array_list=新建ArrayList();
ftpClient.setConnectTimeout(15000);
整数int_port=Integer.parseInt(str_port);
ftpClient.connect(InetAddress.getByName(str_服务器)、int_端口);
ftpClient.login(str_用户,str_密码);
ftpClient.setFileType(FTP.BINARY文件类型);
ftpClient.enterLocalPassiveMode();
System.out.println(“状态::”+ftpClient.getStatus());
System.out.println(“FTPClient:“+”Offen”);
}捕获(例外e){
e、 printStackTrace();
}
最终FTPFile[]文件;
试一试{
files=ftpClient.listFiles();
用于(FTPFile文件:文件){
字符串详细信息=file.getName();
您的数组列表。添加(详细信息);
Log.v(“字符串”,详细信息);
}
}捕获(IOE1异常){
e1.printStackTrace();
}
返回您的数组列表;
}
不,我的循环不会只运行一次<代码>Log.v(“字符串”,“test6”)代码>仅运行一次。您必须查看Log.v(“字符串”,详细信息)代码>在listftpitems函数上它不工作,因为“返回类型与AsyncTask不兼容意味着只需将AsyncTask
更改为AsyncTask
,这肯定会起作用,但我没有运行您的代码,因此可能不会出现问题,但这是在您的应用程序中使用AsyncTask的正确方法。感谢您,所以代码没有崩溃或其他问题。但我的列表中仍然只有一项:-/这是一个布局错误。。。感谢您的帮助和演示如何正确使用asynctask:)
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;
}