如何显示在一个活动的进度条(圈)加载数据的列表视图之前有一个列表视图 [英] how to show progress bar(circle) in an activity having a listview before loading the listview with data

查看:145
本文介绍了如何显示在一个活动的进度条(圈)加载数据的列表视图之前有一个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 在我的第二个 activity.OnItemClick 的它,我叫一个Web服务,并试图获取数据。在这之后,我换了第三个活动也有一个的ListView 为previous活动说明的ListView 项目

我想在组装前,显示进度对话框这个的ListView

我不明白怎么做,在的ListView ?有谁知道该怎么办呢?

我的code -

ThirdActivity.java

 包com.google.iprotect;

进口java.io.IOException异常;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.Map;

进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;
进口org.xmlpull.v1.XmlPullParserException;

进口com.google.iprotect.layout.TitleBarLayout;

进口android.app.Activity;
进口android.app.ListActivity;
进口android.app.ProgressDialog;
进口android.content.Intent;
进口android.content.res.ColorStateList;
进口android.content.res.XmlResourceParser;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.AdapterView;
进口android.widget.LinearLayout;
进口android.widget.ListView;
进口android.widget.ProgressBar;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.TextView;

公共类ThirdActivity扩展ListActivity实现OnItemClickListener {

    JSONArray jArray1,jArray2;
    串一,二,三,表名;
    串色,R;
    的JSONObject responseJSON;
    TitleBarLayout titlebarLayout;
    最后的ArrayList<表> ArrayList的=新的ArrayList<表>();
    TextView的tablename;如果
    ColorStateList colorStateList1;
    字符串email1,密码1;
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.thirdactivity);

        ListView的LV = getListView();
        lv.setOnItemClickListener(本);


        表名= getIntent()getExtras()的getString(表名)。;
        email1 = getIntent()getExtras()的getString(电子邮件)。;
        密码1 = getIntent()getExtras()的getString(密码)。;

        titlebarLayout =新TitleBarLayout(ThirdActivity.this);
        titlebarLayout.setLeftButtonText(回去);
        titlebarLayout.setRightButtonText(注销);
        titlebarLayout.setTitle(表名);
        titlebarLayout.setLeftButtonSize(70,40);
        titlebarLayout.setRightButtonSize(70,40);
        //titlebarLayout.setLeftButtonLeftDrawable(R.drawable.refresh);

        //titlebarLayout.setRightButtonLeftDrawable(R.drawable.buttonrefresh);
        //titlebarLayout.setLeftButtonBackgroundColor(Color.rgb(255,255,255));
        //titlebarLayout.setRightButtonBackgroundColor(Color.rgb(34,49,64));
        //titlebarLayout.setLeftButtonTextColor(Color.rgb(255,255,255));
        //titlebarLayout.setRightButtonTextColor(Color.rgb(255,255,0));

        XmlResourceParser parser1 = getResources()的getXML(R.color.colorstatelist)。


        尝试 {
            colorStateList1 = ColorStateList.createFromXml(getResources(),parser1);
            titlebarLayout.setRightButtonTextColor(colorStateList1);
        }赶上(XmlPullParserException E){
            e.printStackTrace();
        }赶上(IOException异常E){

            e.printStackTrace();
        }

        OnClickListener监听器=新OnClickListener(){

            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                如果(v.getId()== R.id.left_button){
                    意向意图=新的意图(ThirdActivity.this,SecondActivity.class);
                    intent.putExtra(电子邮件,email1);
                    intent.putExtra(密码,密码1);
                    startActivity(意向);
                    完();

                }否则,如果(v.getId()== R.id.right_button){
                    意向意图=新的意图(ThirdActivity.this,
                            MainActivity.class);
                    //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    ThirdActivity.this.startActivity(意向);

                }
            }
        };
        titlebarLayout.setLeftButtonOnClickListener(听众);
        titlebarLayout.setRightButtonOnClickListener(听众);


        updateTableList();




    }

    私人无效updateTableList(){
        // TODO自动生成方法存根
        //最后ProgressDialog PD1 = ProgressDialog.show(这一点,叫web服务,请稍候,真,假);

        最终进度pbHeaderProgress =(进度)findViewById(R.id.pbHeaderProgress);

        新的AsyncTask<虚空,虚空,虚空>(){


            在preExecute保护无效(){
                // TODO自动生成方法存根
                super.on preExecute();
                pbHeaderProgress.setVisibility(View.VISIBLE);
            }



            保护无效doInBackground(虚空...... PARAMS){
                R = invokeWebService1(表名);
                //pd1.dismiss();

                尝试 {
                    responseJSON =新的JSONObject(R);
                    // JSON阅读
                    jArray1 = responseJSON.getJSONArray(FirstThree); //获取JSONArray jArray1从JSONObject的名称为FirstThree
                    jArray2 = responseJSON.getJSONArray(颜色); //获取JSONArray jArray2从JSONOobject与名称颜色
                    JSONObject的json_data1 = NULL;
                    JSONObject的json_data2 = NULL;

                    的for(int i = 0; I< jArray1.length();我++){
                        json_data1 = jArray1.getJSONObject(我); //从JSONArray JSONObject的json_data1索引i;
                        1 = json_data1.getString(1); //获得价值从JSONObject的json_data1与关键一
                        2 = json_data1.getString(二);
                        3 = json_data1.getString(三公);
                        json_data2 = jArray2.getJSONObject(ⅰ);
                        颜色= json_data2.getString(颜色); //获得价值从JSONObject的json_data2与关键色

                        表表=新表();
                        //设置值表类
                        tables.column1 =之一;
                        tables.column2 =两个;
                        tables.column3 =三个;
                        tables.tableName =表名;
                        tables.color =颜色;
                        //将表添加到对象的ArrayList<表>
                        arraylist.add(表);

                        Log.i(ONE,json_data1.getString(1));
                        Log.i(二,json_data1.getString(二));
                        Log.i(三化,json_data1.getString(三公));
                        Log.i(色,+ json_data2.getString(颜色));
                    }

                }赶上(JSONException E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
                返回null;
            }

            保护无效onPostExecute(无效的结果){
                pbHeaderProgress.setVisibility(View.GONE);

                //自定义适配器的ListView
                TableDetailAdapter适配器=新TableDetailAdapter(ThirdActivity.this,
                        R.layout.table_data_list_item,数组列表);
                setListAdapter(适配器);
            }
        }。执行();

    }

    保护字符串invokeWebService1(字符串tablename2){
        // TODO自动生成方法存根
        串响应=;
        尝试 {
            WebService的web服务=新的WebService(
            ?http://sphinx-solution.com/iProtect/api.php);

            //如果需要传递的参数
            地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
            params.put(行动,getTableRecords);
            params.put(表名,tablename2);
            params.put(电子邮件,email1);
            params.put(密码,密码1);

            //从服务器的都是其中的方法名称JSON响应
            如果需要,例如//通常会去
            响应= webService.WebGet(身份验证,则params);

        }赶上(例外五){
            Log.d(错误,e.getMessage());
        }
        返回响应;
    }


    公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,长ARG3){
        // TODO自动生成方法存根

        Log.v(点击列表项数+位置);
        意向意图=新的意图(ThirdActivity.this,FourthActivity.class);
        intent.putExtra(JSON的,responseJSON.toString()); //发送JSON对象为一个字符串下一个活动
        intent.putExtra(表名,表名);
        intent.putExtra(电子邮件,email1);
        intent.putExtra(密码,密码1);
        intent.putExtra(编号,位置);
        startActivity(意向);

    }

}
 

