2013-10-02 3 views
-2

Ниже приведен код java и xml-код. Я хочу показать индикатор выполнения перед показом любых данных. Как я могу это сделать? Im использует asyntask для загрузки данных. Как я покажу прогрессбар в моем коде, чтобы сначала показать progressbar, пока не будут загружены данные. Когда данные загружаются, индикатор выполнения удаляется.как показать индикатор выполнения перед показом активности

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> 
+0

Вы просто вложили весь проект? : P –

ответ

0

Вы можете сделать относительный макет, охватывающий весь экран, его фоном и индикатором выполнения в середине, помимо всего, что вы занимаетесь активностью, как только у вас есть вся информация, которую вы нужно, вы можете просто отбросить этот макет, наложенный на весь макет деятельности, и это сделало бы трюк. Это не чистый подход, но обычно используется.

Чтобы сделать что-то лучше, вы могли бы создать совершенно отдельный макет и просто выполнить прогресс как часть действия, пока не соберете всю информацию, необходимую для вашего контента. Данные просто раздуйте его и покажите, ваш индикатор выполнения должны быть повторно использованы, чтобы вы могли показывать и отклонять прогресс, когда вам это нужно ...

Надеюсь, что это поможет.

С уважением!

+0

дайте какой-нибудь пример, что вы говорите – user2762662

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(); 
    } 
+0

, но я не хочу показывать какую-либо компоновку перед загрузкой данных, например ic_launcher на imageview – user2762662

+0

Вы имеете в виду экран Android Splash? –

+0

нет нет мой пятый шаг экрана после четырехчасового шага, поэтому я хочу, когда я нажимаю сначала показывать только прогрессбар, когда заканчивается, а затем показывает acticvity – user2762662

Смежные вопросы