进度条直到从服务器加载数据

时间:2016-08-04 13:12:22

标签: android json android-asynctask android-progressbar

public class Recyclerview extends AppCompatActivity {

private RecyclerView mRecyclerView;
CustomAdapter cu;


ArrayList<Employee> arr, arr1;
Toolbar toolbar;

TextView t1, t2;
long l = 0;
private ProgressDialog progress;


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

    toolbar = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar);

    final RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setHasFixedSize(true);

    arr = new ArrayList<Employee>();
    arr = InitializeData();

    final LinearLayoutManager llm = new LinearLayoutManager(Recyclerview.this);
    rv.setLayoutManager(llm);
    rv.setHasFixedSize(true);

    cu = new CustomAdapter(Recyclerview.this, arr);

    final bank ban = new bank(Recyclerview.this);
    rv.setAdapter(cu);

    registerForContextMenu(rv);

    ImageButton refresh = (ImageButton) findViewById(R.id.refresh);

    refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(Recyclerview.this, "ok", Toast.LENGTH_LONG).show();


            if (isNetworkAvailable()) {

                String url = ConstantValues.BASE_URL;
                RequestBody formBody = new FormBody.Builder()
                        .add("key1", "value1")
                        .add("key2", "value2")
                        .add("key3", "value3")
                        .build();

                try {
                    post(url, formBody, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.e("JSONDemo", "IOException", e);
                        }

                        @Override
                        public void onResponse(final Call call, final Response response) throws IOException {
                            String JSON = response.body().string();
                            Log.e("res", " " + JSON);
                            try {
                                JSONObject jsonObj = new JSONObject(JSON);

                                JSONArray resultarr = jsonObj.getJSONArray("result");
                                final JSONArray resultarr1 = jsonObj.getJSONArray("result1");


                                ban.OpenDB();
                                /*Delete old Data from Table*/
                                ban.Delete("");
                                for (int i = 0; i < resultarr1.length(); i++) {

                                    Employee emp = new Employee();
                                    JSONObject result1obj = resultarr1.getJSONObject(i);
                                    String result1Id = result1obj.getString(ConstantValues.Bank_ID);
                                    String result1NAME = result1obj.getString(ConstantValues.Bank_NAME);
                                    Log.e("result", " " + result1Id);
                                    Log.e("result", " " + result1NAME);
                                    emp.setId(result1obj.getString(ConstantValues.Bank_ID));
                                    emp.setName(result1obj.getString(ConstantValues.Bank_NAME));
                                    arr.add(emp);

                                    l = ban.InsertQryForTabEmpData(result1Id, result1NAME);
                                }
                                ban.CloseDB();


                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        // you can access all the UI componenet


                                        cu.notifyDataSetChanged();
                                    }
                                });
                            } catch (Exception e) {
                                Log.e("JSONDemo", "onResponse", e);
                            }

                        }
                    });

                } catch (Exception e) {
                    Log.e("JSONDemo", "Post Exception", e);
                }

            } else {
                Toast.makeText(Recyclerview.this, "Internet not available", Toast.LENGTH_LONG).show();
            }


        }
    });


}

private ArrayList<Employee> InitializeData() {
    ArrayList<Employee> arr_emp = new ArrayList<Employee>();
    bank ban = new bank(Recyclerview.this);
    long l = 0;
    ban.OpenDB();
    arr_emp = ban.AllSelectQryForTabEmpData();
    ban.CloseDB();
    return arr_emp;
 }


private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}


private final OkHttpClient client = new OkHttpClient();

Call post(String url, RequestBody formBody, Callback callback) throws IOException {

    Request request = new Request.Builder()
            .url(url)
            .post(formBody)
            .build();

    Call call = client.newCall(request);
    call.enqueue(callback);
    return call;
   }
 }

从服务器获取数据需要一些时间,我想显示进度条我找到了asynctask的代码,但我没有得到asynctask中的内容。所以帮助我在android中摆脱这个。谢谢你

1 个答案:

答案 0 :(得分:0)

progress

中初始化onCreate
progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);

在转到像这样的服务器之前显示progress

if (isNetworkAvailable()) {
      runOnUiThread(new Runnable() {
              @Override
              public void run() {
                    progress.show();
              }
       });
      ...        
}

从服务器获取结果后解雇

runOnUiThread(new Runnable() {
         @Override
         public void run() {
              if (progress.isShowing())
                  progress.dismiss();
         }
});