Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Function On GridView Android

I've been following this tutorial Android Search Filter ListView Images and Texts Tutorial with success. I'm trying to implement it in my own activity and get data from the server.

When I'm typing inside the search field, the grid view becomes empty. It's like the List at custom adapter becomes null.

my activity Class

package com.danz.tensai.catalog;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.danz.tensai.catalog.app.MyApplication;
import com.danz.tensai.catalog.helper.Product;
import com.danz.tensai.catalog.helper.SwipeListAdapter;

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

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainActivity extends ActionBarActivity {

    private String TAG = MainActivity.class.getSimpleName();

    private String URL_TOP_250 = "http://danztensai.hostoi.com/imdb_top_250.php?offset=";

    private SwipeRefreshLayout swipeRefreshLayout;
    //private ListView listView;
    private GridView gridView;
    private SwipeListAdapter adapter;
    private List<Product> productList;
    private ProgressBar spinner;
    EditText editsearch;

    private int offSet = 0;

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


        gridView = (GridView)findViewById(R.id.gridViewListProduct);


        productList = new ArrayList<>();
        fetchProduct();
        adapter = new SwipeListAdapter(this, productList);
        gridView.setAdapter(adapter);

        editsearch = (EditText)findViewById(R.id.search);

        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                Log.d(TAG,"Text To Search : "+text);
                adapter.filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                // TODO Auto-generated method stub
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void fetchProduct() {

        spinner = (ProgressBar)findViewById(R.id.progressBar1);
        // appending offset to url
        String url = URL_TOP_250 + offSet;

        // Volley's json array request object
        JsonArrayRequest req = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        if (response.length() > 0) {

                            // looping through json and adding to movies list
                            for (int i = 0; i < response.length(); i++) {
                                try {
                                     JSONObject movieObj = response.getJSONObject(i);

                                    int rank = movieObj.getInt("rank");
                                    String title = movieObj.getString("title");
                                    String imageURL = movieObj.getString("imageURL");
                                    Product m = new Product(rank, title,imageURL);

                                    productList.add(0, m);

                                    // updating offset value to highest value
                                    if (rank >= offSet)
                                        offSet = rank;

                                } catch (JSONException e) {
                                    Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                                }
                            }

                           // adapter.notifyDataSetChanged();
                        }

                        // stopping swipe refresh
                       // swipeRefreshLayout.setRefreshing(false);
                        spinner.setVisibility(View.GONE);

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Server Error: " + error.getMessage());

                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

                // stopping swipe refresh
            //    swipeRefreshLayout.setRefreshing(false);
                spinner.setVisibility(View.GONE);
            }
        });

        // Adding request to request queue
        Log.e(TAG,req.toString() );
        MyApplication.getInstance().addToRequestQueue(req);
    }
}

and my Custom Adapter

public class SwipeListAdapter extends BaseAdapter {
    private Activity activity;
     LayoutInflater inflater;
    Context mContext;
    private List<Product> productList;
    private ArrayList<Product> arraylist;
    private String TAG = SwipeListAdapter.class.getSimpleName();

    //private String[] bgColors;

    public SwipeListAdapter(Context context, List<Product> productList) {
        //this.activity = activity;
        mContext = context;
        this.productList = productList;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<Product>();
        this.arraylist.addAll(productList);
      //  bgColors = activity.getApplicationContext().getResources().getStringArray(R.array.movie_serial_bg);
    }

    public class ViewHolder{
        ImageView productImage;
    }

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

    @Override
    public Object getItem(int location) {
        return productList.get(location);
    }

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


    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_row,null);
            holder.productImage = (ImageView) convertView.findViewById(R.id.productImage);
            convertView.setTag(holder);
        }else
        {
            holder
                     = (ViewHolder) convertView.getTag();
        }
        new DownloadImageTask((holder.productImage))
                .execute(productList.get(position).imageURL);
        return convertView;
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        productList.clear();
        if (charText.length() == 0) {
            productList.addAll(arraylist);
        } else {
            for (Product wp : arraylist) {
                if (wp.getTitle().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    productList.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }
}

Search Function

like image 242
dmh Avatar asked Jun 30 '15 08:06

dmh


1 Answers

When you're creating the adapter, in its constructor, you add to the arraylist list the content of productList. At that point(the adapter constructor) the productList is most likely empty as the http request to fetch the json hasn't finished yet. So you end up with an empty arraylist and when you do any filtering on the adapter you'll not see anything because there' nothing to filter.

Don't forget to update the arraylist(from the adapter) when the data finally comes in the onResponse() callback so you have a reference to to it to use it for filtering.

I would advise you to follow other tutorials.

Edit:

Add a "add" method to the adapter to insert the new items:

//In the SwipeListAdapter class add
public void add(Product p) {
    productList.add(0, p);
    arraylist.add(0, p);
    notifyDataSetChyanged();
}

Then in your activity, instead of:

productList.add(0, m);

call:

adapter.add(m);
like image 104
user Avatar answered Nov 14 '22 23:11

user