2016-09-24 75 views
0

我想爲個人TextView擁有單獨的進度條。例如,每個TextView都顯示一些我用來從我的數據庫中查詢的信息。我可以使用AsyncTask,並且在postExecute我知道我可以顯示我想要顯示的TextView信息。但是,問題是,當頁面加載以從我的數據庫獲取不同信息時,我正在進行多個異步調用,並且我想爲顯示信息的TextView中的每一個顯示一個ProgressBar。在我的數據庫中有TextView信息可用之前,是否有任何方法可以進行ProgressBar展示?我只是想知道如何進行數據庫調用。如何爲單獨的TextView使用單獨的進度條

我知道我可以用我的AsyncTask做到這一點:

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

     //showDialog() here; 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 

     //do what you want here// 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 

     //dismissDialog() here;// 


    } 

但後來它好像如果我做showDialog(),我知道我必須創建4個不同的ProgressBar在我的XML,但隨後又怎麼會有我確保他們只會在我的TextView謊言,而不是整個屏幕上?

+0

是TextView的確定與旁邊的進度條的項目要求? – user1506104

+0

@ user1506104我想要顯示進度條,直到抓取數據庫中的信息。然後我想將進度條的可見性設置爲消失,並顯示信息準備好顯示時進度條所在的「TextView」 – user1871869

回答

0

可以顯示每個TextView中的旁邊有一個進度條,顯示該領域仍在加載數據:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <ProgressBar 
     android:id="@+id/progressbar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:indeterminate="true" /> 
</LinearLayout> 

一旦你準備好一個TextView的數據,你可以調用它的對象對應的setVisibility(View.GONE)視圖。您應該從您的onPostExecute()中調用它。

0

佈局文件(activity_main.xml中)可能是這個樣子:

enter image description here

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 


<LinearLayout 
    android:layout_marginTop="50dp" 
    android:background="@android:color/black" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="50dp" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textSize="18dp" 
     android:textColor="@android:color/white" 
     android:text="contents of textview 1" /> 

    <ProgressBar 
     android:id="@+id/progressbar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

<LinearLayout 
    android:background="@android:color/black" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textSize="18dp" 
     android:textColor="@android:color/white" 
     android:text="contents of textview 2" /> 

    <ProgressBar 
     android:id="@+id/progressbar2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 


</LinearLayout> 

然後在你的活動,聲明如下:

ProgressBar progressBar1,progressBar2; 

和活動的方法OnCreate()

progressBar1=(ProgressBar)findViewById(R.id.progressbar1); 
progressBar2=(ProgressBar)findViewById(R.id.progressbar2); 

,最後,在你onPostExecute()方法,隱藏相應的進度:

progressBar1.setVisibility(View.GONE); 

或/和,

progressBar2.setVisibility(View.GONE);