请不要考虑其重复的问题。 我已经阅读了有关取消异步任务的所有问题
基本上我有三个函数可以执行三个不同的查询并显示列表 OnSelection相应的单选按钮。
在进行db操作之前,即PreExecute()我隐藏了列表容器和 显示进度条旋转和后执行反之亦然。 用户可以随时点击其他单选按钮并选择不同的列表。
现在我的问题是一旦异步任务启动我无法阻止它因为我在异步任务中调用函数。我只能在执行函数之前检查isCancelled.Once函数是 从Asynch任务调用我没有被调用的控件和函数。被调用的函数占用了大部分时间来执行。那么如何在OnCancelled上停止执行该函数,即当用户按下另一个无线电时。
一种可能的解决方案是为每个函数/任务使用不同的asynch任务子类。 或者代替调用函数将完整代码放入后台函数中
我想知道更多实现这项任务的理想方式。
目前我的代码无效。
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();
}
}
}
答案 0 :(得分:0)
尝试将doInBackground
内码放入此内容:
while (!isCancelled()) {
// your doInBackground code
}