Android Studio // Get recycler text to perform a customized search in database

0

I have a problem with my android project, I can not get the text of a field in a recyclerview to perform a more personalized search. My project I'm doing with a single Activity and the rest are only fragments where in these fragments I make queries to a database (MySQL). To illustrate the problem I will show it with images.

In the image on the left I get some names from my database. To fill the fields occupy a recyclerview (with a fragment and its respective layout). The idea is that when I click on a name, I will take the fragment on the right and this one will tell me its details. The part of the moment I clicked on a name to the other fragment I have it done, but without loading the details, it only shows me the design (in the image I took the direct data and omitted the button on the right, I have to delete it, the idea is to charge alone).

I have searched how to perform the operation but I have not been able to do it. Not like referencing the names on the left to make the respective call.

My codes are as follows:

Left image:

public class ver_lineas extends Fragment implements Response.Listener<JSONObject>,Response.ErrorListener{

View vista;
JsonObjectRequest jsonObjectRequest;
ProgressDialog progreso;
ArrayList<Lineas> listaLineas;
RecyclerView recyclerLineas;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    vista = inflater.inflate(R.layout.fragment_ver_lineas, container, false);


    listaLineas=new ArrayList<>();

    recyclerLineas=vista.findViewById(R.id.recycler_lineas);
    recyclerLineas.setLayoutManager(new LinearLayoutManager(this.getContext()));
    recyclerLineas.setHasFixedSize(true);

    cargarWebService();

    return vista;
}

private void cargarWebService() {
    progreso=new ProgressDialog(getContext());
    progreso.setMessage("Consultando...");
    progreso.show();

    String ip= getString(R.string.ip);
    String url=ip+"/android/consulta_linea_btn.php?";

    jsonObjectRequest=new JsonObjectRequest(Request.Method.GET,url,null,this,this);
    VolleySingleton.getInstanciaVolley(getContext()).addToRequestQueue(jsonObjectRequest);
}

@Override
public void onErrorResponse(VolleyError volleyError) {
    progreso.hide();
    //Message no data in data base
    //Toast.makeText(getContext(),"No se pudo consultar "+volleyError.toString(),Toast.LENGTH_SHORT).show();
    Toast.makeText(getContext(),"No se encontrarón datos de línas",Toast.LENGTH_SHORT).show();
    Log.i("Error",volleyError.toString());
}

@Override
public void onResponse(JSONObject jsonObject) {
    progreso.hide();
    //Response message
    //Toast.makeText(getContext(),"Mensaje: "+jsonObject,Toast.LENGTH_SHORT).show();
    Toast.makeText(getContext(),"Datos consultados exitosamente",Toast.LENGTH_SHORT).show();

    Lineas lineas = null;

    JSONArray json = jsonObject.optJSONArray("LINEA");

    try {
        for (int i=0;i<json.length();i++) {
            lineas=new Lineas();
            JSONObject jsonObjecto= null;
            jsonObjecto=json.getJSONObject(i);
            lineas.setNombre_linea(jsonObjecto.optString("nombre_linea"));
            listaLineas.add(lineas);
        }
        progreso.hide();
        lineas_adapter adapter = new lineas_adapter(listaLineas);
        recyclerLineas.setAdapter(adapter);

        adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ver_lineas_detalle detalle_lineas = new ver_lineas_detalle();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.layout_contenido, detalle_lineas);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
    } catch (JSONException e) {
        e.printStackTrace();
        //Message no conection
        //Toast.makeText(getContext(), "No se ha establecido conexion con el servidor "+jsonObject, Toast.LENGTH_LONG).show();
        Toast.makeText(getContext(), "No se ha establecido conexion con el servidor", Toast.LENGTH_LONG).show();
    }
}
}

Right image:

public class ver_lineas_detalle extends Fragment implements 
Response.Listener<JSONObject>,Response.ErrorListener {
EditText id_linea;
TextView id_ciudad, nombre_linea, descripcion_linea;
Button btnConsulta, nom_linea;
ProgressDialog progreso;

RequestQueue request;
JsonObjectRequest jsonObjectRequest;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View vista = inflater.inflate(R.layout.fragment_ver_lineas_detalle, 
 container, false);
    //Reference to objects
    id_linea = vista.findViewById(R.id.text_res_linea_id);
    id_ciudad = vista.findViewById(R.id.text_res_ciudad_id);
    nombre_linea = vista.findViewById(R.id.text_res_linea_nombre);
    descripcion_linea = vista.findViewById(R.id.text_res_linea_descripcion);
    btnConsulta = vista.findViewById(R.id.btn_consulta_lineas_detalle);

    request = Volley.newRequestQueue(getContext());

    cargarWebService();
    return vista;
}

