我正在尝试显示“进度”对话框,而我的listView(用于显示设备上的所有应用程序)正在加载。
但它没有出现。
这是我的主要活动课程以及我尝试过的内容。
问题是什么?
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
ProgressDialog dlg;
private class Updator extends AsyncTask<Void, Void, Void>
{
public Updator()
{
dlg = new ProgressDialog(MainActivity.this);
dlg.setMessage("Text");
dlg.setTitle("Title");
}
@Override
protected void onPreExecute()
{
dlg.show();
}
protected Void doInBackground(Void... unused) {
runOnUiThread(new Runnable() {
public void run()
{
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
ArrayList<ApplicationInfo> installedApp = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : apps)
{
installedApp.add(app);
}
ListView lv = (ListView) findViewById(R.id.listView1);
//custom list is a class to create a custom list view.
//it also will load application icons into list(which needs time)
CustomList adapter = new CustomList(MainActivity.this, installedApp);
TextView t = (TextView) findViewById(R.id.textView1);
t.setText(installedApp.size());
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//some things app will do
}
});
}
});
return (null);
}
protected void onPostExecute(Void unused)
{
dlg.dismiss();
}
}
public void onBtnClick(View v)
{
new Updator().execute();
}
}
答案 0 :(得分:0)
你应该改变你的asynctask看起来像这样:
private class Updator extends AsyncTask<Void, Void, ArrayList<ApplicationInfo>> {
public Updator()
{
dlg = new ProgressDialog(MainActivity.this);
dlg.setMessage("Text");
dlg.setTitle("Title");
}
@Override
protected void onPreExecute()
{
dlg.show();
}
@Override
protected ArrayList<ApplicationInfo> doInBackground(Void... params) {
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
ArrayList<ApplicationInfo> installedApp = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : apps)
{
installedApp.add(app);
}
return installedApp;
}
@Override
protected void onPostExecute(ArrayList<ApplicationInfo> installedApps) {
ListView lv = (ListView) findViewById(R.id.listView1);
//custom list is a class to create a custom list view.
//it also will load application icons into list(which needs time)
CustomList adapter = new CustomList(MainActivity.this, installedApp);
TextView t = (TextView) findViewById(R.id.textView1);
t.setText(installedApp.size());
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//some things app will do
}
});
dlg.dismiss();
}
}
如果在主线程上的doInBackground中执行所有计算,则它不会显示微调器。 正如在一些评论中指出的那样,最好在后台进行计算,然后将该信息发送到onPostExecute。