如何在显示活动之前显示进度条

时间:2013-10-02 18:27:45

标签: android

下面是我的java代码和xml代码。我想在显示任何数据之前显示进度条。我怎样才能做到这一点?我使用asyntask来加载数据。如何在我的代码中显示进度条,以便在加载数据之前首先显示进度条。加载数据后,将删除进度条。

public class fifthscreen extends Activity {
    String num = null;
    TextView ingredient;

long Menu_ID;
String dish_name;
View row4;

View row3;

String status;

HorizontalListView listview;
CategoryListAdapter3 cla;
String DescriptionAPI;

TextView txt1, txt2, txt3;
ImageView img1;
String URL, URL2;
String SelectMenuAPI;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();
public static String allergen2;
private AQuery androidAQuery;
String name;

String description;
String url1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fifthscreen);

    ingredient = (TextView) findViewById(R.id.ingredient);
    img1 = (ImageView) findViewById(R.id.test_button_image);

    txt1 = (TextView) findViewById(R.id.menuname);
    txt3 = (TextView) findViewById(R.id.description);

    Intent iGet = getIntent();

    ImageView options = (ImageView) findViewById(R.id.options5);
    androidAQuery = new AQuery(this);


    options.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent iMenuList = new Intent(fifthscreen.this,
                    LinkButtons.class);
            startActivity(iMenuList);
        }
    });

    dish_name = iGet.getStringExtra("dish_name");

    listview = (HorizontalListView) this.findViewById(R.id.listview2);

    cla = new CategoryListAdapter3(fifthscreen.this);

    new getDataTask().execute();
    ImageView btnback = (ImageView) findViewById(R.id.btnback);

    btnback.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });



}

void clearData() {
    Category_ID.clear();
    Category_name.clear();
    Category_image.clear();

}

public class getDataTask extends AsyncTask<Void, Void, Void> {

    getDataTask() {

    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        parseJSONData();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        txt1.setText(name);
        txt3.setText(description);
        androidAQuery.id(img1).image(url1, false, false);
        listview.setAdapter(cla);

    }
}

