gpt4 book ai didi

java - 如何从另一个类中的 AsyncTask 调用 notificationDataSetChanged()

转载 作者:行者123 更新时间:2023-12-01 17:23:41 25 4
gpt4 key购买 nike

因此,我尝试调用 notifyDataSetChanged() 来在 OnPostExecute 完成时更新 ArrayList 但是,我发现 AsyncTask 位于另一个类中很难做到这一点。我也无法在 setpagecontent 中调用它,因为它是静态方法。

我感谢提供的所有帮助:)

最佳答案

PostListActivity.java

 public class PostListActivity extends AppCompatActivity {
ArrayList<String> Posts_Array_List = new ArrayList<String>();
// private RequestQueue mQueue;
ArrayAdapter<String> PostsAdaptor;
ListView lv;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_list);
mContext=PostListActivity.this;
lv = (ListView) findViewById(R.id.LV_Post_list);

PostsAdaptor = new ArrayAdapter<String>(PostListActivity.this, android.R.layout.simple_list_item_1, Posts_Array_List);
PostsAdaptor.notifyDataSetChanged();
lv.setAdapter(PostsAdaptor);
String stringUrl = "https://www.reddit.com/r/cars/hot.json?limit=1";

new DownloadAsyncTask(mContext,stringUrl).execute();
Bundle extras = getIntent().getExtras();

if (extras != null) {
String myParam = extras.getString("paramPosition");
// Get the URL from the UI's text field.
}
}

public void setPageContent(String thePageContent) {
System.out.println("Got: " + thePageContent);
String jsonFromReddit = thePageContent;
//ArrayList<String> posts = RedditPostHelper.getRedditPostsFromJSON(jsonFromReddit);
// Posts_Array_List.addAll(posts);
Posts_Array_List.add(thePageContent);
System.out.println("result==="+thePageContent);
PostsAdaptor.notifyDataSetChanged();
}
}

下载AsyncTask.java

 public class DownloadAsyncTask extends AsyncTask<Void,Void,String>{
private Context context;
private String url;
DownloadAsyncTask(Context mContext, String stringUrl)
{
super();
this.context=mContext;
this.url=stringUrl;
}

@Override
protected String doInBackground(Void... voids) {
String result="testing";
return result;
}
@Override
protected void onPostExecute(String result) {
if (!result.isEmpty() && result !=null){
PostListActivity pla = (PostListActivity) context;
pla.setPageContent(result);
}

}

输出:

enter image description here }

关于java - 如何从另一个类中的 AsyncTask 调用 notificationDataSetChanged(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61243658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com