Hello Everyone I am trying to start activity when I click on RecyclerView
, it is started when I add the code in the Custom Adapter , but when I write it in Activity it Just give me the position on Log ..
so any help
here is the code OnClick in activity and that's what I tried to do
@Override
public void onItemClick(int position, View v) {
Log.e("TAG", "You clicked number " + mAdapter.getItemId(position) + ", which is at cell position " + position);
// this.startActivity(new Intent(FrameListActivity.this , FinalActivity.class));
// Bitmap frameSelected = results.get(position).getImage();
// Log.e("frameSelected" , frameSelected+"");
// createImageFromBitmap(frameSelected , "frameImage");
positionId = "" + position;
Context context = v.getContext();
Intent intent = new Intent(context.getApplicationContext() , FinalActivity.class);
intent.putExtra("resultpos", "" + positionId);
context.getApplicationContext().startActivity(intent);
finish();
}
here is the activity
package com.abed.montage.hijabapptest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.kbeanie.imagechooser.api.ImageChooserManager;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
public class FrameListActivity extends AppCompatActivity implements PhotoRecyclerViewAdapter.MyClickListener {
private RecyclerView photoRecyclerView;
private RecyclerView.Adapter mAdapter;
private List<Integer> framePhotoList ;
List<PhotoClass> results ;
private static String LOG_TAG = "CardViewActivity";
String positionId ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_list);
photoRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
photoRecyclerView.setHasFixedSize(true);
int numberOfColumns = 2;
// mLayoutManager = new LinearLayoutManager();
photoRecyclerView.setLayoutManager(new GridLayoutManager(this , numberOfColumns));
mAdapter = new PhotoRecyclerViewAdapter(getDataSet() , this);
photoRecyclerView.setAdapter(mAdapter);
}
@Override
protected void onResume() {
super.onResume();
((PhotoRecyclerViewAdapter) mAdapter).setOnItemClickListener(new PhotoRecyclerViewAdapter
.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
Log.i(LOG_TAG, " Clicked on Item " + position);
}
});
}
private List<PhotoClass> getDataSet() {
fillPhotoFrameList();
results = new ArrayList<>();
for (int index = 0; index < framePhotoList.size(); index++) {
PhotoClass obj = new PhotoClass();
obj.setImage(framePhotoList.get(index));
results.add(index, obj);
}
return results;
}
private void fillPhotoFrameList(){
framePhotoList = new ArrayList<>();
framePhotoList.add(R.drawable.frame_1);
framePhotoList.add(R.drawable.frame_2);
framePhotoList.add(R.drawable.frame_3);
framePhotoList.add(R.drawable.frame_4);
framePhotoList.add(R.drawable.frame_5);
framePhotoList.add(R.drawable.frame_6);
framePhotoList.add(R.drawable.frame_7);
framePhotoList.add(R.drawable.frame_8);
framePhotoList.add(R.drawable.frame_2);
framePhotoList.add(R.drawable.frame_10);
framePhotoList.add(R.drawable.frame_11);
framePhotoList.add(R.drawable.frame_12);
framePhotoList.add(R.drawable.frame_13);
framePhotoList.add(R.drawable.frame_14);
framePhotoList.add(R.drawable.frame_15);
framePhotoList.add(R.drawable.frame_16);
framePhotoList.add(R.drawable.frame_17);
framePhotoList.add(R.drawable.frame_18);
framePhotoList.add(R.drawable.frame_19);
framePhotoList.add(R.drawable.frame_20);
}
@Override
public void onItemClick(int position, View v) {
Log.e("TAG", "You clicked number " + mAdapter.getItemId(position) + ", which is at cell position " + position);
// this.startActivity(new Intent(FrameListActivity.this , FinalActivity.class));
// Bitmap frameSelected = results.get(position).getImage();
// Log.e("frameSelected" , frameSelected+"");
// createImageFromBitmap(frameSelected , "frameImage");
positionId = "" + position;
Context context = v.getContext();
Intent intent = new Intent( context , FinalActivity.class);
intent.putExtra("resultpos", "" + positionId);
context.startActivity(intent);
finish();;
}
public String createImageFromBitmap(Bitmap bitmap , String fileNameForSave) {
String fileName = fileNameForSave;//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
}
and here is the Adapter ....
package com.abed.montage.hijabapptest;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.List;
/**
* Created by hp15-AY on 02/27/2017.
*/
public class PhotoRecyclerViewAdapter extends RecyclerView.Adapter<PhotoRecyclerViewAdapter.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private List<PhotoClass> mDataset;
private static MyClickListener myClickListener;
Context context;
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
ImageView photo;
public DataObjectHolder(View itemView) {
super(itemView);
photo = (ImageView) itemView.findViewById(R.id.photo);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
// Context context = v.getContext();
// Intent intent = new Intent(context, FinalActivity.class);
// context.startActivity(intent);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public PhotoRecyclerViewAdapter(List<PhotoClass> myDataset , Context context) {
this.context = context ;
mDataset = myDataset;
}
@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview_photo, parent, false);
view.setBackgroundResource(R.drawable.rounded_frame);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.photo.setImageBitmap(BitmapFactory.decodeResource(context.getResources() ,mDataset.get(position).getImage()));
}
public void addItem(PhotoClass dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
@Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
and here is the class ...
public class PhotoClass {
int id;
int image ;
public PhotoClass() {
}
public void setId(int id) {
this.id = id;
}
public void setImage(int image) {
this.image = image;
}
public int getId() {
return id;
}
public int getImage() {
return image;
}
}
FinalActivity OnCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Activity Created");
setContentView(R.layout.activity_final);
mGlobal = (Global) getApplicationContext();
imageViewThumbnail = (ImageView) findViewById(R.id.selectedimage);
saveIcon = (ImageView) findViewById(R.id.imageSave);
filterIcon = (ImageView) findViewById(R.id.editImage);
mImageViewFrame = (ImageView) findViewById(R.id.frameimage);
// mInterstitialAd = new InterstitialAd(this);
// mInterstitialAd.setAdUnitId(getApplicationContext().getString(R.string.admob_intersitials));
// mAdRequest= new AdRequest.Builder().build();
// mInterstitialAd.loadAd(mAdRequest);
imageViewThumbnail.setOnTouchListener(new MultiTouchListener());
mDialog = Utils.SetProgressBar(mDialog, FinalActivity.this);
mDialog.dismiss();
loadImage(imageViewThumbnail, mGlobal.getPath());
String result = getIntent().getStringExtra("resultpos");
pos = Integer.parseInt(result);
Log.w("Position===", "" + pos);
String imagevalue = frames[pos];
Log.w("imagename=====++++", "" + imagevalue);
int resID = getResources().getIdentifier(imagevalue, "drawable", getPackageName());
mImageViewFrame.setImageResource(resID);
saveIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// if (mInterstitialAd.isLoaded()) {
// mInterstitialAd.show();
// }
RelativeLayout view = (RelativeLayout) findViewById(R.id.layout);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
SaveImage(bm);
}
});
filterIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout view = (RelativeLayout) findViewById(R.id.layout);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
EditImage(bm);
}
});}
loadImage Function :
private void loadImage(ImageView iv, final String path) {
if(!isAirplaneModeOn(this.getApplicationContext())) {
Picasso.with(FinalActivity.this)
.load(Uri.fromFile(new File(path)))
.fit()
.centerInside()
.into(iv, new Callback() {
@Override
public void onSuccess() {
Log.i(TAG, "Picasso Success Loading Thumbnail - " + path);
}
@Override
public void onError() {
Log.i(TAG, "Picasso Error Loading Thumbnail Small - " + path);
}
});
}
else{
//do something else?
}
File image = new File(path);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
engrave(bitmap);
// bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
}
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isAirplaneModeOn(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}
}