Issue
I am trying to fetch data from MySQL database and display it in listview. The data is successfully retrieved but the listview is not populated until the screen display is off. Progress dialog also doesn't appear until the listview is populated. Any suggestions?
public class BestLinksActivity extends AppCompatActivity {
public ListView myListView;
public MyListViewAdapter mAdapter;
public List<HotelInfo> dataSource;
private String cityName;
ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_best_links);
dataSource = new ArrayList<>();
mProgressDialog = new ProgressDialog(BestLinksActivity.this);
mProgressDialog.setMessage("Loading data...");
Bundle extras = getIntent().getExtras();
if (extras != null) {
cityName = extras.getString("City");
}
DataBaseReader dbReader = new DataBaseReader();
if (!(cityName.equals(null))) {
dbReader.execute(cityName);
} else {
Toast.makeText(getApplicationContext(), "City name not specified", Toast.LENGTH_SHORT).show();
}
//Create adapter
mAdapter = new MyListViewAdapter(getApplicationContext(), dataSource);
//Configure the listview
myListView = (ListView) findViewById(R.id.main_list_view);
myListView.setAdapter(mAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item value
HotelInfo currentItem = dataSource.get(position);
//Open url of the currentItem in web browser
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(currentItem.getLinkUrl())));
}
});
}
public String getTitle(String url) {
String[] subStrings = url.split("/");
String urlTitle = "";
for (int i = 0; i < subStrings.length; i++) {
if (subStrings[i].equals("t"))
urlTitle = (subStrings[i + 1]);
}
urlTitle = urlTitle.replace("-", " ");
urlTitle=((urlTitle.charAt(0)+"").toUpperCase()).concat(urlTitle.substring(1));
return urlTitle;
}
public class DataBaseReader extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected String doInBackground(String... params) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.build();
Log.d("CheckParam", params[0]);
RequestBody postBody = new FormBody.Builder()
.add("cityName", params[0])
.build();
Request myRequest = new Request.Builder()
.url("http://172.18.0.32/extractData.php")
.post(postBody)
.build();
String serverResponse = null;
try {
Response response = okHttpClient.newCall(myRequest).execute();
serverResponse = response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
try {
JSONObject myJsonObj = new JSONObject(serverResponse);
JSONArray myJsonArray = myJsonObj.getJSONArray("server_response");
for (int index = 0; index < myJsonArray.length(); index++) {
JSONObject linkObject = myJsonArray.getJSONObject(index); //Otherwise, you will get last element
HotelInfo myHotelInfo = new HotelInfo();
myHotelInfo.setLinkUrl(linkObject.getString("Link"));
myHotelInfo.setLinkTitle(getTitle(myHotelInfo.getLinkUrl()));
dataSource.add(myHotelInfo);
}
}
catch (JSONException e) {
e.printStackTrace();
}
Log.d("MyKeyser", serverResponse);
return serverResponse;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("MyKey", s);
if (mProgressDialog!=null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}
}
Solution
Add the following line to your onPostExecute
:
mAdapter.notifyDataSetChanged();
You need to notify your adapter that the data had changed. See the docs.
Regarding the ProgressBar
, you need to put it under some Layout in your activity_best_links
layout.
The best way would be to not create it programatically, but add it to your xml layout file, see example here, and then play with its visibility via mProgress.setVisibility();
Answered By - marmor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.