0
请不要低估考虑它的重复问题。 我已阅读所有与取消异步任务有关的问题如何在下面的情况下取消异步任务
基本上我有三个函数执行三个不同的查询并显示列表 OnSelection对应的单选按钮。
在执行数据库操作之前,即PreExecute()我隐藏列表容器和 显示进度条正在旋转并在后期执行反之亦然。 用户可以随时点击其他单选按钮并选择不同的列表。
现在我的问题是一次异步任务开始我无法阻止它因为我在异步任务内调用函数。我只能在执行函数之前检查isCancelled。从Asynch任务调用的一次函数是 我没有控制和被调用的函数。被调用的函数需要大部分时间才能执行。那么如何停止该函数在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();
}
}
}
它将执行内部函数之前检查状态。主要的问题是,一旦内部函数开始执行控制研究被传递到来自while循环。内部函数需要更多时间。我想取消执行该功能 – 2013-04-08 07:45:56