Please dont downvote considering its repeated question. I have already read all questions to related to cancelling asynch task
Basicaly I hav three function which execute three different queries and shows the list On click of corresponding radio button.Before doing db operation I hide list container and display progress bar spining and on post execute vice versa. at any time user can click other radio button and select differnt list.
Now my problem is once asynch task is started I am not able to stop it Since I m calling function inside asynch task.I could only check isCanclelled before executing the function.Once function is invoked from Asynch task I dont have controll and function which is bieng called takes most of the time to execute.So how does I stop that function to execute. when user press another radio.
One possible solution would be to use different subclass of asynch task for each function/task. or instead of calling function put enitre code in do in Background function
I would like to know more Ideal way of achieving this task.
Currently my code which does not work properly.
public class ShopListView extends FragmentActivity {
public static Location currentLocation;
private static LocationTracker lTracker;
private static final String TAG = "ShopListView";
private GetList mTask=null;
private String mIntent="showPopular";
ListView lview;
boolean flagCategory=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop_list_view);
lview= (ListView) findViewById(R.id.listView1);
RadioGroup radioGroupListSelector = (RadioGroup) findViewById(R.id.radio_group_list_selector);
radioGroupListSelector.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch (checkedId) {
case R.id.radioPopular :
Log.i(TAG,"Popular Radio Button Selected");
if(mTask!=null)
{
mTask.cancel(true);
Log.i(TAG,"Canceling Current Task");
}
else
{
mTask=new GetList();
mTask.execute("showPopular");
}
break;
case R.id.radioAZ :
Log.i(TAG,"AZ Radio Button Selected");
if(mTask!=null)
{
mTask.cancel(true);
Log.i(TAG,"Canceling Current Task");
}
else
{
mTask=new GetList();
mTask.execute("showAZ");
}
break;
case R.id.radioCategory:
Log.i(TAG,"Category Radio Button Selected");
if(mTask!=null)
{
mTask.cancel(true);
Log.i(TAG,"Canceling Current Task");
}
else
{
mTask=new GetList();
mTask.execute("showCategory");
}
break;
case R.id.radioNearBy :
Log.i(TAG,"NearBy Radio Button Selected");
if(mTask!=null)
{
mTask.cancel(true);
Log.i(TAG,"Canceling Current Task");
}
else
{
mTask=new GetList();
mTask.execute("showNearBy");
}
break;
default:
if(mTask!=null)
{
mTask.cancel(true);
Log.i(TAG,"Canceling Current Task");
}
showFeatured();
Log.i(TAG,"No Radio Selected");
}
}
});
}
public void showFeatured()
{
}
public ArrayListshowPopular(){
flagCategory=false;
ArrayListlist=new ArrayList ();
String sql="select S.shopName shopName from streetShopInfo AS S JOIN ratings AS R where S.shopName=R.shopName and R.overall >0 order by S.shopName";
Log.i(TAG,"Creating Adapter for Fetching Data");
StreetFoodDataBaseAdapter mDBAdapter= new StreetFoodDataBaseAdapter(this);
Log.i(TAG,"Adapter Ready..");
Log.i(TAG,"Creating/Opening Database");
mDBAdapter.createDatabase();
mDBAdapter.open();
Log.i(TAG,"Requesting info from getInfo function");
list=mDBAdapter.getInfo(sql,"shopName");
Log.i(TAG,"Information Retrived Passing it to SetView");
//setView(list);
mDBAdapter.close();
return list;
}
public ArrayListshowNearBy() {
flagCategory=false;
ArrayListlist=new ArrayList ();
list=null;
String sql="select shopName shopName from streetShopInfo where distance<3";
//currentLocation=lTracker.getLocation();
Log.i(TAG,"Location Tracker Started");
StreetFoodDataBaseAdapter mDBAdapter= new StreetFoodDataBaseAdapter(this);
mDBAdapter.createDatabase();
mDBAdapter.open();
currentLocation=lTracker.getLocation();
if(mDBAdapter.validDistance() && currentLocation!=null && currentLocation.getLatitude()!=0)
{
Log.i(TAG,"Now Fetching Near By Location from DB");
list=mDBAdapter.getInfo(sql,"shopName");
Log.i(TAG,"Cursor Values Retrived into Array list");
mDBAdapter.close();
}
else
{
if(currentLocation!=null && currentLocation.getLatitude()!=0 )
{
Log.i(TAG,"Location Received");
mDBAdapter.updateDistance();
list=mDBAdapter.getInfo(sql,"shopName");
mDBAdapter.close();
}
}
return list;
}
public ArrayListshowAZ(){
ArrayListlist=new ArrayList ();
flagCategory=false;
String sql="select shopName from streetShopInfo order by shopName";
StreetFoodDataBaseAdapter mDBAdapter= new StreetFoodDataBaseAdapter(this);
mDBAdapter.createDatabase();
mDBAdapter.open();
list=mDBAdapter.getInfo(sql,"shopName");
Log.i(TAG,"Cursor Values Retrived into Array list");
//setView(list);
mDBAdapter.close();
return list;
}
public ArrayListshowCategory(){
ArrayListlist=new ArrayList ();
flagCategory=true;
String sql="select distinct category from streetShopInfo order by category";
StreetFoodDataBaseAdapter mDBAdapter= new StreetFoodDataBaseAdapter(this);
mDBAdapter.createDatabase();
mDBAdapter.open();
list=mDBAdapter.getInfo(sql,"category");
Log.i(TAG,"Cursor Values Retrived into Array list");
//setView(list);
mDBAdapter.close();
return list;
}
/*
* Sub Class for Asynchronous Task
*/
class GetList extends AsyncTask>
{
LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
LinearLayout linlaContainer = (LinearLayout) findViewById(R.id.ListViewContainer);
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
linlaContainer.setVisibility(View.GONE);
linlaHeaderProgress.setVisibility(View.VISIBLE);
}
protected ArrayListdoInBackground(String... params)
{
ArrayListresult = null;
// TODO Auto-generated method stub
if(params[0].equals("showNearBy") && !isCancelled())
result=showNearBy();
else if(params[0].equals("showPopular") && !isCancelled())
result=showPopular();
else if(params[0].equals("showAZ") && !isCancelled())
result=showAZ();
else if(params[0].equals("showCategory") && !isCancelled())
result=showCategory();
return result;
}
@Override
protected void onCancelled() {
super.onCancelled();
linlaHeaderProgress.setVisibility(View.GONE);
linlaContainer.setVisibility(View.VISIBLE);
// ask if user wants to try again
}
@Override
protected void onPostExecute(ArrayListresult)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
linlaHeaderProgress.setVisibility(View.GONE);
linlaContainer.setVisibility(View.VISIBLE);
if(result!=null)
setView(result);
else
Toast.makeText(ShopListView.this,"Sorry Your Location not available..",Toast.LENGTH_LONG).show();
}
}
}
.
stackoverflow.comm
No comments:
Post a Comment