Android - Llenar un ListView desde otra clase

 
Vista:
sin imagen de perfil

Llenar un ListView desde otra clase

Publicado por Emilio (10 intervenciones) el 03/10/2017 03:06:48
Esta es la clase desde la que quiero llenar el Listview para ser mas preciso en ObtenerProductos()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import android.widget.Toast;
 
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tian.detapro.productos.Bean.ProductoBean;
import com.tian.detapro.productos.Bean.RespuestaBean;
 
import org.json.JSONArray;
 
import java.util.ArrayList;
 
import cz.msebera.android.httpclient.Header;
 
/**
 * Created by Paredes on 02/10/2017.
 */
 
public class Producto extends Activity {
 
    public void ObtenerProductos(){
        AsyncHttpClient client=new AsyncHttpClient();
        String url="http://hostwebonline.esy.es/Android/Dao/ProductoVer.php";
 
 
        RequestParams request=new RequestParams();
 
        client.get(url, request, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode==200){
                    if (ObtenerDatosProductos(new String(responseBody)).isEmpty()){
                        Toast toast = Toast.makeText(Producto.this,"No de pudo cargar los productos",Toast.LENGTH_SHORT);
                        toast.show();
                    }else {
                        ArrayList<String> lista=ObtenerDatosProductos(new String(responseBody));
 
 
                    }
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
 
            }
        });
 
    }
 
    public ArrayList<String> ObtenerDatosProductos(String response){
        ArrayList<String> listado=new ArrayList<String>();
        try{
            JSONArray jsonArray=new JSONArray(response);
            String datos;
 
            ArrayList<String> codigo = new ArrayList<>();
            ArrayList<String> nombre =new ArrayList<>();
            ArrayList<String> precio =new ArrayList<>();
            ArrayList<String> descripcion =new ArrayList<>();
            ArrayList<String> marca =new ArrayList<>();
            ArrayList<String> categoria =new ArrayList<>();
            ArrayList<String> cmarca =new ArrayList<>();
            ArrayList<String> ccategoria =new ArrayList<>();
            ArrayList<String> producto=new ArrayList<>();
 
            ProductoBean bean=new ProductoBean();
            for(int i=0;i<jsonArray.length();i++){
                datos=jsonArray.getJSONObject(i).getString("pro_nombre")+" "+
                    jsonArray.getJSONObject(i).getString("pro_descripcion")+" "+
                    jsonArray.getJSONObject(i).getString("cat_nombre")+" Precio:S/"+
                    jsonArray.getJSONObject(i).getString("pro_precio");
                producto.add(jsonArray.getJSONObject(i).getString("pro_nombre")+" "+
                        jsonArray.getJSONObject(i).getString("pro_descripcion")+" "+
                        jsonArray.getJSONObject(i).getString("cat_nombre")+" Precio:S/"+
                        jsonArray.getJSONObject(i).getString("pro_precio"));
                listado.add(datos);
 
                codigo.add(jsonArray.getJSONObject(i).getString("pro_cod"));
                nombre.add(jsonArray.getJSONObject(i).getString("pro_nombre"));
                precio.add(jsonArray.getJSONObject(i).getString("pro_precio"));
                descripcion.add(jsonArray.getJSONObject(i).getString("pro_descripcion"));
                marca.add(jsonArray.getJSONObject(i).getString("cat_nombre"));
                categoria.add(jsonArray.getJSONObject(i).getString("mar_nombre"));
                cmarca.add(jsonArray.getJSONObject(i).getString("mar_cod"));
                ccategoria.add(jsonArray.getJSONObject(i).getString("cat_cod"));
 
                bean.setCod(codigo);
                bean.setNom(nombre);
                bean.setPre(precio);
                bean.setDes(descripcion);
                bean.setMar(marca);
                bean.setCat(categoria);
                bean.setCmar(cmarca);
                bean.setCat(ccategoria);
                bean.setProducto(producto);
 
            }
        }catch (Exception e){
            e.printStackTrace();
        }
 
        return  listado;
    }
}

ahora esta en la clase donde estoy instanciando el ListView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.tian.detapro.productos.Fragmentos;
 
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
import android.widget.Button;
import android.widget.ListView;
 
import com.tian.detapro.productos.R;
 
import java.util.ArrayList;
 
 
 
/**
 * Created by Paredes on 01/10/2017.
 */
 
public class ConsultaRapida extends Fragment{
 
    ListView LST;
    Button BTNPR;
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
 
        final ArrayList<String> lst;
 
        View rootView = inflater.inflate(R.layout.frag_consulta_rapida, container, false);
 
        LST=(ListView) rootView.findViewById(R.id.LSTP);
 
        return rootView;
    }
}

lo que quiero que me expliquen o ayuden es lo siguiente la class Producto.java es donde cargo los datos del servidor y es donde quiero que se cargue el ListView que esta en ConsultaRapida.java y es necesario usar setter y getter este es el cogido que tengo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.tian.detapro.productos.Bean;
 
import java.util.ArrayList;
 
/**
 * Created by Paredes on 02/10/2017.
 */
 
public class ProductoBean {
    private static ArrayList<String> cod;
    private static ArrayList<String> nom;
    private static ArrayList<String> pre;
    private static ArrayList<String> des;
    private static ArrayList<String> mar;
    private static ArrayList<String> cat;
    private static ArrayList<String> cmar;
    private static ArrayList<String> ccat;
    private static ArrayList<String> producto;
 
 
    public static ArrayList<String> getCod() {
        return cod;
    }
 
    public static void setCod(ArrayList<String> cod) {
        ProductoBean.cod = cod;
    }
 
    public static ArrayList<String> getNom() {
        return nom;
    }
 
    public static void setNom(ArrayList<String> nom) {
        ProductoBean.nom = nom;
    }
 
    public static ArrayList<String> getPre() {
        return pre;
    }
 
    public static void setPre(ArrayList<String> pre) {
        ProductoBean.pre = pre;
    }
 
    public static ArrayList<String> getDes() {
        return des;
    }
 
    public static void setDes(ArrayList<String> des) {
        ProductoBean.des = des;
    }
 
    public static ArrayList<String> getMar() {
        return mar;
    }
 
    public static void setMar(ArrayList<String> mar) {
        ProductoBean.mar = mar;
    }
 
    public static ArrayList<String> getCat() {
        return cat;
    }
 
    public static void setCat(ArrayList<String> cat) {
        ProductoBean.cat = cat;
    }
 
    public static ArrayList<String> getCmar() {
        return cmar;
    }
 
    public static void setCmar(ArrayList<String> cmar) {
        ProductoBean.cmar = cmar;
    }
 
    public static ArrayList<String> getCcat() {
        return ccat;
    }
 
    public static void setCcat(ArrayList<String> ccat) {
        ProductoBean.ccat = ccat;
    }
 
 
    public static ArrayList<String> getProducto() {
        return producto;
    }
 
    public static void setProducto(ArrayList<String> producto) {
        ProductoBean.producto = producto;
    }
}

gracias por su ayuda
Gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder