Issue
This site helped me already a ton for a year now and it's the very first time I cannot find an answer to my problem.
I am trying to publish a progress from an AsyncTask without ProgressDialog. The user just typed an address and I want to display places of interest within a certain radius from that address on a map and in a list. Therefor I need to get the addresses of these places from a database and calculate the distances, before I know which places I need to display on the map as markers.
While calculating the distances (in an AsyncTask), I would like to keep the user updated on the progress, before the markers can be finally displayed on the map.
The main two problems, that I have with my code:
- It will show and dismiss the DialogFragment properly, but will not show the progress (neither by the bar, nor by the numbers)
- I do not want to use ProgressDialog
Thanks a ton in advance, for any hint or suggestion!
My AsyncTask looks like this:
public class GetDistanceTask extends AsyncTask<Void,Integer,Integer>
{
private Location mSearchAddress;
private FragmentManager mSfm;
private ContentResolver mCr;
private static final String TAG = GetDistanceTask.class.getSimpleName();
private DialogFragment mDf;
private ProgressDialog mProgressDialog;
public GetDistanceTask(FragmentManager sfm, ContentResolver cr, Location searchAddress){
mSfm = sfm;
mCr = cr;
mSearchAddress = searchAddress;
}
@Override
protected Integer doInBackground(Void...voids) {
int rowsUpdated = 0;
int max = 0;
final Uri locationUri = KitaContract.LocationEntry.CONTENT_URI;
String selection = KitaProvider.sLocationKitaSelection;
String[] args = new String[] {"3"};
//All entries, unsorted
Cursor cursor = mCr.query(locationUri, null, selection, args, null);
if (cursor != null) {
max = cursor.getCount();
cursor.moveToFirst();
} else {
Log.e(TAG, "Cursor == null !");
return 0;
}
do{
double kitaLat = cursor.getDouble(COL_LAT);
double kitaLong = cursor.getDouble(COL_LONG);
int id = cursor.getInt(COL_LOCID);
//make a Location-object from Kita's Lat & Long
Location kitaAddress = new Location("kitaName");
kitaAddress.setLatitude(kitaLat);
kitaAddress.setLongitude(kitaLong);
//calculate distance
float distance = kitaAddress.distanceTo(mSearchAddress);
ContentValues contentValues = new ContentValues();
contentValues.put(KitaContract.LocationEntry.COLUMN_DIST, distance);
//add the distance to the kita entry in the database
int rowUpdated =mCr.update(
locationUri,
contentValues,
KitaContract.LocationEntry._ID + " = ?",
new String[] {String.valueOf(id)});
rowsUpdated += rowUpdated;
publishProgress(rowsUpdated, max);
}while (cursor.moveToNext());
cursor.close();
return rowsUpdated;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// instantiate ProgressDialog
mDf = (DistanceProgressDialogFragment) mSfm.findFragmentByTag("dpdf_tag");
if (mDf == null){
mDf = new DistanceProgressDialogFragment();
mSfm.beginTransaction()
.add(mDf, "dpdf_tag")
.commitAllowingStateLoss();
}
else mDf.show(mSfm, "dpdf_tag");
if (mDf != null) {
mProgressDialog = (ProgressDialog) mDf.getDialog();
mProgressDialog.setProgressNumberFormat("%1d/%2d Kitas");
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setMax(progress[1]);
mProgressDialog.setProgress(progress[0]);
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(Integer rowsUpdated) {
if ( mDf != null) {
mDf.dismiss();
}
}
My DialogFragment like this:
public class DistanceProgressDialogFragment extends DialogFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setCancelable(false);
}
@NonNull
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState)
{
ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
dialog.setTitle("Kita Guide");
dialog.setMessage("Suche Kitas in der Nähe");
dialog.setIndeterminate(true);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
return dialog;
}
}
Solution
dialog.setIndeterminate(true);
indicates that progressbar should keep running forever.
Answered By - Viswanath Kumar Sandu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.