Search function on Android GridView

Hope someone can help me. I am following this Android Search Filter ListView tutorial Images and Texts Tutorial with success. I try to implement it in my own activities and receive data from the server.

When I type inside the search box, the grid view becomes blank. Its as a list on the user 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 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

+6
source share
1 answer

When you create an adapter, in its constructor you add the contents of productList . At this point (adapter constructor), productList is most likely empty because the HTTP request to select json is not yet complete. Thus, you get an empty arraylist, and when you do any filtering on the adapter, you will not see anything, because there is nothing to filter.

Remember to update the arraylist (from the adapter) when the data finally enters the onResponse () callback, so you have a link to it to use it for filtering.

I would advise you to follow other tutorials.

Edit:

Add an “incremental” method to the adapter to add new elements:

 //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); 
+2
source

Source: https://habr.com/ru/post/989994/


All Articles