Abhi Bardolia Abhi Bardolia - 4 months ago 25
Android Question

Progressbar until the load of data from server

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;
}
}


By fetching the data from server takes some amount of time and i want to display the progressbar I find a code of asynctask but i am not getting what is there in asynctask. so do help me to get rid out from this in android. Advance thank you

Answer

Initialize progress in onCreate

progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);

Show progress before go to server like this

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

And dismiss after get result from server

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