Filter listview from the edittext

0

Hello good afternoon please a genius to help me with this that I could not, I have this class that brings me mysql information when I open the activity but I'm trying to filter with edittext the loaded information I do not know what else should I do missing or that I modify. thank you very much if you can help me.

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://gadsistem.com/ejemploBDRemota/AllStudentDataEGIPTO.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) {


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

             }

             @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;
        String valorFiltro ;
        public GetHttpResponse(Context context)
        {
            this.context = context;
            this.valorFiltro = String.valueOf(editName);
        }

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

            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 adapter2 = new ListAdapterClass(filtrado, context);
                StudentListView.setAdapter(adapter);
            }


        }


        ///////////////////////////////////////

        /////////////////////////////////////////
    }
}
    
asked by juan perez 25.10.2017 в 00:27
source

0 answers