如何从Adapter类调用AsyncTask?

时间:2015-08-18 09:43:41

标签: android listview android-adapter

我的活动课程为GetMembersAsyncTask。我还有一个带DeleteAsynccTask的Adapter类。当我在列表查看项目中单击取消图像时,DeleteAsynccTask将调用。如果该项目已成功删除,那么我将在GetMemberAsynacTask中的OnPostExecute()方法内拨打DeleteAsynccTask

public class AddmemberSearch extends Activity {
private ListViewAdapter listviewAdapter;
protected void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.family_search_members);
    ButterKnife.inject(this);
    new GetFamilyListAsynctask().execute();
 }
class GetMembersAsyncTask extends AsyncTask<String, String, String> {
    protected void OnPreExecute(){}
    protected String doInBackground(String... params) { 
        some code here
    }
    protected void onPostExecute(String file_url) { }
}
}

现在ListViewAdapter类是这样的。

public class ListViewAdapter extends BaseAdapter {
Activity context;    
private ImageButton cancel_img;
SharedPreferences sharedPrefUser;
public ListViewAdapter(Activity context, ArrayList<familiesListBO> data) {
    super();
    this.context = context;
    this.data = data;
}
@Override
public int getCount() {
    return data.size();
}
@Override
public Object getItem(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}
private class ViewHolder {
    TextView txtViewMember; 
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    LayoutInflater inflater = context.getLayoutInflater();
    FamiliesListBO familiesListBO;
    familiesListBO = data.get(position);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.added_member_listview, null);
        holder = new ViewHolder();
        holder.txtViewMember = (TextView) convertView.findViewById(R.id.txt_addedmember_names);            
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }        
    if (familiesListBO.getName() != null) {
        holder.txtViewMember.setText(familiesListBO.getName());
        familiesListBO.getName();
    }
    cancel_img = (ImageButton) convertView.findViewById(R.id.cancel_row_img);
    cancel_img.setTag(holder);
    cancel_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            new DeleteAsynccTask().execute();
        }
    });
    return convertView;
}

class DeleteAsynccTask extends AsyncTask<String, String, String> {
    ResultVO getDeletionsResponse;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected String doInBackground(String... params) {
         try {                
            getDeletionsResponse = services.delete("this is delete code");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String s) {
        int appStatusCode = getDeletionsResponse.getAppStatusCode();
        if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
            ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_SUCCESS_TITLE, IFamilyUtil.FAMILY_DELETE_SUCCESS_MESSAGE, true);
            new AddmemberSearch.GetMembersAsyncTask().execute();
        } else {
            ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_FAIL_TITLE, IFamilyUtil.FAMILY_DELETE_FAIL_MESSAGE, true);
        }
        notifyDataSetChanged();           
    }
}
}

此处如何从Adapter类调用GetMemberAsynctask

4 个答案:

答案 0 :(得分:5)

您可以在活动类中移动适配器类,并将适配器类作为内部类。因此您可以在适配器中调用GetMemberAsynctask,如下所示:

public class AddmemberSearch extends Activity {
private ListViewAdapter listviewAdapter;
protected void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.family_search_members);
    ButterKnife.inject(this);
    new GetFamilyListAsynctask().execute();
 }

public class ListViewAdapter extends BaseAdapter {
Activity context;    
private ImageButton cancel_img;
SharedPreferences sharedPrefUser;
public ListViewAdapter(Activity context, ArrayList<familiesListBO> data) {
    super();
    this.context = context;
    this.data = data;
}
@Override
public int getCount() {
    return data.size();
}
@Override
public Object getItem(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}
private class ViewHolder {
    TextView txtViewMember; 
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    LayoutInflater inflater = context.getLayoutInflater();
    FamiliesListBO familiesListBO;
    familiesListBO = data.get(position);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.added_member_listview, null);
        holder = new ViewHolder();
        holder.txtViewMember = (TextView) convertView.findViewById(R.id.txt_addedmember_names);            
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }        
    if (familiesListBO.getName() != null) {
        holder.txtViewMember.setText(familiesListBO.getName());
        familiesListBO.getName();
    }
    cancel_img = (ImageButton) convertView.findViewById(R.id.cancel_row_img);
    cancel_img.setTag(holder);
    cancel_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            new DeleteAsynccTask().execute();
        }
    });
    return convertView;
}

class DeleteAsynccTask extends AsyncTask<String, String, String> {
    ResultVO getDeletionsResponse;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected String doInBackground(String... params) {
         try {                
            getDeletionsResponse = services.delete("this is delete code");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String s) {
        int appStatusCode = getDeletionsResponse.getAppStatusCode();
        if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
            ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_SUCCESS_TITLE, IFamilyUtil.FAMILY_DELETE_SUCCESS_MESSAGE, true);
            new AddmemberSearch.GetMembersAsyncTask().execute();
        } else {
            ExceptionMessages.showAlertDialog(context, IFamilyUtil.FAMILY_DELETE_FAIL_TITLE, IFamilyUtil.FAMILY_DELETE_FAIL_MESSAGE, true);
        }
        notifyDataSetChanged();           
    }
}

class GetMembersAsyncTask extends AsyncTask<String, String, String> {
    protected void OnPreExecute(){}
    protected String doInBackground(String... params) { 
        some code here
    }
    protected void onPostExecute(String file_url) { }
}

我希望它有所帮助!

答案 1 :(得分:3)

从AddmemberSearch类中删除GetMembersAsyncTask内部类 和
将GetMembersAsyncTask作为一个单独的类(将它放在你的包中)

之后你可以这样调用“new GetFamilyListAsynctask()。execute();”来自任何地方。

答案 2 :(得分:0)

只需将 ListViewAdapter 放在 AddMemberSearch

答案 3 :(得分:0)

从适配器类调用AsyncTask时,只需将notifyDataSetChanged放在onPostExecute

notifyDataSetChanged();

效果很好