我正在制作显示应用程序列表的应用程序 这是我的代码:-
MainActivity.java
import android.app.*;
import android.content.*;
import android.content.pm.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.util.*;
import android.graphics.drawable.*;
import android.content.pm.PackageManager.*;
public class MainActivity extends Activity
{
ProgressDialog pd;
List<Drawable> ld;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pd = new ProgressDialog(this);
pd.setTitle("Loading...");
pd.setCancelable(true);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.create();
pd.show();
}
@Override
protected void onResume()
{
super.onResume();
createlist();
}
public void createlist(){
ld=new ArrayList<Drawable>();
List<String> pl= new ArrayList<String>();
PackageManager pm= getPackageManager();
List<ApplicationInfo> l = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo ai:l){
String name= ai.packageName.toString();
pl.add(name);
try
{
Drawable d = getApplicationContext().getPackageManager().getApplicationIcon(name);
ld.add(d);
}
catch (PackageManager.NameNotFoundException e)
{}
}
ListView lv = (ListView) findViewById(R.id.mainListView1);
ArrayAdapter aa= new MyAda(this,pl);
lv.setAdapter(aa);
//pd.dismiss();
}
class MyAda extends ArrayAdapter{
public MyAda(Context c,List val){
super(c,R.layout.list,val);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater li= LayoutInflater.from(getApplicationContext());
View view = li.inflate(R.layout.list,null);
TextView tv = (TextView) view.findViewById(R.id.listTextView1);
tv.setText(getItem(position).toString());
ImageView iv = (ImageView) view.findViewById(R.id.listImageView1);
iv.setImageDrawable(ld.get(position));
return view;
}
}
}
当应用程序列表完全加载时,我的应用程序显示进度对话框 ,但是我想在加载应用程序列表时显示进度对话框 这需要更多时间才能启动应用
请告诉我在列表加载数据时显示进度对话框的正确方法
答案 0 :(得分:1)
移动创建列表函数以分隔线程,您可以为此使用异步任务, 在onPreExecute中显示前移对话框,然后在onPost execute中关闭它。
将以下代码粘贴到您的活动中,它应该可以正常工作。
ProgressDialog pd;
List<Drawable> ld;
List<String> pl;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
pd = new ProgressDialog(this);
pd.setTitle("Loading...");
pd.setCancelable(true);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.create();
}
@Override
protected void onResume()
{
super.onResume();
new AsyncTask<Void,Void,Void>(){
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.show();
}
@Override
protected Void doInBackground(Void... voids) {
createlist();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pd.dismiss();
ListView lv = (ListView) findViewById(R.id.listView);
ArrayAdapter aa= new MyAda(Main3Activity.this,pl);
lv.setAdapter(aa);
}
}.execute();
}
public void createlist(){
ld=new ArrayList<Drawable>();
pl= new ArrayList<String>();
PackageManager pm= getPackageManager();
List<ApplicationInfo> l = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo ai:l){
String name= ai.packageName.toString();
pl.add(name);
try
{
Drawable d = getApplicationContext().getPackageManager().getApplicationIcon(name);
ld.add(d);
}
catch (PackageManager.NameNotFoundException e)
{}
}
}
class MyAda extends ArrayAdapter{
public MyAda(Context c, List val){
super(c,R.layout.list,val);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater li= LayoutInflater.from(getApplicationContext());
View view = li.inflate(R.layout.list,null);
TextView tv = (TextView) view.findViewById(R.id.txt);
tv.setText(getItem(position).toString());
ImageView iv = (ImageView) view.findViewById(R.id.iv);
iv.setImageDrawable(ld.get(position));
return view;
}
}
您没有在代码中看到进度对话框,因为您正在主线程中显示进度对话框和获取应用程序数据,主线程一次只能做一件事。将繁重的工作移至后台线程始终是一个好习惯,以实现流畅的UI和性能优势。 另外,异步任务不是执行多线程的bset方法,请参考https://medium.com/@maheswaranapk/android-threading-introduction-90e91e196600
答案 1 :(得分:0)
尝试将createlist();
移到pd.show();
下方并删除onResume
答案 2 :(得分:0)
'将进度对话框代码添加到诸如此类的createlist()函数中
public void createlist(){
pd = new ProgressDialog(this);
pd.setTitle("Loading...");
pd.setCancelable(true);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.create();
pd.show();
ld=new ArrayList<Drawable>();
List<String> pl= new ArrayList<String>();
PackageManager pm= getPackageManager();
List<ApplicationInfo> l = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo ai:l){
String name= ai.packageName.toString();
pl.add(name);
try
{
Drawable d = getApplicationContext().getPackageManager().getApplicationIcon(name);
ld.add(d);
}
catch (PackageManager.NameNotFoundException e)
{}
}
ListView lv = (ListView) findViewById(R.id.mainListView1);
ArrayAdapter aa= new MyAda(this,pl);
lv.setAdapter(aa);
pd.dismiss();
}
“在此功能结束时关闭对话框”
“希望您能得到答案”