首先抱歉,如果它是重复的,因为我问它是因为无法理解其他问题的任何内容,或者我可以说我无法在我的情况下实施。
实际上我遇到了这样一种情况:我希望用ListView
中的网络更新我的AsyncTask
,但是现在如何实现它,不知道我尝试了一些但不能制作的东西这行得通。请任何人详细解释我如何做,这是我的活动代码和Async类
package app.balaji.sid.listapp;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPFile;
public class ListActivity extends ActionBarActivity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
lv=(ListView)findViewById(R.id.listView);
String[] values = new String[] { "Item 1",
"Item 2",
"Item 3 ",
"Item 4",
"Item 5",
"Item 6",
"Item 7",
"Item 8"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
lv.setAdapter(adapter);
new ListFetcher().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected class ListFetcher extends AsyncTask<String,String,String>{
String sid;
@Override
protected String doInBackground(String... params) {
String user = "user";
String pass="password";
String host="204.236.238.164";
FTPClient client = new FTPClient();
try {
client.connect(host,21);
client.login(user, pass);
client.setType(FTPClient.TYPE_BINARY);
client.changeDirectory("/");
FTPFile[] files = client.list();
String[] fileNames = new String[files.length];
for (int i = 0; i < files.length; i++)
{
fileNames[i] = files[i].getName();
String logVariable=fileNames[i];
Log.v(sid, logVariable); // Here i am able to see list in log
}
}catch (Exception e)
{
Log.v(sid,e.toString());
}
return sid;
}
protected void onPostExecute()
{
//i think list should be updated from here but don't know how to do it correctly
// i just know i can update my listview by creating a new adapter and assign it to listview but don't know how
// also i am not able to get the fileNames array here
}
}
}
答案 0 :(得分:0)
如果您发布了您尝试过的欺骗行为的链接并解释您不理解的内容,那么回答会更容易。什么是/不会发生也会有所帮助。但是,从我所看到的情况来看,您只需要将新的String
传递给onPostExecute()
并更新列表并在那里Adapter
。
protected void onPostExecute(String result) // get the String you are passing
{
//i think list should be updated from here but don't know how to do it correctly
// I agree. Let's try this
if(result != null) // let's make sure you got something
{
values.add(result); // add the new String to your list
adapter.notifyDataSetChanged(); // Hey, Adapter, we've got some new data
}
您需要将Array
更改为ArrayList
,以便长度可以是动态的。您还需要创建它和您的Adapter
成员变量,以便内部类可以看到它们。
public class ListActivity extends ActionBarActivity {
ListView lv;
// Declare them here
ArrayAdapter<String> adapter;
ArrayList<String> values = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
lv=(ListView)findViewById(R.id.listView);
values.add("Item 1");
values.add("Items 2");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
android.R.id.text1, values);
lv.setAdapter(adapter);
new ListFetcher().execute();
您没有将任何内容传递给您的任务,但您想要返回一个数组
@Override
protected String[] doInBackground(Void... params) {
protected void onPostExecute(String[] result)
如果要为适配器返回一组全新的值,则可以使用新值更新它。如果您只是添加一些值,那么您可以迭代它们并将它们添加到ArrayList
并调用notifyDataSetChanged()
,如上所示。
答案 1 :(得分:0)
我刚刚解决了我的问题,所以只是想我需要分享它,所以其他面临同样问题的人也可以轻松解决。
首先,在我的问题中,我刚刚用字符串参数声明了我的AsyncTask类,但实际上我需要从这里填充我的ListView。
所以我做了什么:
protected class ListFetcher extends AsyncTask<ArrayList,ArrayList,ArrayList>{
String sid;
ArrayList list;
@Override
protected ArrayList doInBackground(ArrayList... params) {
FTPClient client = new FTPClient();
try {
client.connect(host,21);
client.login(user, pass);
client.setType(FTPClient.TYPE_BINARY);
client.changeDirectory("/");
FTPFile[] files = client.list();
String[] fileNames = new String[files.length];
for (int i = 0; i < files.length; i++)
{
fileNames[i] = files[i].getName();
String logVariable=fileNames[i];
Log.v(sid, logVariable);
values.add(fileNames[i]);
}
}catch (Exception e)
{
Log.v(sid,e.toString());
}
return list;
}
protected void onPostExecute(final ArrayList result)//received result but didn't use it because in my case i don't need it instead of it i just updated my arraylist in doInbackground and updated my ui from here
{
adapter = new ArrayAdapter<String>(ListActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
ListView listView =(ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
}