进度条,直到从服务器加载数据
Posted
技术标签:
【中文标题】进度条,直到从服务器加载数据【英文标题】:Progressbar until the load of data from server 【发布时间】:2016-12-10 15:16:28 【问题描述】: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】:在onCreate
中初始化progress
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();
);
【讨论】:
老兄,if (isNetworkAvailable())
已经在 refresh.setOnClickListener(new View.OnClickListener()
块中。
很高兴能为您提供帮助。以上是关于进度条,直到从服务器加载数据的主要内容,如果未能解决你的问题,请参考以下文章