Okay so I am making this app that launch's games but I cannot seem to get the search functionality for my GridView (named grid) to work properly. Every time I search for a game and then tap the game I want to launch instead of launching the correct game it launches a different game. Before I added the search functionality when I tapped on a game it would launch the correct one because the position number was always the same but now with the search functionality the grid positions of the games are now different from the order of my ArrayList (named Games) causing the wrong game to launch. The only time it launches the correct game is when I don't use the search at all or leave the field blank.
Code in MainActivity
//Launch Game
grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
try
{
String appToLaunch = Games.get(position).getAppPackage();
Intent launchGame = new Intent();
launchGame = getPackageManager().getLaunchIntentForPackage(appToLaunch);
startActivity(launchGame);
Toast.makeText(getApplicationContext(),"Launched " Games.get(position).getTitle() ,Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(),"Error Failed to Launch " Games.get(position).getTitle() ". Make sure that app package is correct",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
ImageAdapter Class for my grid view
public class ImageAdapter extends BaseAdapter implements Filterable
{
private Context context;
ArrayList<Game> Games;
ArrayList<Game> GamesFiltered;
public ImageAdapter(Context context, ArrayList<Game> Games)
{
this.context = context;
this.Games = Games;
this.GamesFiltered = new ArrayList<>(Games);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// get layout from custom_gridview.xml
View gridView = inflater.inflate(R.layout.custom_gridview, null);
// set value into imageview
ImageView image = gridView.findViewById(R.id.item_image);
Bitmap bm = GamesFiltered.get(position).getImage();
if(bm != null)
image.setImageBitmap(bm);
else
image.setImageResource(R.drawable.blank);
// set value into textview
TextView text = gridView.findViewById(R.id.item_text);
text.setText(GamesFiltered.get(position).getTitle());
return gridView;
}
@Override
public int getCount()
{
return GamesFiltered.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public Filter getFilter()
{
Filter filter = new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence charSequence)
{
FilterResults filterResults = new FilterResults();
if(charSequence == null || charSequence.length() ==0)
{
filterResults.count = Games.size();
filterResults.values = Games;
}
else
{
String searchStr = charSequence.toString();
ArrayList<Game> resultData = new ArrayList<>();
for(Game game: Games)
{
if(game.getTitle().contains(searchStr))
{
resultData.add(game);
}
filterResults.count = resultData.size();
filterResults.values = resultData;
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults)
{
GamesFiltered = (ArrayList<Game>) filterResults.values;
notifyDataSetChanged();
}
};
return filter;
}
}
Edit here is my Game Class
public class Game
{
private @NonNull String appPackage;
private @NonNull String title;
private Bitmap image;
public Game(@NonNull String appPackage,@NonNull String title,Bitmap image)
{
this.appPackage = appPackage;
this.title = title;
this.image = image;
}
public void setAppPackage(@NonNull String appPackage){this.appPackage = appPackage;}
public String getAppPackage(){return this.appPackage;}
public void setTitle(@NonNull String title){this.title = title;}
public String getTitle() {return this.title;}
public Bitmap getImage () { return this.image; }
public void setImage(Bitmap image) { this.image = image;}
}
CodePudding user response:
Probably you need to use list of filtered items to get element in your onItemClick
method:
String appToLaunch = GamesFiltered.get(position).getAppPackage();
instead of
String appToLaunch = Games.get(position).getAppPackage();