Search actionbar to filter array of JSON objects

0

I'm not able to implement a searchview in the actionbar to filter the listview items I get from a JSON. What could I use?

UserHomeActivity

package com.example.maxmendes.pokemonfindermax;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.ActionMenuItem;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class UserHomeActivity extends AppCompatActivity {
    ArrayList<Pokemon> arrayList;
    ListView lv;
    HorizontalScrollView sv;
    String elementoAtual;
    String elementoAtualAux;
    CustomListAdapterUserHome adapter;
    String[] nomePokemon = new String[951];
    String[] imagePokemon = new String[951];


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_home);

        ActionBar actionBar = getSupportActionBar();


        arrayList = new ArrayList<>();
        lv = (ListView) findViewById(R.id.listViewPokemonsId);


        LinearLayout elementos = findViewById(R.id.linearLayoutElementsId);
        LayoutInflater inflater = LayoutInflater.from(this);

        for (int i = 0; i < 18; i++) {
            View view = inflater.inflate(R.layout.elements_list, elementos, false);

            TextView textView = view.findViewById(R.id.textViewElementsId);
            ImageView imageView = view.findViewById(R.id.imageViewElementsId);


            elementoAtual = getIntent().getStringExtra("elementoEnviado");


            switch (i) {
                case 0:
                    textView.setText("Normal");
                    imageView.setImageResource(R.drawable.normal);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "normal");
                            startActivity(intent);
                        }
                    });

                    break;

                case 1:
                    textView.setText("Fighting");
                    imageView.setImageResource(R.drawable.fighting);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "fighting");
                            startActivity(intent);
                        }
                    });

                    break;

                case 2:
                    textView.setText("Flying");
                    imageView.setImageResource(R.drawable.flying);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "flying");
                            startActivity(intent);
                        }
                    });

                    break;

                case 3:
                    textView.setText("Poison");
                    imageView.setImageResource(R.drawable.poison);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "poison");
                            startActivity(intent);
                        }
                    });


                    break;

                case 4:
                    textView.setText("Ground");
                    imageView.setImageResource(R.drawable.ground);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "ground");
                            startActivity(intent);
                        }
                    });

                    break;

                case 5:
                    textView.setText("Rock");
                    imageView.setImageResource(R.drawable.rock);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "rock");
                            startActivity(intent);
                        }
                    });

                    break;

                case 6:
                    textView.setText("Bug");
                    imageView.setImageResource(R.drawable.bug);


                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "bug");
                            startActivity(intent);
                        }
                    });

                    break;

                case 7:
                    textView.setText("Ghost");
                    imageView.setImageResource(R.drawable.ghost);


                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "ghost");
                            startActivity(intent);
                        }
                    });

                    break;

                case 8:
                    textView.setText("Steel");
                    imageView.setImageResource(R.drawable.steel);


                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "steel");
                            startActivity(intent);
                        }
                    });

                    break;

                case 9:
                    textView.setText("Fire");
                    imageView.setImageResource(R.drawable.fire);


                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "fire");
                            startActivity(intent);
                        }
                    });

                    break;

                case 10:
                    textView.setText("Water");
                    imageView.setImageResource(R.drawable.water);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "water");
                            startActivity(intent);
                        }
                    });

                    break;

                case 11:
                    textView.setText("Grass");
                    imageView.setImageResource(R.drawable.grass);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "grass");
                            startActivity(intent);
                        }
                    });

                    break;

                case 12:
                    textView.setText("Electric");
                    imageView.setImageResource(R.drawable.electric);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "electric");
                            startActivity(intent);
                        }
                    });

                    break;

                case 13:
                    textView.setText("Psychic");
                    imageView.setImageResource(R.drawable.psychic);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "psychic");
                            startActivity(intent);
                        }
                    });

                    break;

                case 14:
                    textView.setText("Ice");
                    imageView.setImageResource(R.drawable.ice);


                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "ice");
                            startActivity(intent);
                        }
                    });

                    break;

                case 15:
                    textView.setText("Dragon");
                    imageView.setImageResource(R.drawable.dragon);


                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "dragon");
                            startActivity(intent);
                        }
                    });

                    break;

                case 16:
                    textView.setText("Dark");
                    imageView.setImageResource(R.drawable.dark);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "dark");
                            startActivity(intent);
                        }
                    });

                    break;

                case 17:
                    textView.setText("Fairy");
                    imageView.setImageResource(R.drawable.fairy);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(UserHomeActivity.this, UserHomeRefreshActivity.class);
                            intent.putExtra("elemento", "fairy");
                            startActivity(intent);
                        }
                    });

                    break;


            }

            elementos.addView(view);


        }


        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new ReadJSON().execute("https://vortigo.blob.core.windows.net/files/pokemon/data/pokemons.json");

            }
        });


    }


    class ReadJSON extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            return readURL(params[0]);
        }

        @Override
        protected void onPostExecute(String content) {
            try {
                JSONArray json = new JSONArray(content);

                Log.i("E: >>>", "Elemento recebido: " + elementoAtual);

                for (int i = 0; i < json.length(); i++) {
                    JSONObject pokemonObject = json.getJSONObject(i);
                    JSONArray pokemonArray = pokemonObject.getJSONArray("type");

                    if (elementoAtual == null) {

                        nomePokemon[i] = pokemonObject.getString("name");
                        imagePokemon[i] = pokemonObject.getString("thumbnailImage");

                        Log.i("NOME POKEMON: " , ">>>" + nomePokemon[i]);
                        Log.i("IMAGEM POKEMON: " , ">>>" + imagePokemon[i]);

                        arrayList.add(new Pokemon(
                                pokemonObject.getString("thumbnailImage"),
                                pokemonObject.getString("name")));

                    } else {
                        for (int j = 0; j < pokemonArray.length(); j++) {
                            if (pokemonArray.getString(j).contains(elementoAtual) || pokemonArray.getString(j).contains(elementoAtual)) {

                                arrayList.add(new Pokemon(
                                        pokemonObject.getString("thumbnailImage"),
                                        pokemonObject.getString("name")));
                            }
                        }

                    }

                }


            } catch (JSONException e) {
                e.printStackTrace();
            }

            adapter = new CustomListAdapterUserHome(
                    getApplicationContext(), R.layout.custom_list_userhome, arrayList
            );


            lv.setAdapter(adapter);
        }


        private String readURL(String theUrl) {
            StringBuilder content = new StringBuilder();
            try {
                // create a url object
                URL url = new URL(theUrl);
                // create a urlconnection object
                URLConnection urlConnection = url.openConnection();
                // wrap the urlconnection in a bufferedreader
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String line;
                // read from the urlconnection via the bufferedreader
                while ((line = bufferedReader.readLine()) != null) {
                    content.append(line + "\n");
                }
                bufferedReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return content.toString();
        }
    }

    //------------------------------------------------------------------------------------------

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_search , menu);
        MenuItem item = menu.findItem(R.id.menuSearch);
        SearchView searchView = (SearchView) item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                 Log.i("QUERY >>>", query);

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                Log.i("TEXTO >>>", newText);

                return true;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }
    //---------------------------------------------------------------------------------------------


}//final UserHome

CustomListAdapterUserHome

package com.example.maxmendes.pokemonfindermax;

import android.app.Activity;
import android.content.Context;
import android.graphics.ColorSpace;
import android.support.v4.view.MenuItemCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Locale;

public class CustomListAdapterUserHome extends ArrayAdapter<Pokemon> {
    ArrayList<Pokemon> pokemons;
    Context context;
    int resource;


    public CustomListAdapterUserHome(Context context, int resource, ArrayList<Pokemon> pokemons) {
        super(context, resource, pokemons);
        this.pokemons = pokemons;
        this.context = context;
        this.resource = resource;
    }//final  constructor CustomListAdapterUserHome

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.custom_list_userhome, null, true);
        }

        Pokemon pokemons = getItem(position);

        ImageView imageView = convertView.findViewById(R.id.pokemonImageId);
        Picasso.with(context).load(pokemons.getPokemonImage()).into(imageView);
        TextView pokemonName = convertView.findViewById(R.id.pokemonNameId);

        pokemonName.setText(pokemons.getPokemonName());


        return convertView;
    }

}
    
asked by anonymous 08.10.2018 / 07:51

0 answers