0
我通過php腳本和httpclient在客戶端從網站獲取數據。數據加載時,我想在操作欄中顯示進度條。問題是進度條沒有顯示。代碼如下 -爲什麼進度沒有顯示
public class MainActivity extends Activity {
private ListView listView1;
private String str = null;
private Classification[] classification_datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
ActionBarUtils.setActionBar(this);
AsyncData data=new AsyncData();
data.execute("http://kurdshopping.net/apj/category.php");
try {
classification_datas = data.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
ClassificationAdapter adapter=new ClassificationAdapter(this, R.layout.listview_item_row, classification_datas);
listView1=(ListView) findViewById(R.id.listViewAllItems);
View header=(View) getLayoutInflater().inflate(
R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent subcategory = new Intent(MainActivity.this,
SubCategoryActivity.class);
subcategory
.putExtra("id", classification_datas[position - 1].id);
startActivity(subcategory);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
break;
case R.id.action_all:
Intent allitemIntent = new Intent(MainActivity.this,
AllItemsActivity.class);
startActivity(allitemIntent);
break;
case R.id.action_search:
Intent search = new Intent(this, SearchDialogActivity.class);
startActivity(search);
break;
}
return super.onOptionsItemSelected(item);
}
private class AsyncData extends AsyncTask<String, Void, Classification[]>{
private ProgressDialog pd;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd=ProgressDialog.show(MainActivity.this, "Loading. Please wait", "Loading Please wait");
pd.show();
setProgressBarIndeterminateVisibility(true);
}
@Override
protected Classification[] doInBackground(String... arg0) {
final Classification classification_data[] = new Classification[12];
try {
str = CustomHttpClient
.executeHttpGet(arg0[0]);
// str = CustomHttpClient
// .executeHttpGet("http://kurdshopping.net/apj/category.php");
JSONArray array = null;
array = new JSONArray(str);
JSONObject jdata = null;
jdata = array.getJSONObject(0);
classification_data[0] = new Classification(R.drawable.motor_icon,
jdata.getString("name")
/* res.getString(R.string.Motor) */, jdata.getInt("id"));
jdata = array.getJSONObject(1);
classification_data[1] = new Classification(R.drawable.electronics,
jdata.getString("name")
/* res.getString(R.string.Electronics) */,
jdata.getInt("id"));
jdata = array.getJSONObject(2);
classification_data[2] = new Classification(
R.drawable.property_icon, jdata.getString("name")
/* res.getString(R.string.Property) */, jdata.getInt("id"));
jdata = array.getJSONObject(3);
classification_data[3] = new Classification(R.drawable.animal_icon,
jdata.getString("name")
/* res.getString(R.string.Animals) */, jdata.getInt("id"));
jdata = array.getJSONObject(4);
classification_data[4] = new Classification(R.drawable.house_icon,
jdata.getString("name")
/* res.getString(R.string.House) */, jdata.getInt("id"));
jdata = array.getJSONObject(5);
classification_data[5] = new Classification(R.drawable.gold,
jdata.getString("name")/* res.getString(R.string.Gold) */,
jdata.getInt("id"));
jdata = array.getJSONObject(6);
classification_data[6] = new Classification(
R.drawable.farming_icon, jdata.getString("name")
/* res.getString(R.string.Farming) */, jdata.getInt("id"));
jdata = array.getJSONObject(7);
classification_data[7] = new Classification(
R.drawable.bussines_icon, jdata.getString("name")
/* res.getString(R.string.Business) */, jdata.getInt("id"));
jdata = array.getJSONObject(8);
classification_data[8] = new Classification(R.drawable.sports_icon,
jdata.getString("name")
/* res.getString(R.string.Sports) */, jdata.getInt("id"));
jdata = array.getJSONObject(9);
classification_data[9] = new Classification(
R.drawable.music_icon,
jdata.getString("name")/* res.getString(R.string.Music) */,
jdata.getInt("id"));
jdata = array.getJSONObject(10);
classification_data[10] = new Classification(
R.drawable.fashion_icon, jdata.getString("name")
/* res.getString(R.string.Fashion) */, jdata.getInt("id"));
jdata = array.getJSONObject(11);
classification_data[11] = new Classification(R.drawable.jobs_icon,
jdata.getString("name")/* res.getString(R.string.Jobs) */,
jdata.getInt("id"));
} catch (NotFoundException e) {
} catch (JSONException e) {
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return classification_data;
}
@Override
protected void onPostExecute(Classification[] result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
setProgressBarIndeterminateVisibility(false);
}
}
}
我在做我的代碼錯了嗎?提前致謝。
它沒有初始化進度對話框,
。其實我添加了這些行來看看這個方法是否有效。但setProgressBarIndeterminateVisibility(true)正是我所問的。這應該在操作欄中顯示進度條。 –
它產生了什麼異常? 「logcat」在說什麼? –
在'setProgressBarIndeterminateVisibility(true);'後面使用'setProgressBarVisibility(true);'看看它是否適合你。 –