我需要顯示一個循環ProgressBar,直到3個AsynchTasks完成。如何在3個AsynchTasks完成後隱藏ProgressBar並顯示ListView
我無法達到結果。
請任何幫助表示讚賞。
public class MainActivity extends Activity {
public ArrayList<String> files=new ArrayList<String>();
public String mQuery=null;
public File root=Environment.getRootDirectory();
public ArrayAdapter<String> ad;
public ArrayList<String> queries= new ArrayList<String>(Arrays.asList(".prc",".mobi",".epub"));
public ProgressBar pb;
public ListView lv;
public LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll=(LinearLayout)findViewById(R.id.layout);
pb =(ProgressBar)findViewById(R.id.progress);
lv=(ListView)findViewById(R.id.listView1);
}
protected void onResume(){
super.onResume();
ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
lv.setAdapter(ad);
for(int i=0;i<queries.size();i++){
AddStringTask as= new AddStringTask();
try {
mQuery=queries.get(i);
as.execute(Environment.getRootDirectory());
} catch (Exception e) {
e.printStackTrace();
}
}
//pb.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
for(int i1=0;i1<files.size();i1++){
ad.add(files.get(i1));
}
ad.notifyDataSetChanged();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String path =(String) lv.getItemAtPosition(position);
if((!path.endsWith(".prc"))) {
Intent vi=new Intent();
vi.setAction(Intent.ACTION_VIEW);
vi.setData(Uri.parse("file://" +path));
vi.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(vi);
}
if((path.endsWith(".prc"))) {
String bookID=path.substring(path.length()-19, path.length()-9);
Intent vi=new Intent();
vi.setAction(Intent.ACTION_VIEW);
Uri file = Uri.parse("kindle://book/?action=open&book_id=AMZNID0/" + bookID + "/0/");
vi.setData(file);
vi.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(vi);
}
}
});
}
class AddStringTask extends AsyncTask<File, Void, Void> {
@Override
protected void onPreExecute(){
pb.setVisibility(View.VISIBLE);
//lv.setVisibility(View.INVISIBLE);
}
@Override
protected Void doInBackground(File... root) {
search(root[0]);
return (null);
}
@Override
protected void onProgressUpdate(Void... unused) {
}
@Override
protected void onPostExecute(Void unused) {
Toast.makeText(MainActivity.this, "DONE", Toast.LENGTH_LONG)
.show();
pb.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
ll.invalidate();
}
}
這是我的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>