问题
I am using fast adapter
of the mike penz where I am trying to remove the item from the adapter
. Its like when I press the accept button then the item must be removed from the list but to remove the like I need to pass the position variable in it but I am not able to get the position variable from the adapter
.
MainActivity.class
public class Doc_Appointment_Req extends BaseActivity {
@BindView(R.id.doc_app_req_recyclerview)
RecyclerView doc_app_req_recyclerview;
FastItemAdapter<Doc_Appointment_Req_Adapter> doctorreqappadapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.doc_appointment_req);
ButterKnife.bind(this);
assert getSupportActionBar() != null;
getSupportActionBar().setTitle("Appointments Requests");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
doctorreqappadapter = new FastItemAdapter<>();
LinearLayoutManager departmentmanager = new LinearLayoutManager(Doc_Appointment_Req.this, LinearLayoutManager.VERTICAL, false);
doc_app_req_recyclerview.setLayoutManager(departmentmanager);
doc_app_req_recyclerview.setAdapter(doctorreqappadapter);
appointmentreqmethod();
}
private void appointmentreqmethod() {
Display.openloader(Doc_Appointment_Req.this);
final String docappointmentreq = Constant.url + "getmyappointment&dr_id=" + Session.getUserID(getApplicationContext());
Display.log(docappointmentreq);
JsonObjectRequest docappointrequest = new JsonObjectRequest(Request.Method.GET, docappointmentreq, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray docarray = response.getJSONArray("data");
for (int i = 0; i < docarray.length(); i++) {
JSONObject docreqappobj = docarray.getJSONObject(i);
Doc_Appointment_Req_Adapter docapplistreq = new Doc_Appointment_Req_Adapter(getApplicationContext());
docapplistreq.setPatient_name(docreqappobj.getString("patient_name"));
docapplistreq.setApp_date(docreqappobj.getString("app_date"));
docapplistreq.setTime(docreqappobj.getString("time").replace("$", " "));
docapplistreq.setAge(docreqappobj.getString("age"));
docapplistreq.setDescription(docreqappobj.getString("description"));
docapplistreq.setGender(docreqappobj.getString("gender"));
docapplistreq.setType(docreqappobj.getString("type"));
docapplistreq.setId(docreqappobj.getString("id"));
docapplistreq.setUser_id(docreqappobj.getString("user_id"));
doctorreqappadapter.add(docapplistreq);
}
} catch (JSONException e) {
e.printStackTrace();
}
doctorreqappadapter.notifyDataSetChanged();
Display.dismissloader();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Display.log(error.toString());
}
});
docappointrequest.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(docappointrequest);
}
}
Adapter.class
public class Doc_Appointment_Req_Adapter extends AbstractItem<Doc_Appointment_Req_Adapter, Doc_Appointment_Req_Adapter.Doc_App_req_Vh> {
private String patient_name;
private String time;
private String app_date;
private String gender;
private String age;
private String description;
private String type;
private String user_id;
private String id;
Context context;
FastItemAdapter<Doc_Appointment_Req_Adapter> docfastitemadapter;
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getPatient_name() {
return patient_name;
}
public void setPatient_name(String patient_name) {
this.patient_name = patient_name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getApp_date() {
return app_date;
}
public void setApp_date(String app_date) {
this.app_date = app_date;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setType(String type) {
this.type = type;
}
public Doc_Appointment_Req_Adapter(Context context) {
this.context = context;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public int getType() {
return R.id.doc_app_req_recyclerview;
}
@Override
public int getLayoutRes() {
return R.layout.doc_appointment_req_row;
}
@Override
public void bindView(Doc_App_req_Vh holder, List payloads) {
super.bindView(holder, payloads);
holder.pat_name.setText(patient_name);
holder.pat_problem.setText(description);
holder.pat_age.setText(age);
holder.pat_gender.setText(gender);
holder.pat_you.setText(type);
holder.date.setText(app_date);
holder.timeslot.setText(time);
holder.accept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
payloads.remove(here i need the position);
}
});
holder.reject.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
payloads.remove(here i need the position);
}
});
holder.reschedule.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
payloads.remove(here i need the position);
}
});
}
public static class Doc_App_req_Vh extends RecyclerView.ViewHolder {
@BindView(R.id.pat_name)
TextView pat_name;
@BindView(R.id.pat_problem)
TextView pat_problem;
@BindView(R.id.pat_age)
TextView pat_age;
@BindView(R.id.pat_gender)
TextView pat_gender;
@BindView(R.id.pat_you)
TextView pat_you;
@BindView(R.id.date)
TextView date;
@BindView(R.id.timeslot)
TextView timeslot;
@BindView(R.id.accept)
Button accept;
@BindView(R.id.reject)
Button reject;
@BindView(R.id.reschedule)
Button reschedule;
public Doc_App_req_Vh(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
private void appointmentaccept(String actionpass) {
String accepturl = Constant.url + actionpass + "&user_id=" + user_id + "&dr_id=" + Session.getUserID(context) + "&app_id=" + id;
Display.log(accepturl);
JsonObjectRequest acceptrequest = new JsonObjectRequest(Request.Method.GET, accepturl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("responce").equals("success")) {
Display.toast(context, response.getString("data"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Display.log(error.toString());
}
});
acceptrequest.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(acceptrequest);
}
}
回答1:
You should not set click listeners within the ViewHolder
as you would not be able to access position or anything. Please use the ClickListenerHelper
as shown in the sample.
//init the ClickListenerHelper which simplifies custom click listeners on views of the Adapter
mClickListenerHelper = new ClickListenerHelper<>(fastItemAdapter);
fastItemAdapter.withOnCreateViewHolderListener(new FastAdapter.OnCreateViewHolderListener() {
@Override
public RecyclerView.ViewHolder onPreCreateViewHolder(ViewGroup parent, int viewType) {
return fastItemAdapter.getTypeInstance(viewType).getViewHolder(parent);
}
@Override
public RecyclerView.ViewHolder onPostCreateViewHolder(final RecyclerView.ViewHolder viewHolder) {
mClickListenerHelper.listen(viewHolder, ((Doc_Appointment_Req_Adapter.Doc_App_req_Vh) viewHolder). accept, new ClickListenerHelper.OnClickListener<Doc_Appointment_Req_Adapter>() {
@Override
public void onClick(View v, int position, Doc_Appointment_Req_Adapter item) {
...
}
});
return viewHolder;
}
});
Basically, this will add a listener
to the view when the ViewHolder
is created. And returns the item, the position, and its view for you.
来源:https://stackoverflow.com/questions/39832543/fastadapter-position