thirdactivity.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / linlaHeaderProgress
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    工具:上下文=ThirdActivity。>

    <包括
        机器人:ID =@ + ID /标题栏
        布局=@布局/ titlebar_layout/>

    <进度
        机器人:ID =@ + ID / pbHeaderProgress
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =中心
        机器人:layout_weight =2>
    < /进度>

    <的ListView
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =match_parent
        机器人:layout_height =260dp
        机器人:layout_weight =5.04>
    < / ListView控件>

    <的LinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =@扪/ titlebar_height
        机器人:layout_alignParentBottom =真
        机器人:背景=@色/ footer_bg_color
        机器人:重力=底
        机器人:方向=横向>

        <包括
            机器人:ID =@ + ID /页脚
            机器人:layout_height =@扪/ titlebar_height
            机器人:layout_gravity =底部| center_horizo​​ntal
            布局=@布局/ footer_layout/>
    < / LinearLayout中>

< / LinearLayout中>
 

解决方案

有显示进度条(圈),在加载活动的几种方法。在你的情况下,人们在它的ListView

在动作条

如果您使用的是动作条,您可以调用进度这样的(这可能进入你的的onCreate()

  requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(真正的);
 

和你做显示列表后,将其隐藏。

  setProgressBarIndeterminateVisibility(假);
 

在布局(该XML)

 <的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_weight =1
    机器人:方向=垂直>

    <的LinearLayout
        机器人:ID =@ + ID / linlaHeaderProgress
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:重力=中心
        机器人:方向=垂直
        机器人:能见度=水涨船高>

        <进度
            机器人:ID =@ + ID / pbHeaderProgress
            机器人:风格=@风格/微调
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT>
        < /进度>
    < / LinearLayout中>

    <的ListView
        机器人:ID =@ + ID /列表
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_weight =1
        机器人:cacheColorHint =@机器人:彩色/透明
        机器人:分隔=#00000000
        机器人:dividerHeight =0dp
        机器人:fadingEdge =无
        机器人:persistentDrawingCache =滚动
        机器人:smoothScrollbar =假>
    < / ListView控件>
< / LinearLayout中>
 

而在你的活动(JAVA) 我使用的AsyncTask来获取数据,我的名单。所以,在AsyncTask的的在preExecute()我用的是这样的:

  //投的LinearLayout控股的主要进展(旋转)
的LinearLayout linlaHeaderProgress =(的LinearLayout)findViewById(R.id.linlaHeaderProgress);

@覆盖
在preExecute保护无效(){
    //显示当装载喂SPINNER
    linlaHeaderProgress.setVisibility(View.VISIBLE);
}
 

onPostExecute(),设置适配器连接到的ListView 之后:

  @覆盖
保护无效onPostExecute(无效的结果){
    //设置适配器到ListView
    lv.setAdapter(适配器);

    //改变的LOADINGMORE状态,以允许获取更多的数据
    loadingMore = FALSE;

    //隐藏载荷送入后,SPINNER
    linlaHeaderProgress.setVisibility(View.GONE);
}
 

编辑:这就是它会在我的应用程序加载时的几个中的一个列表视图

I have a ListView in my second activity.OnItemClick of it I called a webservice and trying to fetch data. And after that I am moving to third activity which also have a ListView having description of previous activities ListView item.

I want to display a progress dialog before populating this ListView.

I don't understand how to do it on ListView? Does anybody know how to do it?

My Code-

ThirdActivity.java

   package com.google.iprotect;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;

import com.google.iprotect.layout.TitleBarLayout;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.XmlResourceParser;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class ThirdActivity extends ListActivity implements OnItemClickListener{

    JSONArray jArray1,jArray2;
    String one,two,three,tablename;
    String color,r;
    JSONObject responseJSON;
    TitleBarLayout titlebarLayout;
    final ArrayList<Tables> arraylist = new ArrayList<Tables>();
    TextView tableName;
    ColorStateList colorStateList1;
    String email1,password1;
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thirdactivity);

        ListView lv=getListView();
        lv.setOnItemClickListener(this);


        tablename=getIntent().getExtras().getString("Table Name");
        email1 = getIntent().getExtras().getString("email");
        password1 =getIntent().getExtras().getString("password");

        titlebarLayout = new TitleBarLayout(ThirdActivity.this);
        titlebarLayout.setLeftButtonText("go Back");
        titlebarLayout.setRightButtonText("Logout");
        titlebarLayout.setTitle(tablename);
        titlebarLayout.setLeftButtonSize(70,40);
        titlebarLayout.setRightButtonSize(70,40);
        //titlebarLayout.setLeftButtonLeftDrawable(R.drawable.refresh);

        //titlebarLayout.setRightButtonLeftDrawable(R.drawable.buttonrefresh);
        //titlebarLayout.setLeftButtonBackgroundColor(Color.rgb(255,255,255));
        //titlebarLayout.setRightButtonBackgroundColor(Color.rgb(34,49,64));
        //titlebarLayout.setLeftButtonTextColor(Color.rgb(255,255,255));
        //titlebarLayout.setRightButtonTextColor(Color.rgb(255,255,0));     

        XmlResourceParser parser1 =getResources().getXml(R.color.colorstatelist);


        try {
            colorStateList1 = ColorStateList.createFromXml(getResources(), parser1);
            titlebarLayout.setRightButtonTextColor(colorStateList1);
        } catch (XmlPullParserException e) {    
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        OnClickListener listener = new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (v.getId() == R.id.left_button) {
                    Intent intent = new Intent(ThirdActivity.this,SecondActivity.class);
                    intent.putExtra("email", email1);
                    intent.putExtra("password", password1);
                    startActivity(intent);
                    finish();

                } else if (v.getId() == R.id.right_button) {
                    Intent intent = new Intent(ThirdActivity.this,
                            MainActivity.class);
                    //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    ThirdActivity.this.startActivity(intent);

                }
            }
        };
        titlebarLayout.setLeftButtonOnClickListener(listener);
        titlebarLayout.setRightButtonOnClickListener(listener);


        updateTableList();




    }

    private void updateTableList() {
        // TODO Auto-generated method stub
        //final ProgressDialog pd1=ProgressDialog.show(this, "Calling Webservice", "Waiting...", true, false);

        final ProgressBar pbHeaderProgress = (ProgressBar) findViewById(R.id.pbHeaderProgress);

        new AsyncTask<Void, Void, Void>() {


            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                pbHeaderProgress.setVisibility(View.VISIBLE);
            }



            protected Void doInBackground(Void... params) {
                r = invokeWebService1(tablename);
                //pd1.dismiss();

                try {
                    responseJSON = new JSONObject(r);
                    //json reading
                    jArray1 = responseJSON.getJSONArray("FirstThree");//get JSONArray jArray1 from JSONObject with name FirstThree
                    jArray2 = responseJSON.getJSONArray("Color");//get JSONArray jArray2 from JSONOobject with name Color
                    JSONObject json_data1 = null;
                    JSONObject json_data2 = null;

                    for (int i = 0; i < jArray1.length(); i++) {
                        json_data1 = jArray1.getJSONObject(i);//get JSONObject json_data1 from JSONArray at index i;
                        one = json_data1.getString("One");//get value from JSONObject json_data1 with key "One"
                        two = json_data1.getString("Two");
                        three = json_data1.getString("Three");
                        json_data2 = jArray2.getJSONObject(i);
                        color = json_data2.getString("color");//get value from JSONObject json_data2 with key "color"

                        Tables tables = new Tables();
                        //set value to Tables Class
                        tables.column1 = one;
                        tables.column2 = two;
                        tables.column3 = three;
                        tables.tableName=tablename;
                        tables.color=color;
                        //add Tables object into ArrayList<Tables>
                        arraylist.add(tables);

                        Log.i("ONE", json_data1.getString("One"));
                        Log.i("TWO", json_data1.getString("Two"));
                        Log.i("THREE", json_data1.getString("Three"));
                        Log.i("color",""+ json_data2.getString("color"));
                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            protected void onPostExecute(Void result) {
                pbHeaderProgress.setVisibility(View.GONE);

                //Custom Adapter for ListView
                TableDetailAdapter adaptor = new TableDetailAdapter(ThirdActivity.this,
                        R.layout.table_data_list_item, arraylist);
                setListAdapter(adaptor);
            }
        }.execute();

    }

    protected String invokeWebService1(String tablename2) {
        // TODO Auto-generated method stub
        String response = "";
        try {
            WebService webService = new WebService(
            "http://sphinx-solution.com/iProtect/api.php?");

            // Pass the parameters if needed
            Map<String, String> params = new HashMap<String, String>();
            params.put("action", "getTableRecords");
            params.put("tablename", tablename2);
            params.put("email", email1);
            params.put("password", password1);

            // Get JSON response from server the "" are where the method name
            // would normally go if needed example
            response = webService.WebGet("auth", params);

        } catch (Exception e) {
            Log.d("Error: ", e.getMessage());
        }
        return response;
    }


    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub

        Log.v("", "Click ListItem Number "+position);
        Intent intent = new Intent(ThirdActivity.this,FourthActivity.class);
        intent.putExtra("Json", responseJSON.toString());//sending json Object as a string to next activity
        intent.putExtra("Table Name", tablename);
        intent.putExtra("email", email1);
        intent.putExtra("password", password1);
        intent.putExtra("Item No", position);
        startActivity(intent);

    }

}

