like Filter listview from an edittext

0

Help please I am new to this. with this code I can now bring my data from a local server now I want to filter that list with an edittext as I can do from here? What am I missing? Thank you very much for your cooperation.

package com.androidjson.serverupdate_androidjsoncom;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.*;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import android.os.AsyncTask;
import android.os.Bundle;

import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;

public class ShowAllStudentsActivity extends AppCompatActivity {

    ListView StudentListView;
    ProgressBar progressBar;

    EditText editName;
    String HttpUrl = "http://10.0.2.2:80/ejemploBDRemota/AllStudentData.php";
    List<String> IdList = new ArrayList<>();

    ListAdapterClass listAdapterClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_show_all_students);

        StudentListView = (ListView)findViewById(R.id.listview1);

        progressBar = (ProgressBar)findViewById(R.id.progressBar);

        editName = (EditText) findViewById(R.id.editName);

        StudentListView.setTextFilterEnabled(true);


        new GetHttpResponse(ShowAllStudentsActivity.this).execute();

        //Adding ListView Item click Listener.
        StudentListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // TODO Auto-generated method stub

                Intent intent = new Intent(ShowAllStudentsActivity.this,ShowSingleRecordActivity.class);

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

                startActivity(intent);

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

         editName.addTextChangedListener(new TextWatcher() {
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count, int after) {

             }

             @Override
             public void onTextChanged(CharSequence stringVar, int start, int before, int count) {




             }

             @Override
             public void afterTextChanged(Editable s) {

             }
         });



            }
        });

    }



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

        String JSonResult;

        List<Student> studentList;

        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(HttpUrl);
            try
            {
                httpServicesClass.ExecutePostRequest();

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

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

                        try {
                            jsonArray = new JSONArray(JSonResult);

                            JSONObject jsonObject;

                            Student student;

                            studentList = new ArrayList<Student>();

                            for(int i=0; i<jsonArray.length(); i++)
                            {
                                student = new Student();

                                jsonObject = jsonArray.getJSONObject(i);

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

                                //Adding Student Name.
                                student.StudentName = jsonObject.getString("Cliente").toString();
                                student.Studenmarcador = jsonObject.getString("Gano").toString();
                                studentList.add(student);

                            }
                        }
                        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);

            StudentListView.setVisibility(View.VISIBLE);

            ListAdapterClass adapter = new ListAdapterClass(studentList, context);

            StudentListView.setAdapter(adapter);

        }
    }
}
    
asked by juan perez 15.10.2017 в 18:34
source

1 answer

1

For that you will have to filter the list studentList and then then assign the adapter with the filtered values. For example you could send the value of EditText and then when you load the list you filter it:

private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
    public Context context;

    String JSonResult;

    List<Student> studentList;
    String valorFiltro ;

    public GetHttpResponse(Context context, String editTextValue)
    {
        this.context = context;
        this.valorFiltro = editTextValue;
    }

    //...

    @Override
    protected void onPostExecute(Void result){
        progressBar.setVisibility(View.GONE);

        StudentListView.setVisibility(View.VISIBLE);

        ArrayList<Student> filtrado = new ArrayList<>();
        for(Student s : studentList)
        {
            // verificamos si el nombre del estudiatne contiene lo que se escribio en el editText
             if(s.StudentName.contains(this.valorFiltro))
             {
                // es valido, lo agregamos a la lista
                filtrado.add(s);
             }

             ListAdapterClass adapter = new ListAdapterClass(filtrado, context);
             StudentListView.setAdapter(adapter);
        }

    }

}

Then the use of the GetHttpResponse class would be:

new GetHttpResponse(context, EditTextDelFiltro.getText().toString());

Although the correct thing would be to filter the results from the server but that is another issue.

    
answered by 15.10.2017 в 18:55