*************问题未解决,请检查以下解决方案*************
近半天我一直在努力。无法使其正常工作。 我有私有方法的AsyncTask,所以我可以在CustomLvAdapter中传递boolean和String值
private void changeJobStatus(final boolean isAppliedforAJob, final String jobID){
class ChangeJobStatus extends AsyncTask<Void,Void,String> {
//private Delegates del = null;
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
if(isAppliedforAJob) {
loading = ProgressDialog.show(context, "","Canceling application", false);
}
else {
loading = ProgressDialog.show(context, "","Applying for position", false);
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//del.asyncCompleteOnCustomJob(true);
loading.dismiss();
}
@Override
protected String doInBackground(Void... v) {
String res;
HashMap<String,String> params = new HashMap<>();
params.put(Config.KEY_USER_ID, studentID);
params.put(Config.KEY_JOB_ID, jobID);
RequestHandler rh = new RequestHandler();
if(isAppliedforAJob)
res = rh.sendPostRequest(Config.URL_CANCEL_APPLICATION, params);
else
res = rh.sendPostRequest(Config.URL_APPLY_FOR_A_JOB, params);
Log.d("Stringas", "CustomListViewBackground " + res);
return res;
}
}
ChangeJobStatus cjs = new ChangeJobStatus();
cjs.execute();
}
并在onPostExcecute()中我想将notifyOnDataSetChanged()调用到我的另一个活动lvAdapters。 据我所知,我必须实现委托接口,但我没有成功。我在主类中初始化委托时失败了,因为changeJobStatus方法是私有的,它在customLvAdapter类中调用。
如果我在ChangeJobStatus类中创建一个构造函数
public ChangeJobStatus(Delegates delegate)
{
this.del = delegate;
}
我必须在参数中传递一些东西。如果我传递新的委托,则不会触发我的另一个活动中的委托实现。
ChangeJobStatus cjs = new ChangeJobStatus(new Delegates() {
@Override
public void asyncCompleteOnCustomJob(boolean success) {
//whatever
}
});
cjs.execute();
我希望你能帮助我找出正确的实施方案, 干杯
*********** SOLUTION ***********
可悲的是,我无法实现用户给我的内容,但我很高兴听到你们其中一个人可以使用广播接收器。它奏效了。 这就是我所做的
在主课程中创建广播接收器
private final BroadcastReceiver broadcastJobList = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//what will happen, when event triggers
}
};
注册自定义意图并将其注册到主类onCreate方法中的广播接收器或您感觉舒适的地方:)
IntentFilter filter = new IntentFilter();
filter.addAction("jobListChanged");
registerReceiver(broadcastJobList, filter);
我们要做的就是发送意图,它将触发广播接收器。我的场景中的以下代码转到自定义适配器中的onPostExcecute方法(在自定义适配器的开始时为Context初始化了上下文)
Intent intent = new Intent();
intent.setAction("jobListChanged");
context.sendBroadcast(intent);
希望我会帮助有这个问题的人。干杯!
答案 0 :(得分:0)
//你的asynctask类
public class ChangeJobStatus extends AsyncTask<String, Void, String> {
private ProgressDialog loading;
private OnResponseListener responseListener;
private boolean isAppliedforAJob;
private Context con;
public ChangeJobStatus(Context con,boolean state) {
super();
// TODO Auto-generated constructor stub
this.con=con;
isAppliedforAJob = state;
}
public void setOnResponseListener(OnResponseListener onLoadMoreListener) {
this.responseListener = onLoadMoreListener;
}
public interface OnResponseListener {
public void onResponse(String responsecode);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (isAppliedforAJob) {
loading = ProgressDialog.show(con, "", "Canceling application", false);
} else {
loading = ProgressDialog.show(con, "", "Applying for position", false);
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// del.asyncCompleteOnCustomJob(true);
loading.dismiss();
responseListener.onResponse(s);
}
@Override
protected String doInBackground(String... param) {
String res="";
HashMap<String, String> params = new HashMap<>();
params.put(Config.KEY_JOB_ID, param[0]);// job id
params.put(Config.KEY_USER_ID, param[1]);// student id
RequestHandler rh = new RequestHandler();
if (isAppliedforAJob)
res = rh.sendPostRequest(Config.URL_CANCEL_APPLICATION, params);
else
res = rh.sendPostRequest(Config.URL_APPLY_FOR_A_JOB, params);
return res;
}
}
在您的活动类
中public class MainActivity extends Activity implements OnResponseListener {
String jobId="1",studId="1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ChangeJobStatus cbs=new ChangeJobStatus(this, true);
cbs.setOnResponseListener(this);
cbs.execute(jobId,studId);
}
@Override
public void onResponse(String responsecode) {
// TODO Auto-generated method stub
//here u can do ur stuff with the string
}
}