Estoy tratando de crear una vista con una lista de registros que ya tengo en la base de datos, estoy trabajando con fragment
, el cual tengo tengo un ListView
donde quiero mostrar el código del producto y descripción del producto, a lo hora de compilar solo me muestra el interfaz mas no los registros que están almacenados.
listViveres.php
<?php
include('conexion.php');
$result = array();
$result['t_viveres'] = array();
$sql = "SELECT * FROM t_viveres";
$response = $mysql->query ($sql);
while ($row = mysqli_fetch_array($response)) {
$index['codigoViveres'] = $row['1'];
$index['descriProducto'] = $row['2'];
$index['tipoClasificacion'] = $row['3'];
$index['cantidad'] = $row['4'];
$index['unidadMedida'] = $row['5'];
$index['fechaRegistro'] = $row['6'];
$index['fechaCaducidad'] = $row['7'];
array_push($result['t_viveres'],$index);
}
$result["success"]="1";
echo json_encode($result);
mysqli_close($mysql);
?>
list_viveres.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/id_codigo"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="1"
android:textColor="@color/white"
android:gravity="center_vertical|center"
android:textSize="20sp"
android:background="@drawable/rectangulo"/>
<TextView
android:id="@+id/id_producto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Descripción de producto"
android:gravity="center_vertical"
android:textSize="18sp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
fragment_lista_viveres.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="FragmentAlbergues.Lista_viveres">
<ListView
android:id="@+id/id_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Lista_viveres.java
public class Lista_viveres extends Fragment {
ListView listView;
AdapterVivere adapter;
public static ArrayList<Vivere> vivereArrayList = new ArrayList<>();
String Url ="http://192.168.4.32/LogisHuman/listViveres.php";
Vivere vivere;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.fragment_lista_viveres, container, false);
return vista;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = view.findViewById(R.id.id_listView);
adapter = new AdapterVivere(getActivity(),vivereArrayList);
listView.setAdapter(adapter);
mostrarLista();
}
public void mostrarLista(){
StringRequest request = new StringRequest(Request.Method.POST, Url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
vivereArrayList.clear();
try {
JSONObject jsonObject = new JSONObject(response);
String sucess = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("t_viveres");
if (sucess.equals("1")){
for (int i=0;i<jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String codigoViveres = object.getString("codigoViveres");
String descriProducto = object.getString("descriProducto");
String tipoClasificacion = object.getString("tipoClasificacion");
String cantidad = object.getString("cantidad");
String unidadMedida = object.getString("unidadMedida");
String fechaRegistro = object.getString("fechaRegistro");
String fechaCaducidad = object.getString("fechaCaducidad");
vivere = new Vivere(codigoViveres,descriProducto,tipoClasificacion,
cantidad,unidadMedida,fechaRegistro,fechaCaducidad);
vivereArrayList.add(vivere);
adapter.notifyDataSetChanged();
}
}
}catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
}
}
AdapterVivere.java
public class AdapterVivere extends ArrayAdapter<Vivere> {
Context context;
List<Vivere> viveresList;
public AdapterVivere(@NonNull Context context, List<Vivere> viveresList) {
super(context, R.layout.list_viveres,viveresList);
this.context = context;
this.viveresList = viveresList;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vista = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_viveres,null, true);
TextView textcodigo = (TextView) vista.findViewById(R.id.id_codigo);
TextView textproducto = (TextView) vista.findViewById(R.id.id_producto);
textcodigo.setText(viveresList.get(position).getCodigoViveres());
textproducto.setText(viveresList.get(position).getDescriProducto());
return vista;
}
}
Captura donde solo me muestra el interfaz más no los datos