从数据库加载数据时添加进度条

时间:2016-10-01 08:31:47

标签: java android progress-bar android-volley

我使用volley库从数据库中获取数据我使用此代码

public class R_arabic extends AppCompatActivity {

RequestQueue requestQueue;
ListView listView;

ArrayList<listitem_gib> listitems = new ArrayList<listitem_gib>();
String name, img, url, num;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_r_arabic);

    listView = (ListView) findViewById(R.id.listView);
    TextView textView_Title = (TextView) findViewById(R.id.textView2);

    Intent intent = getIntent();
    String story_type = intent.getStringExtra("story_type");

    switch (story_type) {
        case "arabic":
            textView_Title.setText("arabic");
            break;
        case "romance":
            textView_Title.setText("romance");
            break;
        case "motrgm":
            textView_Title.setText("motrgm");
            break;
        case "ro3b":
            textView_Title.setText("ro3b");
            break;
        case "siasa":
            textView_Title.setText("siasa");
            break;

    }

    String url = "http://grassyhat.com/android/" + story_type + ".php";


    requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {
                public void onResponse(JSONObject response) {

                    try {
                        JSONArray jsonArray = response.getJSONArray("all");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject respons = jsonArray.getJSONObject(i);
                            String id = respons.getString("id");
                            String name = respons.getString("name");
                            String img = respons.getString("img");
                            String url = respons.getString("url");
                            String num = respons.getString("num");
                            listitems.add(new listitem_gib(id, name, img, url, num));

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    listAllItme();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("VOLLEY", "ERROR");
        }
    }

    );
    requestQueue.add(jsonObjectRequest);

}


public void listAllItme() {
    ListAdapter lA = new listAdapter(listitems);
    listView.setAdapter(lA);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckInternetConnection cic = new CheckInternetConnection(getApplicationContext());
            Boolean Ch = cic.isConnectingToInternet();
            if (!Ch) {
                Toast.makeText(R_arabic.this, "no connection", Toast.LENGTH_LONG).show();
            } else {
                Intent open = new Intent(R_arabic.this, rewaya_show.class);
                open.putExtra("name", listitems.get(position).name);
                open.putExtra("url", listitems.get(position).url);
                open.putExtra("img", listitems.get(position).img);
                open.putExtra("num", listitems.get(position).num);

                startActivity(open);

                showad++;
                if (showad >= 5) {
                    showad = 0;
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                    }
                }
            }
        }
    });

}



class listAdapter extends BaseAdapter {
    ArrayList<listitem_gib> lista = new ArrayList<listitem_gib>();

    public listAdapter(ArrayList<listitem_gib> lista) {
        this.lista = lista;
    }

    @Override
    public int getCount() {
        return lista.size();
    }

    @Override
    public Object getItem(int position) {
        return lista.get(position).name;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.row_item_gib, null);

        TextView name = (TextView) view.findViewById(R.id.textView_gib);
        ImageView img = (ImageView) view.findViewById(R.id.imageView_gib);
        TextView num = (TextView) view.findViewById(R.id.textView_gib2);


        name.setText(lista.get(position).name);
        num.setText(lista.get(position).num);

        Picasso.with(R_arabic.this).load("http://grassyhat.com/android/image/" + lista.get(position).img).into(img);


        return view;
    }
}

我想在加载数据时添加进度条以避免空白页

抱歉,我是Android的新手,我谷歌为此而且没有得到有用的答案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kamal.ahmed.rewaya.R_arabic"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="@drawable/bg"
    tools:showIn="@layout/app_bar_r_arabic">

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adView">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="40sp"
            android:textStyle="bold"
            android:textColor="#e873400c"
            android:layout_gravity="center"
            android:id="@+id/textView2" />

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:divider="@drawable/div1"
            android:dividerHeight="35dp" />

    </LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/progress_layout"
    android:visibility="gone">

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="100dp"
        android:layout_marginBottom="60dp"
        android:layout_weight="1"/>

    <TextView
        android:text="Download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progress_txt"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="#e873400c"
        android:layout_gravity="center"
        android:layout_marginRight="90dp"
        android:layout_marginBottom="60dp"
        android:layout_weight="1" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

progressBar

中添加activity_r_arabic
<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

在活动中添加ProgessBar progressBar;作为全局变量

并将其初始化为

progressBar = (ProgessBar) findViewById(R.id.progress_bar);

然后在onResponse(JSONObject response)方法中添加以下行

progressBar.setVisibility(View.GONE)

修改

让你的linearLayout在xml中可见

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/progress_layout"
    android:visibility="visible">

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="100dp"
        android:layout_marginBottom="60dp"
        android:layout_weight="1"/>

    <TextView
        android:text="Download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progress_txt"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="#e873400c"
        android:layout_gravity="center"
        android:layout_marginRight="90dp"
        android:layout_marginBottom="60dp"
        android:layout_weight="1" />

</LinearLayout>

并在onResponse(JSONObject response)方法内添加以下行

progress_layout.setVisibility(View.GONE)