2016-09-24 34 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);