thirdactivity.xml

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

    <include
        android:id="@+id/titlebar"
        layout="@layout/titlebar_layout" />

    <ProgressBar
        android:id="@+id/pbHeaderProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="2" >
    </ProgressBar>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="260dp" 
        android:layout_weight="5.04">
    </ListView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="@dimen/titlebar_height"
        android:layout_alignParentBottom="true"
        android:background="@color/footer_bg_color"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <include
            android:id="@+id/footer"
            android:layout_height="@dimen/titlebar_height"
            android:layout_gravity="bottom|center_horizontal"
            layout="@layout/footer_layout" />
    </LinearLayout>

</LinearLayout>

解决方案

There are several methods of showing a progress bar (circle) while loading an activity. In your case, one with a ListView in it.

IN ACTIONBAR

If you are using an ActionBar, you can call the ProgressBar like this (this could go in your onCreate()

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
setProgressBarIndeterminateVisibility(true);

And after you are done displaying the list, to hide it.

setProgressBarIndeterminateVisibility(false);

IN THE LAYOUT (The XML)

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linlaHeaderProgress"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone" >

        <ProgressBar
            android:id="@+id/pbHeaderProgress"
            android:style="@style/Spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>
    </LinearLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:cacheColorHint="@android:color/transparent"
        android:divider="#00000000"
        android:dividerHeight="0dp"
        android:fadingEdge="none"
        android:persistentDrawingCache="scrolling"
        android:smoothScrollbar="false" >
    </ListView>
</LinearLayout>

And in your activity (Java) I use an AsyncTask to fetch data for my lists. SO, in the AsyncTask's onPreExecute() I use something like this:

// CAST THE LINEARLAYOUT HOLDING THE MAIN PROGRESS (SPINNER)
LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);

@Override
protected void onPreExecute() {    
    // SHOW THE SPINNER WHILE LOADING FEEDS
    linlaHeaderProgress.setVisibility(View.VISIBLE);
}

and in the onPostExecute(), after setting the adapter to the ListView:

@Override
protected void onPostExecute(Void result) {     
    // SET THE ADAPTER TO THE LISTVIEW
    lv.setAdapter(adapter);

    // CHANGE THE LOADINGMORE STATUS TO PERMIT FETCHING MORE DATA
    loadingMore = false;

    // HIDE THE SPINNER AFTER LOADING FEEDS
    linlaHeaderProgress.setVisibility(View.GONE);
}

EDIT: This is how it looks in my app while loading one of several ListViews

这篇关于如何显示在一个活动的进度条(圈)加载数据的列表视图之前有一个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