1
I have my search screen. When typing, it brings the search result. The result is shown in a recyclerview. My Recyclerview was modeled with some textviews and an image. I wanted when touching the item of Recyclerview, that the text of the textview was passed to another Fragment (screen) that I have. I can even do the part of the screen call. I just can’t rescue the text from the textview on the other screen. I even instantiated the textviews of the other screen that modeled the Recycler.
My class of adapter:
public class CursosAdapterImg extends RecyclerView.Adapter<CursosAdapterImg.CursosHolder> {
List<Curso>listaCursos;
private OnNoteListener mOnNoteListener;
public CursosAdapterImg(List<Curso> listaCursos, Context context,OnNoteListener onNoteListener) {
this.listaCursos = listaCursos;
this.mOnNoteListener = onNoteListener;
}
@NonNull
@Override
public CursosHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View vista = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_cursos_img, parent, false);
RecyclerView.LayoutParams layoutParams =
new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
vista.setLayoutParams(layoutParams);
return new CursosHolder(vista, mOnNoteListener);
}
@Override
public void onBindViewHolder(@NonNull CursosHolder holder, int position) {
// holder.txtCodigo.setText(listaCursos.get(position).getCodigo().toString());
holder.txtNome.setText(listaCursos.get(position).getNome().toString());
holder.txtProfessor.setText(listaCursos.get(position).getProfessor().toString());
holder.txtCategoria.setText(listaCursos.get(position).getCategoria().toString());
if(listaCursos.get(position).getImagem()!=null){
holder.idImagem.setImageBitmap(listaCursos.get(position).getImagem());
}else{
holder.idImagem.setImageResource(R.drawable.sem_foto);
}
}
@Override
public int getItemCount() {
return listaCursos.size();
}
public class CursosHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txtNome,txtCodigo,txtProfessor, txtCategoria;
ImageView idImagem;
OnNoteListener onNoteListener;
public CursosHolder(View itemView,OnNoteListener onNoteListener) {
super(itemView);
txtNome= (TextView) itemView.findViewById(R.id.nomeCurso);
//txtCodigo= (TextView) itemView.findViewById(R.id.txtCodigo);
txtProfessor= (TextView) itemView.findViewById(R.id.Professor);
txtCategoria= (TextView) itemView.findViewById(R.id.Categoria);
idImagem= itemView.findViewById(R.id.idImagem);
this.onNoteListener = onNoteListener;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
onNoteListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteListener{
void onNoteClick(int position);
}
}
Below is my search screen that returns a Recyclerview:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.fragment_consultar_lista_nome, container, false);
listaCursos=new ArrayList<>();
recyclerCursos= (RecyclerView) vista.findViewById(R.id.idRecyclerNome); // se der problema, mude aqui
recyclerCursos.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerCursos.setHasFixedSize(true);
txt_titulo = (TextView) vista.findViewById(R.id.titulo);
professor = (TextView) vista.findViewById(R.id.Professor);
campoNome = (EditText) vista.findViewById(R.id.campoNome);
botaoConsultar = (Button) vista.findViewById(R.id.btnConsultar);
request= Volley.newRequestQueue(getContext());
botaoConsultar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
carregarWEBService();
}
});
return vista;
}
private void carregarWEBService() {
progresso = new ProgressDialog(getContext());
progresso.setMessage("Buscando...");
progresso.show();
String url = "http://192.168.0.5/webservices/consultarListaImagemUrlNome.php?nome="+ campoNome.getText().toString();
jsonObjectReq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
request.add(jsonObjectReq);
}
@Override
public void onErrorResponse(VolleyError error) {
progresso.hide();
Toast.makeText(getContext(), "Não foi possível listar os cursos " +error.toString() , Toast.LENGTH_SHORT).show();
Log.i("ERROR", error.toString());
}
@Override
public void onResponse(JSONObject response) {
progresso.hide();
Curso curso = null;
JSONArray json = response.optJSONArray("curso"); // nome da tabela curso
try {
for(int i=0; i<json.length();i++){
curso = new Curso();
JSONObject jsonObject = null;
jsonObject = json.getJSONObject(i);
curso.setNome(jsonObject.optString("nome"));
curso.setProfessor(jsonObject.optString("professor"));
curso.setCategoria(jsonObject.optString("categoria"));
curso.setDado(jsonObject.optString("imagem"));
listaCursos.add(curso);
}
progresso.hide();
CursosAdapterImg adapter = new CursosAdapterImg(listaCursos,getContext(),this);
recyclerCursos.setAdapter(adapter);
}catch (JSONException e){
e.printStackTrace();
progresso.hide();
Toast.makeText(getContext(), "Não foi possível listar os cursos " +response , Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNoteClick(int position) {
listaCursos.get(position);
//Falta passar os parâmetros
consultarCursoUrl fragmentConsulta = new consultarCursoUrl();
/* Estava testando nesse bloco passagem de parâmetros para outra fragment
Bundle arguments = new Bundle();
arguments.putString( "professor" , "Testando a passagem");
fragmentConsulta.setArguments(arguments); */
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main,fragmentConsulta).commit();
}
And this code below, is my screen that is called after touching the item of the Recycle:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.fragment_consultar_curso_url, container, false);
campoCodigo = vista.findViewById(R.id.codigo);
campoNome = vista.findViewById(R.id.txt_nome);
campoCategoria = vista.findViewById(R.id.txt_categoria);
campoProfessor = vista.findViewById(R.id.txt_professor);
btnAtualizar = vista.findViewById(R.id.btnAtualizar);
btnDeletar = vista.findViewById(R.id.btnDeletar);
btnConsultar = vista.findViewById(R.id.btnConsultar);
imgFoto = vista.findViewById(R.id.imagemId);
/* Estava fazendo teste de parametros nesse bloco
Bundle arguments = getArguments();
String nomeProfessor = arguments.getString("professor");
campoNome.setText(nomeProfessor);
*/
request = Volley.newRequestQueue(getContext());
btnConsultar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
carregarWEBService();
}
});
if(solicitarPermissoesVersoesSuperiores()){
imgFoto.setEnabled(true);
}else{
imgFoto.setEnabled(false);
}
imgFoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
carregarDialog();
}
});
btnAtualizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
carregarWEBServiceAtualizar();
}
});
btnDeletar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
carregarWEBServiceDeletar();
}
});
return vista;
}
Maybe it works well for you to use Viewmodel
– Murillo Comino