如何向此添加ProgressDialog

时间:2015-10-27 02:18:09

标签: java android progressdialog

我正在填充来自RecyclerView的{​​{1}}并且在同一个帖子上,我希望添加一个Parse.com,直到数据完全加载为止。但是,很困惑,如何我能做到吗。

以下是目前的代码:

ProgressDialog

所以我从方法 public class BigBoard extends ActionBarActivity { private List<Person> persons; private RecyclerView rv; RVAdapter adapter; String a,b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Parse.initialize(this, "app-id", "client-key"); setContentView(R.layout.activity_big_board); initializeData(); // <----- This is where initialization begins adapter = new RVAdapter(persons); rv=(RecyclerView)findViewById(R.id.rv); LinearLayoutManager llm = new LinearLayoutManager(this); rv.setLayoutManager(llm); rv.setHasFixedSize(true); initializeAdapter(); } private void initializeData(){ persons = new ArrayList<>(); ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Credentials"); query.findInBackground(new FindCallback<ParseObject>() { public void done(List<ParseObject> credentialList, ParseException e) { if (e == null) { for(int i=0;i<credentialList.size();i++) { a=credentialList.get(i).getString("Name"); b=credentialList.get(i).getString("SurName"); persons.add(new Person(a,b)); Log.d("OUT", "So the Val::------> " +a +b); adapter.notifyDataSetChanged(); } } else { Log.d("score", "Error: " + e.getMessage()); } } }); //persons.add(new Person("Emma Wilson", "23 years old")); //persons.add(new Person("Lavery Maiss", "25 years old")); //persons.add(new Person("Lillie Watts", "35 years old")); } private void initializeAdapter(){ rv.setAdapter(adapter); } } 将数据加载到列表中,是否可以运行initializeData()直到完成?或者我应该去ProgressDialog实施它?任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

您现在正在做的事情是不正确的,因为您实际上正在解析UI线程上的n / w数据。即使您显示ProgressDialog,也会造成糟糕的用户体验,因为ProgressDialog动画不会很流畅,因为您的UI线程在initializeData()方法中常常忙碌

您应该仅在initializeData()中使用AsyncTask方法。在此期间,请从ProgressDialog方法中显示onCreate()。当您的initializeData()方法完成后,请关闭ProgressDialog