ListView Android Custom

0

I am implementing a listview which I already have it running, but I need to change the data that is displayed. In the table of the database that I show, I have 10 data with a foreign id, and 10 data with another foreign id, and so on, and what I want is to filter the data that is shown in the listview according to the foreign id that enter that in the application, this step is the one that I do not know how to implement it.

Here I leave the code of the listview and adapter, and screenshots of the app

public class ListarEstacionamientos extends AppCompatActivity{

JSONParser jsonParser = new JSONParser();
ListView listaestacionamiento;
private ProgressDialog progressDialog;
ProgressBar progressBar;
String idparkingholder;


private static final String REGISTER_URL ="http://www.app.transportessalgado.cl/listarcupos.php";
List<String > idLista=new ArrayList<>();
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listar_estacionamientos);
    listaestacionamiento = (ListView) findViewById(R.id.idlistview);
    progressBar = (ProgressBar) findViewById(R.id.id_progressbar);

    new GetHttpResponse(ListarEstacionamientos.this).execute();
    idparkingholder=getIntent().getStringExtra("id_estacionamiento");
    //new GetHttpResponse(ListarEstacionamientos.this).execute();


    listaestacionamiento.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(ListarEstacionamientos.this, IngresoEstacionamientos.class);

            // Sending ListView clicked value using intent.
            intent.putExtra("valorLista", idLista.get(position).toString());


            startActivity(intent);

            //Finishing current activity after open next activity.
            finish();


        }
    });
}

// JSON parse class started from here.
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
    public Context context;

    String JSonResult;

    List<Cupos> cuposList;

    public GetHttpResponse(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0)
    {
        // Passing HTTP URL to HttpServicesClass Class.
        HttpServicesClass httpServicesClass = new HttpServicesClass(REGISTER_URL);
        try
        {
            httpServicesClass.ExecutePostRequest();

            if(httpServicesClass.getResponseCode() == 200)
            {
                JSonResult = httpServicesClass.getResponse();

                if(JSonResult != null)
                {
                    JSONArray jsonArray = null;

                    try {
                        jsonArray = new JSONArray(JSonResult);

                        JSONObject jsonObject;

                        Cupos cupos;

                       cuposList = new ArrayList<Cupos>();

                        for(int i=0; i<jsonArray.length(); i++)
                        {
                            cupos = new Cupos();

                            jsonObject = jsonArray.getJSONObject(i);

                            // Adding Student Id TO IdList Array.
                            idLista.add(jsonObject.getString("id_cupo").toString());

                            //Adding Student Name.
                            cupos.cuposEstacionamiento = jsonObject.getString("id_cupo").toString();
                            cupos.estado=jsonObject.getString("estado").toString();

                            cuposList.add(cupos);

                        }
                    }
                    catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            else
            {
                Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)

    {
        progressBar.setVisibility(View.GONE);

        listaestacionamiento.setVisibility(View.VISIBLE);

        ListAdapterClass adapter = new ListAdapterClass(cuposList, context);

        listaestacionamiento.setAdapter(adapter);

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menucerrarsesion, menu);
    return true;
}
}

and here the adapter

public class ListAdapterClass extends BaseAdapter {

Context context;
List<Cupos> valueList;

public ListAdapterClass(List<Cupos> listValue, Context context)
{
    this.context = context;
    this.valueList = listValue;
}

@Override
public int getCount()
{
    return this.valueList.size();
}

@Override
public Object getItem(int position)
{
    return this.valueList.get(position);
}

@Override
public long getItemId(int position)
{
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewItem viewItem = null;

    if(convertView == null)
    {
        viewItem = new ViewItem();

        LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        convertView = layoutInfiater.inflate(R.layout.vista_listview, null);

        viewItem.TextViewCupos = (TextView)convertView.findViewById(R.id.txtcupos);
        viewItem.estadoCupos=(TextView)convertView.findViewById(R.id.txtestados);

        convertView.setTag(viewItem);
    }
    else
    {
        viewItem = (ViewItem) convertView.getTag();
    }

    viewItem.TextViewCupos.setText(valueList.get(position).cuposEstacionamiento);
    viewItem.estadoCupos.setText(valueList.get(position).estado);

    return convertView;
}
}

class ViewItem
{
    TextView TextViewCupos;
    TextView estadoCupos;
}

and here the screenshots of the application

Here I get the parking id and what I want is for me to show the data associated with that id in the listview. Any idea how to do it please.

    
asked by Fernando Brito 04.07.2018 в 03:07
source

3 answers

0

Greetings!

You must create an Endpoint in your service which receives the foreign id parameter and perform the search in your DB.

Example:

// Opción 1
REGISTER_URL ="http://tudominio/listarcupos.php/3"
// Opción 2
REGISTER_URL ="http://tudominio/listarcupos.php?id=3"

Note: As an HTTP client I recommend you take a look at Retrofit

I hope it helped you out

    
answered by 04.07.2018 в 17:02
0

You are sending a string to your intent

intent.putExtra("valorLista", idLista.get(position).toString());

try to do this

Intent intent = new Intent(ListarEstacionamientos.this,             
IngresoEstacionamientos.class);

// intenta enviarlo como objeto java
intent.putExtra("valorLista", idLista.get(position))

startActivity(intent);

then you can get it in your activity

    
answered by 04.07.2018 в 18:17
0

I found a solution, which filters the data according to the ID that is entered, I leave the code in case someone has a similar problem.

 protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listar_estacionamientos);
    listaestacionamiento = (ListView) findViewById(R.id.idlistview);
    progressBar = (ProgressBar) findViewById(R.id.id_progressbar);
    eleccionestacionamiento = findViewById(R.id.ideleccionestacionamiento);

    SharedPreferences guardarRut = getBaseContext().getSharedPreferences("guardarID", MODE_PRIVATE);
    String userName = guardarRut.getString("ID", "");
    eleccionestacionamiento.setText(userName);

     String idestacionado=eleccionestacionamiento.getText().toString();

    new GetHttpResponse(ListarEstacionamientos.this, idestacionado).execute();





    listaestacionamiento.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(ListarEstacionamientos.this, EstadoReserva.class);

            // Sending ListView clicked value using intent.
            intent.putExtra("valorLista", idLista.get(position).toString());


            startActivity(intent);

            //Finishing current activity after open next activity.
            finish();


        }
    });
}

