I want to search my GridView list items which include image and a text field. Please help me out that I can search from my GridView either from search view or edit text from the custom list adapter filter method. Thanks in advance.
This is my main activity 'XML'.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#e9e9e9">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:columnWidth="200dp"
android:numColumns="2"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center|bottom"
android:drawSelectorOnTop="true"
/>
</android.support.constraint.ConstraintLayout>
Custom Array adapter java class
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ItemArrayAdapter extends ArrayAdapter {
Context context;
private List scoreList = new ArrayList();
private int [] images;
static class ItemViewHolder {
public TextView episodeName;
public TextView url;
public ImageView icon;
TextView name;
TextView score;
}
public ItemArrayAdapter(Context context, int textViewResourceId, int []
images) {
super(context, textViewResourceId);
this.images = images;
this.context = context;
}
public void add(Object object) {
scoreList.add(object);
super.add(object);
}
@Override
public int getCount() {
return this.scoreList.size();
}
@Override
public String[] getItem(int index) {
return (String[]) this.scoreList.get(index);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemViewHolder viewHolder;
if (convertView== null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.single_list_item, parent,
false);
viewHolder = new ItemViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.name);
viewHolder.score = (TextView) convertView.findViewById(R.id.score);
viewHolder.icon = (ImageView)
convertView.findViewById(R.id.appIconIV);
convertView.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder)convertView.getTag();
}
try {
String[] stat = getItem(position);
viewHolder.name.setText(stat[0]);
viewHolder.score.setText(stat[1]);
viewHolder.icon.setImageResource(images[position]);
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
}
Main Activity java class
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListView;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import java.io.InputStream;
import java.util.List;
public class MainActivity extends BaseActivity {
private GridView gridView;
private InterstitialAd mInterstitialAd;
private ItemArrayAdapter itemArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
try {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout)
findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_main,
contentFrameLayout);
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
gridView = findViewById(R.id.listview);
itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(),
R.layout.single_list_item, images);
Parcelable state = gridView.onSaveInstanceState();
gridView.setAdapter(itemArrayAdapter);
gridView.onRestoreInstanceState(state);
InputStream inputStream =
getResources().openRawResource(R.raw.stats);
CSVFile csvFile = new CSVFile(inputStream);
List scoreList = csvFile.read();
for(Object scoreData:scoreList ) {
itemArrayAdapter.add(scoreData);
}
}
Comments
Post a Comment