private void cargarWebService() {

    progreso=new ProgressDialog((getContext()));
    progreso.setMessage("Consultando...");
    progreso.show();

    String ip = getString(R.string.ip);

    //AQUI ME IMAGINO QUE DEBE IR ALGUNA VARIABLE, PERO NO SE CUAL

    String url=ip+"/android/test_lineas.php?nombre_linea=linea88";

    jsonObjectRequest=new JsonObjectRequest(Request.Method.GET,url,null, 
 this,this);
    request.add(jsonObjectRequest);
}

@Override
public void onErrorResponse(VolleyError volleyError) {
    progreso.hide();
    Toast.makeText(getContext(),"No se pudo consultar "+volleyError.toString(),Toast.LENGTH_SHORT).show();
    Log.i("Error",volleyError.toString());
}

@Override
public void onResponse(JSONObject jsonObject) {
    progreso.hide();
    //Show loaded data
    //Toast.makeText(getContext(),"Mensaje: "+jsonObject,Toast.LENGTH_LONG).show();

    Toast.makeText(getContext(), "Datos cargados exitosamente",Toast.LENGTH_SHORT).show();
    Lineas lineas = new Lineas();
    JSONArray json=jsonObject.optJSONArray("LINEA");
    JSONObject jsonObjecto= null;

    try {
        jsonObjecto = json.getJSONObject(0);
        //lineas.setId_linea(jsonObjecto.optString("id_linea"));
        lineas.setId_ciudad(jsonObjecto.optString("id_ciudad"));
        lineas.setNombre_linea(jsonObjecto.optString("nombre_linea"));
        lineas.setDescripcion_linea(jsonObjecto.optString("descripcion_linea"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    //Set text to variables
    id_linea.setText(lineas.getId_linea());
    id_ciudad.setText(lineas.getId_ciudad());
    nombre_linea.setText(lineas.getNombre_linea());
    descripcion_linea.setText(lineas.getDescripcion_linea());
}
}

The fragment XML on the left:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Negro"
android:orientation="vertical"
tools:context=".fragments.ver_lineas">


<LinearLayout
    android:id="@+id/layout_ver_lineas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_verinfo_lineas"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Ver información sobre líneas"
        android:textColor="@color/Blanco"
        android:textSize="28dp"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_lineas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

Adapter:

public class lineas_adapter extends RecyclerView.Adapter<lineas_adapter.LineasHolder> implements View.OnClickListener {


List<Lineas> listaLineas;
private View.OnClickListener listener;

public lineas_adapter(List<Lineas> listaLineas) {
    this.listaLineas = listaLineas;
}



@Override
public LineasHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View vista = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_list_buttons_lineas,parent,false);
    RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    vista.setLayoutParams(layoutParams);

    vista.setOnClickListener(this);
    return new LineasHolder(vista);
}

@Override
public void onBindViewHolder(LineasHolder lineasHolder, int position) {
    lineasHolder.txtLineas.setText((listaLineas.get(position).getNombre_linea()));
}

@Override
public int getItemCount() {

    return listaLineas.size();
}

public void setOnClickListener(View.OnClickListener listener) {
    this.listener = listener;
}

@Override
public void onClick(View v) {
    if(listener!=null) {
        listener.onClick(v);
    }

}


public class LineasHolder extends RecyclerView.ViewHolder {
    TextView txtLineas;

    public LineasHolder(View itemView) {
        super(itemView);
        txtLineas = itemView.findViewById(R.id.text_buttons_lineas);
    }
}
}

RecyclerView:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Negro"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/layout_buttons_lineas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_buttons_lineas"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="sample_text"
        android:textColor="@color/Blanco"
        android:textSize="28dp"
        android:textStyle="bold" />
</LinearLayout>

Thank you in advance, I'm attentive to any comments. Greetings.

    
asked by Mauricio Albrechtt 05.11.2018 в 01:14
source

0 answers