// JSON parse class started from here.
private class GetHttpResponse extends AsyncTask<Void, Void, Void> {
    public Context context;

    String JSonResult;
    String valorlista;
    String username;


    List<Cupos> cuposList;

    public GetHttpResponse(Context context, String idestacionado)
    //
    {
        this.context = context;
        this.valorlista = idestacionado;

        Log.d(TAG_MESSAGE, "onPostExecute: "+ idestacionado);

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }


    @Override
    protected Void doInBackground(Void... arg0) {
        // Passing HTTP URL to HttpServicesClass Class.
        HttpServicesClass httpServicesClass = new HttpServicesClass(REGISTER_URL);
        try {
            httpServicesClass.ExecutePostRequest();

            if (httpServicesClass.getResponseCode() == 200) {
                JSonResult = httpServicesClass.getResponse();

                if (JSonResult != null) {
                    JSONArray jsonArray = null;

                    try {
                        jsonArray = new JSONArray(JSonResult);

                        JSONObject jsonObject;

                        Cupos cupos;

                        cuposList = new ArrayList<Cupos>();

                        for (int i = 0; i < jsonArray.length(); i++) {
                            cupos = new Cupos();

                            jsonObject = jsonArray.getJSONObject(i);

                            // Adding Student Id TO IdList Array.
                            idLista.add(jsonObject.getString("id_cupo").toString());
                            //idLista.add(jsonObject.getString("estacionamiento_id_estacionamiento").toString());

                            //Adding Student Name.
                            cupos.cuposEstacionamiento = jsonObject.getString("id_cupo").toString();
                            cupos.estado = jsonObject.getString("estado").toString();
                            cupos.id_estacionamiento = jsonObject.getString("estacionamiento_id_estacionamiento").toString();

                            cuposList.add(cupos);
                        }

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } else {
                Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)

    {
        progressBar.setVisibility(View.GONE);
        listaestacionamiento.setVisibility(View.VISIBLE);
        ArrayList<Cupos> filtrado = new ArrayList<>();


        for (Cupos s : cuposList) {
            //verificamos si el nombre del estudiatne contiene lo que se escribio en el editText
            if (s.id_estacionamiento.equals(valorlista)) {
                // es valido, lo agregamos a la lista
                filtrado.add(s);
            }
            Log.d(TAG_MESSAGE, "onPostExecute: "+ s);

            ListAdapterClass adapter = new ListAdapterClass(filtrado, context);

            listaestacionamiento.setAdapter(adapter);

        }

    }

}
    
answered by 05.07.2018 в 08:49