public void parseJSONData() {

    SelectMenuAPI = Utils.dishdescription + dish_name;

    clearData();
    URL = SelectMenuAPI;
    URL2 = URL.replace(" ", "%20");

    try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams
                .setConnectionTimeout(client.getParams(), 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(URL2);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                atomInputStream),8192);

        String line;
        String str = "";
        while ((line = in.readLine()) != null) {
            str += line;
        }

        JSONObject json2 = new JSONObject(str);

        status = json2.getString("status");
        if (status.equals("1")) {

            JSONArray school2 = json2.getJSONArray("data");

            for (int i = 0; i < school2.length(); i++) {

                name = school2.getJSONObject(0).getString("name");

                description = school2.getJSONObject(0).getString(
                        "description");

                url1 = school2.getJSONObject(0).getString("image");

            }

            JSONObject school3 = json2.getJSONObject("dish_nutrition");

final TableLayout table = (TableLayout) findViewById(R.id.table2);

            for (int j = 0; j < school3.length(); j++) {

                String s = String.valueOf(j + 1);

    row3 = getLayoutInflater().inflate(R.layout.rows, null);
    ((TextView) row3.findViewById(R.id.localTime))

   .setText(school3.getJSONObject(s).getString("qty"));
((TextView) row3.findViewById(R.id.apprentTemp))

  .setText(school3.getJSONObject(s).getString("name"));



                fifthscreen.this.runOnUiThread(new Runnable() {
                    public void run() {
                        table.addView(row3);
                    }
                });

            }

            JSONArray school4 = json2.getJSONArray("dish_allergen");
            //
            for (int i = 0; i < school4.length(); i++) {
                JSONObject object = school4.getJSONObject(i);

                Category_ID.add((long) i);
                Category_name.add(object.getString("name"));
                Category_image.add(object.getString("image"));

            }

            final LinearLayout table3 = (LinearLayout)  
    findViewById(R.id.table3);

            JSONArray school5 = json2.getJSONArray("dish_ingredient");

            for (int i = 0; i < school5.length(); i++) {

row4 = getLayoutInflater().inflate(R.layout.row2, null);
((TextView)  row4.findViewById(R.id.name)).setText(school5

      .getJSONObject(i).getString("name"));
                ((TextView) row4.findViewById(R.id.subingredients))
                        .setText(school5.getJSONObject(i).getString(
                                "sub_ingredients"));

                // table3.addView(row2);

                fifthscreen.this.runOnUiThread(new Runnable() {
                    public void run() {
                        table3.addView(row4);
                    }
                });

            }

        }

        else {

            JSONArray school2 = json2.getJSONArray("data");
            for (int i = 0; i < school2.length(); i++) {
                JSONObject object = school2.getJSONObject(i);

                Category_ID.add((long) i);
                Category_name.add(object.getString("name"));

            }

        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    //   IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

   }



      <?xml version="1.0" encoding="UTF-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#E5E5E5"
 android:orientation="vertical" >

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/layoutbordercolor" >

        <ImageView
            android:id="@+id/btnback"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:paddingLeft="3dp"
            android:paddingTop="15dp"
            android:src="@drawable/back"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/bitmap"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dp"
            android:layout_weight="1.5"
            android:src="@drawable/logostar" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingTop="12dp"
                android:text="San Diego Unified"
                android:textColor="#603311"
                android:textSize="14sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:text="SCHOOL DISTRICT"
                android:textColor="#603311"
                android:textSize="10sp" />
        </LinearLayout>

        <ImageView
            android:id="@+id/options"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:paddingBottom="5dp"
            android:paddingTop="10dp"
            android:src="@drawable/options1" />
    </LinearLayout>
  </LinearLayout>



 <LinearLayout
    android:id="@+id/lytContent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:background="@drawable/editbackground"
        android:paddingBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="8dp"
            android:paddingBottom="2dp"
            android:src="@drawable/search_btn" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="fill_parent"
            android:layout_height="36dp"
            android:layout_toLeftOf="@+id/imageView1"
           android:layout_marginTop="5dp"
           android:paddingLeft="10dp"
           android:layout_marginLeft="10dp"
           android:paddingRight="5dp"
           android:gravity="left"
            android:background="#FFF"
            android:hint="  Search School by name"
              android:imeOptions="actionSearch"
          android:paddingTop="10dp"
            android:textSize="15dp" >
        </EditText>
    </RelativeLayout>


      <LinearLayout
        android:id="@+id/lytContent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"


        android:orientation="vertical" >

    <TextView
        android:id="@+id/pleaseselect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:paddingLeft="15dp"
        android:text=" Select a school type"
        android:textColor="#0A2A1B"
        android:textSize="18sp"
        android:textStyle="bold"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/lytContent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

        android:layout_marginTop="10dp"
    android:background="@drawable/layout_border"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listMainMenuhome"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
              android:paddingTop="3dp"
   android:paddingBottom="3dp"
   android:paddingLeft="3dp"
   android:paddingRight="3dp"
            android:dividerHeight="1dip"
            android:fadeScrollbars="true"
          android:listSelector="@color/listview_selector"

            android:visibility="gone" />
    </LinearLayout>

    <!--   android:listSelector="@drawable/listviewborder" -->


    <ProgressBar
        android:id="@+id/prgLoading"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

          />

    <TextView
        android:id="@+id/txtAlert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:paddingLeft="10dp"
        android:text="@string/alert"
        android:textColor="@color/text"
        android:textSize="14sp"
        android:visibility="gone" />



    </LinearLayout>



  </LinearLayout>

   </LinearLayout>

3 个答案:

答案 0 :(得分:1)

你可以制作一个覆盖整个屏幕的相对布局,它的背景和中间的进度条,在你所有的活动内容之上,一旦你拥有了你需要的所有信息,你就可以忽略那个布局覆盖整个活动布局,这将成功。这不是一种干净的方法,但是常用。

为了做得更好,您可以创建一个完全独立的布局,只需将进度作为活动的一部分,直到您收集内容数据所需的所有信息,只需对其进行充气并显示,您的进度条应该可以重复使用,这样你就可以随时显示和消除进度......

希望这有助于。

问候!

答案 1 :(得分:0)

http://developer.android.com/reference/android/widget/ProgressBar.html

有一个例子。看起来完全像你需要做的

答案 2 :(得分:0)

试试这段代码。如果有效,请告诉我。

ProgressDialog mProgressDialog;
@Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(fifthscreen.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Your Title");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        parseJSONData();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        txt1.setText(name);
        txt3.setText(description);
        androidAQuery.id(img1).image(url1, false, false);
        listview.setAdapter(cla);
        mProgressDialog.dismiss();
    }