我有一个从RecyclerView.Adapter扩展的类,在该类中我有两个按钮,一个用于更新,一个用于删除,问题是我想使用这些按钮来调用另一个类中的方法它扩展了Fragments但我不知道如何将实例设置为我的方法,我也想传递一个参数。
这是我的类,它来自RecyclerView.Adapter:
package com.example.enriq.examen_tareas_segundo_plano_enrique_espinosa.adapter;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.enriq.examen_tareas_segundo_plano_enrique_espinosa.R;
import com.example.enriq.examen_tareas_segundo_plano_enrique_espinosa.tareasFragmento;
import java.util.ArrayList;
/**
* Created Enrique Espinosa.
*/
public class TareasAdapter extends RecyclerView.Adapter<TareasAdapter.UsuarioHolder>{
private Activity activity;
private ArrayList<Tareas> tareas;
public TareasAdapter(Activity activity, ArrayList<Tareas> tareas) {
this.activity = activity;
this.tareas = tareas;
}
public class UsuarioHolder extends RecyclerView.ViewHolder {
public TextView txtId;
public TextView txtNombreUsuario;
public TextView txtNota;
public TextView txtEstudiante;
public int pasId;
Button btnEliminar = (Button)itemView.findViewById(R.id.btnEliminar);
Button btnActualizar = (Button)itemView.findViewById(R.id.btnActualizar);
public UsuarioHolder(View itemView) {
super(itemView);
txtId = (TextView) itemView.findViewById(R.id.txt_id);
txtNombreUsuario = (TextView) itemView.findViewById(R.id.txt_nombre_usuario);
txtNota = (TextView) itemView.findViewById(R.id.txt_nota);
txtEstudiante = (TextView) itemView.findViewById(R.id.txtEstudiante);
}
}
@Override
public UsuarioHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = activity.getLayoutInflater().inflate(R.layout.plantilla_usuariosegundo, parent, false);
UsuarioHolder holder = new UsuarioHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final UsuarioHolder holder, int position) {
holder.txtId.setText(tareas.get(position).getId());
holder.txtNombreUsuario.setText("Tarea: "+ tareas.get(position).getNombreTarea());
holder.txtNota.setText("Nota: "+ tareas.get(position).getNota());
holder.txtEstudiante.setText("Estudiante: "+ tareas.get(position).getEstudiante());
}
@Override
public int getItemCount() {
return tareas.size();
}
}
这是我的课程,从片段延伸:
package com.example.enriq.examen_tareas_segundo_plano_enrique_espinosa;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.enriq.examen_tareas_segundo_plano_enrique_espinosa.adapter.Tareas;
import com.example.enriq.examen_tareas_segundo_plano_enrique_espinosa.adapter.TareasAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Enriq on 26/04/2018.
*/
public class tareasFragmento extends Fragment {
private String url = "http://192.168.1.128:8080/WebServiceExamenFinal/webapi/tareas";
private ArrayList<Tareas> estudiantes = new ArrayList<>();
//Si no funciona esto se puede borrar
AlertDialog alertDialog;
private List<Tareas> tareasList;
private TareasAdapter tareasAdapter;
private int assiId;
public tareasFragmento() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_tareas, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2,
GridLayoutManager.VERTICAL, false));
final TareasAdapter adapter = new TareasAdapter(getActivity(), estudiantes);
recyclerView.setAdapter(adapter);
RequestQueue queue = Volley.newRequestQueue(getContext());
final ProgressDialog dialog = new ProgressDialog(getContext());
dialog.setMessage("Por favor espere...");
dialog.show();
try {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
deserializarJSONArray(response);
adapter.notifyDataSetChanged();
if (dialog.isShowing()) dialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),
"Error al realizar la petición\n" + error.getMessage(),
Toast.LENGTH_LONG).show();
if (dialog.isShowing()) dialog.dismiss();
}
});
queue.add(jsonArrayRequest);
}catch (Exception e) {
Toast.makeText(getActivity(),"El error es: "+e,Toast.LENGTH_LONG).show();
}
return view;
}
public void deserializarJSONArray(JSONArray jsonArray) {
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject item = jsonArray.getJSONObject(i);
Tareas tareas = new Tareas();
tareas.setId(item.getString("id"));
tareas.setNombreTarea(item.getString("nombreUsuario"));
tareas.setEstudiante(item.getString("estudiante"));
tareas.setNota(item.getString("nota"));
estudiantes.add(tareas);
} catch (JSONException e) {
Toast.makeText(getActivity(), "Error al procesar la respuesta de la petición: " + e,
Toast.LENGTH_SHORT).show();
}
}
}
public void delete(int id){
Toast.makeText(getActivity(),"Este es el id recibido: "+id,Toast.LENGTH_LONG).show();
}
public int getAssiId(){
Toast.makeText(getActivity(),"Estas en el get de tareas fragmento",Toast.LENGTH_LONG).show();
return assiId;
}
}
我尝试过类似的事情:
tareasFragmento tarea = new tareasFragmento();
tarea.eliminar(id);
但它会返回此错误:
尝试调用虚拟方法&#39; android.content.res.Resources android.content.Context.getResources()&#39; null对象引用
答案 0 :(得分:0)
我已更新您的适配器。
public class TareasAdapter extends RecyclerView.Adapter<TareasAdapter.UsuarioHolder> {
private Activity activity;
private ArrayList<Tareas> tareas;
private OnClickButton callback;
public TareasAdapter(Activity activity, ArrayList<Tareas> tareas) {
this.activity = activity;
this.tareas = tareas;
}
public void setCallback(OnClickButton callback) {
this.callback = callback;
}
public class UsuarioHolder extends RecyclerView.ViewHolder {
public TextView txtId;
public TextView txtNombreUsuario;
public TextView txtNota;
public TextView txtEstudiante;
public int pasId;
Button btnEliminar;
Button btnActualizar;
public UsuarioHolder(View itemView) {
super(itemView);
txtId = (TextView) itemView.findViewById(R.id.txt_id);
txtNombreUsuario = (TextView) itemView.findViewById(R.id.txt_nombre_usuario);
txtNota = (TextView) itemView.findViewById(R.id.txt_nota);
txtEstudiante = (TextView) itemView.findViewById(R.id.txtEstudiante);
btnEliminar = (Button) itemView.findViewById(R.id.btnEliminar);
btnActualizar = (Button) itemView.findViewById(R.id.btnActualizar);
}
}
@Override
public UsuarioHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = activity.getLayoutInflater().inflate(R.layout.plantilla_usuariosegundo, parent, false);
UsuarioHolder holder = new UsuarioHolder(view);
holder.btnActualizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onClickUpdate();
}
});
holder.btnEliminar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onClickDelete();
}
});
return holder;
}
@Override
public void onBindViewHolder(final UsuarioHolder holder, int position) {
holder.txtId.setText(tareas.get(position).getId());
holder.txtNombreUsuario.setText("Tarea: " + tareas.get(position).getNombreTarea());
holder.txtNota.setText("Nota: " + tareas.get(position).getNota());
holder.txtEstudiante.setText("Estudiante: " + tareas.get(position).getEstudiante());
}
@Override
public int getItemCount() {
return tareas.size();
}
public interface OnClickButton {
void onClickUpdate();
void onClickDelete();
}
}
修改强>
现在,使用此适配器的片段实现接口OnClickButton
。然后,您将能够操纵活动中的点击事件,这是操作/与片段通信的正确位置。
最后在初始化适配器调用之后:
adapter.setCallback(this);