【问题标题】:How do I call notifyDataSetChanged() from a AsyncTask that is in another class如何从另一个类中的 AsyncTask 调用 notifyDataSetChanged()
【发布时间】:2020-04-16 06:01:46
【问题描述】:

所以我试图在OnPostExecute 完成后调用notifyDataSetChanged() 来更新ArrayList 但是我发现AsyncTask 在另一个类中很难做到。我也不能在setpagecontent 中调用它,因为它是一个静态方法。

感谢提供的所有帮助:)

【问题讨论】:

  • 使用 eventBus 或监听器。
  • 您可以为 ArrayAdapter PostsAdaptor 创建一个静态变量。并调用 PostsAdaptor.notifyDataSetChanged();在 setPageContent() 中。所以代码如下: public static void setPageContent(String thePageContent) { System.out.println("Got: " + thePageContent); String jsonFromReddit = thePageContent; ArrayList 帖子 = RedditPostHelper.getRedditPostsFromJSON(jsonFromReddit); Posts_Array_List.addAll(posts); PostsAdaptor.notifyDataSetChanged() }
  • @Priyanka 感谢您的回答能否请您将其添加为答案,以便我能够以正确的语法查看代码:)
  • @Priyanka 我将如何制作静态变量?
  • 请检查以下代码...

标签: java android


【解决方案1】:

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);
    }

}

输出:

}

【讨论】:

  • 错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ArrayAdapter.notifyDataSetChanged()”
  • 同样的错误 IDK 怎么办
  • 否则在 setPageContent() 中再次调用列表.. 我更新了代码
  • PostListActivity.this 不能从静态方法调用
  • 否则,我们可以使用静态上下文,并将其传递给 ArraryAdapter..
【解决方案2】:

可能会对代码进行一些更改,但对于类间通信,您可以使用接口并将其函数用作回调

【讨论】:

  • 抱歉,对于 android 和 java 来说都是新手,一般来说,我该怎么做呢?
  • 好的让我们试试这个......使用异步任务的后执行方法......在这里你应该能够通知适配器有关更改
  • 这不相关,因为类不同并且是静态的
  • 我的 ArrayAdapter 在另一个类中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多