[英]Updating a ListView on MainActivity from Asynctask
I have a list view on the main Ui that I wish to update through an Asynctask which connects to a Mikrotik device to retrieve details. 我在主Ui上有一个列表视图,我希望通过Asynctask更新该列表,该Asynctask连接到Mikrotik设备以检索详细信息。
I am passing the activity too, so that I can update the UI from a separate class file, however I keep getting the error "Class name expected here" for the activity (mActivity) in the ListAdapter constructor. 我也传递了活动,以便可以从单独的类文件更新UI,但是在ListAdapter构造函数中,我不断收到活动(mActivity)的错误“期望的类名在这里”。
Here is the Asynctask code: 这是Asynctask代码:
public class MikrotikReader extends AsyncTask<String, Void, ArrayList<String>> {
//Default connection details for mikrotik
private int mkPort = 8728;
private int mkTimeout = 5000;
private String userName;
private String userPass;
private String mkIpAddress;
private String mkCommand;
private ListView listView;
private Activity mActivity;
public MikrotikReader(String user, String pass, String ipaddress, String command, Activity activity) {
this.userName = user;
this.userPass = pass;
this.mkIpAddress = ipaddress;
this.mkCommand = command;
this.mActivity = activity;
}
@Override
protected ArrayList<String> doInBackground(String... params) {
ArrayList<String> mylist = new ArrayList<String>();
try {
ApiConnection con = ApiConnection.connect(SocketFactory.getDefault(), mkIpAddress, mkPort, mkTimeout);
con.login(userName, userPass); // log in to router
if (con.isConnected()) {
List<Map<String, String>> rs1 = con.execute(mkCommand);
for (Map<String, String> results1 : rs1) {
getResults[0] = new String(results1.get("comment"));
mylist.add(getResults[0]);
}
con.close();
}
} catch (MikrotikApiException e) {
} catch (Throwable e) {
e.printStackTrace();
}
return mylist;
}
@Override
protected void onPostExecute(ArrayList<String> mylist) {
System.out.println("This is the array content " + mylist.get(0));
listView = (ListView) mActivity.this.findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter<String>(mActivity.this,android.R.layout.simple_list_item_1,new ArrayList<String>())); }
This is how I call the Asynctask from the MainActivity class file, passing "this" as the activity: 这是我从MainActivity类文件调用Asynctask的方式,将“ this”作为活动传递:
new MikrotikReader(user_name, password, ipaddress,"/ip/firewall/filter/print where comment=Test", this).execute();
Here is the listview in the mainactivity.xml file: 这是mainactivity.xml文件中的列表视图:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_bg" />
尝试在您的AsyncTask类中仅使用mActivity而不是mActivity.this
Don't do like this. 不要这样 Make interface like
使界面像
public interface ListPreparingListener {
void onListPrepared(ArrayList<String> mylist);
}
And implement it on your main activity like 并在您的主要活动上实施
@Override
onListPrepared(ArrayList<String> myList) {
listView.setAdapter(new ArrayAdapter<String>(mActivity.this,android.R.layout.simple_list_item_1,new ArrayList<String>()));
}
Then, pass this interface to you reader's constructor and call this method on post execute 然后,将此接口传递给读者的构造函数,并在执行后调用此方法
Upd: Actually it could produce memory leak, if you store a reference to your activity inside other objects. Upd:实际上,如果将对活动的引用存储在其他对象中,则可能会导致内存泄漏。 The most proper way is to make methods
subscribeListener
and unsubsucribeListener
and subscribe when you need it from main activity, unsubscribe onStop
. 最合适的方法是使方法
subscribeListener
和unsubsucribeListener
并在需要时从主要活动中进行订阅,即取消订阅onStop
。 Also on your onPostExecute should be check if there are available listeners 另外在onPostExecute上应该检查是否有可用的侦听器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.