为什么进展没有表现出来

时间:2014-04-19 10:01:10

标签: android http progress-bar

我通过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);
        }

    }
}

我在代码中做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity {
    private ListView listView1;
    private String str = null;
    private Classification[] classification_datas;
    private ProgressDialog pd;

并删除

private ProgressDialog pd; 
来自asyncTask的

并查看它是否适合您。并将您的进度对话框初始化为

pd = new ProgressDialog(MainActivity.this);
pd.setCancelable(true);
pd.setTitle("Please wait!");
pd.setMessage("Connecting...");
pd.show();

preExecuteMethod