[postlink]
https://the-best-way-of-life-is-islam.blogspot.com/2015/07/my-androidstudio-app-doesnt-display.html
[/postlink]
i already parse a json String from my website and save it into an arraylist but when i run the app the elements doesnt show in the fragment, i already debug and the info does save it in the arraylist but it doesnt show it, i already check the similars topics but no one solve my problem, here is my code:
Adapter:
package com.example.luiggi.myapplication;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by andre_000 on 6/28/2015.
*/
public class FacturasAdapter extends ArrayAdapter<Invoices> {
Context context;
int layoutResourceId;
ArrayList<Invoices> data;
public FacturasAdapter(Context context, int layoutResourceId, ArrayList<Invoices> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
FacturasHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new FacturasHolder();
holder.shipping_id = (TextView) row.findViewById(R.id.shipping_id);
holder.company_id = (TextView) row.findViewById(R.id.company_id);
holder.user_id = (TextView) row.findViewById(R.id.user_id);
holder.driver_id = (TextView) row.findViewById(R.id.driver_id);
holder.id = (TextView) row.findViewById(R.id.id);
holder.fecha_factura = (TextView) row.findViewById(R.id.fecha_factura);
holder.monto = (TextView) row.findViewById(R.id.monto);
holder.precio_km = (TextView) row.findViewById(R.id.precio_km);
holder.nombre = (TextView) row.findViewById(R.id.nombre);
holder.direccion = (TextView) row.findViewById(R.id.direccion);
holder.telefono = (TextView) row.findViewById(R.id.telefono);
holder.estado = (TextView) row.findViewById(R.id.estado);
row.setTag(holder);
} else {
holder = (FacturasHolder) row.getTag();
}
Invoices pedido = data.get(position);
holder.shipping_id.setText(pedido.shipping_id);
holder.company_id.setText(pedido.company_id);
holder.user_id.setText(pedido.user_id);
holder.driver_id.setText(pedido.driver_id);
holder.id.setText(pedido.id);
holder.fecha_factura.setText(pedido.fecha_factura);
holder.monto.setText(pedido.monto);
holder.precio_km.setText(pedido.precio_km);
holder.nombre.setText(pedido.nombre);
holder.direccion.setText(pedido.direccion);
holder.telefono.setText(pedido.telefono);
holder.estado.setText(pedido.estado);
//final int pos = position;
/*row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PresupuestosFragment.presupuestoSeleccionado = PresupuestosFragment.listPresupuestos.get(pos);
new PresupuestoRepuestoTask().execute();
}
});
*/
return row;
}
static class FacturasHolder {
public TextView shipping_id;
public TextView company_id;
public TextView user_id;
public TextView driver_id;
public TextView id;
public TextView fecha_factura;
public TextView monto;
public TextView precio_km;
public TextView nombre;
public TextView direccion;
public TextView telefono;
public TextView estado;
}
}
Fragment:
package com.example.luiggi.myapplication;
import android.app.Activity;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
public class FacturasFragments extends Fragment {
View rootView;
private ListView ListFacturas;
//public static Presupuestos presupuestoSeleccionado;
//public static ArrayList<PresupuestoRepuesto> presupuestoRepuesto;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView=inflater.inflate(R.layout.facturas_fragments, container, false);
ListFacturas = (ListView)rootView.findViewById(R.id.ListFacturas);
Jsonclass conexion = new Jsonclass();
conexion.execute();
return rootView;
}
public class Jsonclass extends AsyncTask<Void, Void, String> {
public ArrayList<Invoices> listfacturas;
@Override
protected String doInBackground(Void... params) {
WebRequest facturas = null;
try {
facturas = new WebRequest(
"http://ift.tt/1HyuK3G"
);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
return facturas.getResponse();
}
@Override
protected void onPostExecute(final String result) {
//if (result != null) {
//Log.v("Prueba",result);
listfacturas = StringToFacturas(result);
ListFacturas.setAdapter(new FacturasAdapter(getActivity(),R.layout.facturasadapterview, listfacturas));
//} else {
//}
}
// TODO: Convertir string a JSON y luego a ArrayList<presupuestos>
private ArrayList<Invoices> StringToFacturas(String result) {
ArrayList<Invoices> listfacturas = new ArrayList<Invoices>();
try {
JSONArray presupuestos = new JSONArray(result);
for (int i = 0; i < presupuestos.length(); i++) {
//JSONArray p = presupuestos.getJSONObject(i).getJSONArray("presupuestos");
//for (int j = 0; j < p.length(); j++) {
Invoices invoice = new Invoices(
presupuestos.getJSONObject(i).getString("id"),
presupuestos.getJSONObject(i).getString("shipping_id"),
presupuestos.getJSONObject(i).getString("company_id"),
presupuestos.getJSONObject(i).getString("user_id"),
presupuestos.getJSONObject(i).getString("driver_id"),
presupuestos.getJSONObject(i).getString("fecha_factura"),
presupuestos.getJSONObject(i).getString("monto"),
presupuestos.getJSONObject(i).getString("precio_km"),
presupuestos.getJSONObject(i).getString("nombre"),
presupuestos.getJSONObject(i).getString("direccion"),
presupuestos.getJSONObject(i).getString("telefono"),
presupuestos.getJSONObject(i).getString("estado")
//p.getJSONObject(j).getJSONObject("usuario").getString("correo") + " (" + p.getJSONObject(j).getJSONObject("usuario").getString("nombre") + " " + p.getJSONObject(j).getJSONObject("usuario").getString("apellido") + ")"
);
listfacturas.add(invoice);
//}
}
//JSONObject jsonResponse = new JSONObject(result);
//Log.v("FINAPARTY", result);
//Log.v("FINAPARTY", presupuestos.getJSONObject(0).getString("fecha_registro"));
//Log.v("FINAPARTY", presupuestos.getJSONObject(0).getJSONArray("presupuestos").getJSONObject(0).getString("id_usuario"));
} catch (JSONException e) {
e.printStackTrace();
}
return listfacturas;
}
}
}
Fragment XML:
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListFacturas"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
Adapter XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/shipping_id"
android:layout_gravity="bottom"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/company_id"
android:layout_gravity="center_vertical"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="52dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/user_id"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="104dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/driver_id"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="156dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/id"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="208dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/fecha_factura"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="260dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/monto"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="312dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/precio_km"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="364dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/nombre"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="416dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/direccion"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="468dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/telefono"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="520dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/estado"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="572dp" />
</RelativeLayout>
Enregistrer un commentaire