如何为单独的TextView具有单独的进度条

时间:2016-09-24 18:02:47

标签: android android-asynctask android-progressbar

我想拥有个人TextView的个人进度条。例如,每个TextView显示我用于从数据库查询的某种信息。我可以使用AsyncTaskpostExecute我知道我可以显示我要显示的TextView信息。但是,问题是,当页面加载以从我的数据库中获取不同的信息时,我正在进行多次异步调用,并且我想为显示信息的每个ProgressBar显示一个TextView。有没有办法在我的数据库中提供TextView信息之前制作ProgressBar节目?我只是想知道UI,因为我知道如何进行数据库调用。

我知道我可以使用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(),我知道我必须在我的XML中创建4个不同的ProgressBar,但是我如何确保它们只会在TextView 1}}谎言而不是整个屏幕?

2 个答案:

答案 0 :(得分: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()

中拨打电话

答案 1 :(得分: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);