考虑到它的重复问题,请不要投反对票。我已经阅读了与取消异步任务相关的所有问题
基本上我有三个函数,它们执行三个不同的查询,并在相应单选按钮的 OnSelection 上显示列表。
在执行 db 操作之前,即 PreExecute() 我隐藏列表容器并显示进度条旋转,反之亦然。用户可以随时单击其他单选按钮并选择不同的列表。
现在我的问题是,一旦异步任务启动,我就无法停止它,因为我在异步任务中调用函数。我只能在执行函数之前检查 isCancelled。一旦从异步任务调用函数,我就没有被调用的控制和函数。被调用的函数需要大部分时间来执行。所以我如何停止该函数在 OnCancelled 上执行,即当用户按下另一个收音机时。
一种可能的解决方案是为每个功能/任务使用不同的异步任务子类。或者代替调用函数将整个代码放在后台函数中
我想了解更多实现此任务的理想方式。
目前我的代码不能正常工作。
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 ArrayList<String> showPopular(){
flagCategory=false;
ArrayList<String> list=new ArrayList<String>();
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 ArrayList<String> showNearBy() {
flagCategory=false;
ArrayList<String> list=new ArrayList<String>();
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 ArrayList<String> showAZ(){
ArrayList<String> list=new ArrayList<String>();
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 ArrayList<String> showCategory(){
ArrayList<String> list=new ArrayList<String>();
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<String,Void,ArrayList<String> >
{
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 ArrayList<String> doInBackground(String... params)
{
ArrayList<String> result = 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(ArrayList<String> result)
{
// 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();
}
}
}