I'm trying to visualize a toast inside a fragment by pressing an item of a recycling but I can not see it
Note: There is no error in the log attached to my class
This is my fragment
public class FotoFragment extends Fragment
{
private Cursor cursor;
private int columnIndex;
private static final String TAG = "RecyclerViewExample";
private List<DataPictures> mediaList = new ArrayList<>();
private RecyclerView mRecyclerView;
private MediaRVAdapter adapter;
String type = "";
public FotoFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
type = "images";
new MediaAsyncTask().execute(type);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
return view;
}
public class MediaAsyncTask extends AsyncTask<String, Void, Integer> {
@Override
protected Integer doInBackground(String... params) {
Integer result = 0;
String type = params[0];
try {
mediaList = new ArrayList<>();
if (type.equalsIgnoreCase("images")) {
result = 1;
}
} catch (Exception e) {
e.printStackTrace();
result = 0;
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
if (result == 1) {
adapter = new MediaRVAdapter(getActivity(), mediaList);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Click pressed",Toast.LENGTH_SHORT).show();
}
});
} else {
Log.e(TAG, "Failed to show list");
}
}
}
}
This is my adapter
public class MediaRVAdapter extends RecyclerView.Adapter<MediaListRowHolder> {
private List<DataPictures> itemList;
private Context mContext;
public MediaRVAdapter(Context context, List<DataPictures> itemList) {
this.itemList = itemList;
this.mContext = context;
}
@Override
public MediaListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
MediaListRowHolder mh = new MediaListRowHolder(v);
return mh;
}
@Override
public void onBindViewHolder(MediaListRowHolder mediaListRowHolder, int i) {
try{
DataPictures item = itemList.get(i);
Uri uri = Uri.fromFile(new File(item.getFilePath()));
if(item.getFileType().equalsIgnoreCase("video")) {
Bitmap bmThumbnail = ThumbnailUtils.
extractThumbnail(ThumbnailUtils.createVideoThumbnail(item.getFilePath(),
MediaStore.Video.Thumbnails.FULL_SCREEN_KIND), 90, 60);
if(bmThumbnail != null) {
mediaListRowHolder.thumbnail.setImageBitmap(bmThumbnail);
}
} else if (item.getFileType().equalsIgnoreCase("audio")) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(item.getFilePath());
try{
if (mmr != null) {
byte[] art = mmr.getEmbeddedPicture();
Bitmap bmp = BitmapFactory.decodeByteArray(art,0,art.length);
if(bmp != null) {
bmp= ThumbnailUtils.extractThumbnail(bmp,90,60);
mediaListRowHolder.thumbnail.setImageBitmap(bmp);
}
}
}catch (Exception e){
e.printStackTrace();
}
}else {
Picasso.with(mContext).load(uri)
.error(R.drawable.logo_slogan)
.placeholder(R.drawable.logo_slogan)
.centerCrop()
.resize(90, 60)
.into(mediaListRowHolder.thumbnail);
}
}catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return (null != itemList ? itemList.size() : 0);
}
}
Adapter class adapter
public class MediaListRowHolder extends RecyclerView.ViewHolder {
protected ImageView thumbnail;
protected TextView title;
public MediaListRowHolder(View view) {
super(view);
this.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
this.title = (TextView) view.findViewById(R.id.title);
}
}
solution:
public class MediaListRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected ImageView thumbnail;
protected TextView title;
public MediaListRowHolder(View view) {
super(view);
this.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
this.title = (TextView) view.findViewById(R.id.title);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "position = " + getPosition(), Toast.LENGTH_SHORT).show();
}
}