android


Sending Image from recycler view to Other Activity


I have a one Grid RecyclerView with image , now I want when user click on the recycler View , RecyclerView image show in the DetailActivity .
This is my RecyclerAdapter :
public class AdapterRecyclerDars extends RecyclerView.Adapter<AdapterRecyclerDars.ViewHolder> {
private ArrayList<Modelars> arsha;
private Context context;
//========================================== constructor
public AdapterRecyclerDars(ArrayList<Modelars> arsha, Context context) {
this.arsha = arsha;
this.context = context;
}
//========================================== ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView txt_grid;
private ImageView img_grid;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_grid = (TextView)itemView.findViewById(R.id.txt_grid);
img_grid = (ImageView)itemView.findViewById(R.id.img_grid);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), DetailActivity.class);
intent.putExtra("img_id",img_grid.getId()); <== I don't know this
v.getContext().startActivity(intent);
}
}
//========================================== onCreateViewHolder
#Override
public AdapterRecyclerDars.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_row, parent, false);
return new ViewHolder(view);
}
//========================================== onBindViewHolder
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
//setText
holder.txt_grid.setText(arsha.get(position).getars_name());
//download image
Glide.with(context).load(arsha.get(position).getars_img()).into(holder.img_grid);
}
//========================================== getItemcount
#Override
public int getItemCount() {
return darsha.size();
}
//==========================================
//==========================================
}
And This is my Fragment where RecyclerView Load :
public class Fragment_one extends Fragment {
ArrayList<Modelars> arsha;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
RecyclerView recyclerAvalDab = (RecyclerView) view.findViewById(R.id.recycler_frg_aval_dab);
// recyclerAvalDab.setHasFixedSize(true);
initializeData();
RecyclerView.LayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
recyclerAvalDab.setLayoutManager(gridLayoutManager);
AdapterRecyclerDars adapterRecyclerDars = new AdapterRecyclerDars(darsha, getActivity());
recyclerAvalDab.setAdapter(adapterRecyclerDars);
return view;
}
private void initializeData() {
arsha = new ArrayList<>();
arsha.add(new Modelars("1", R.drawable.z));
arsha.add(new Modelars("2", R.drawable.s));
arsha.add(new Modelars("3", R.drawable.r));
arsha.add(new Modelars("4", R.drawable.f));
arsha.add(new Modelars("5", R.drawable.a));
arsha.add(new Modelars("6", R.drawable.ad));
arsha.add(new Modelars("7", R.drawable.d));
arsha.add(new Modelars("8", R.drawable.z));
}
}
Try this i think work :
Just add These line to your ViewHolder in your RecyclerAdapter:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView txt_grid;
private ImageView img_grid;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt_grid = (TextView)itemView.findViewById(R.id.txt_grid);
img_grid = (ImageView)itemView.findViewById(R.id.img_grid);
}
#Override
public void onClick(View v) {
Intent intent=new Intent(v.getContext(),DetailActivity.class);
Bundle bundle=new Bundle();
bundle.putInt("IMAGE", arsha.get(getAdapterPosition()).getars_img());
intent.putExtras(bundle);
v.getContext().startActivity(intent);
}
}
And in your DetailActivity write these code in onCreate method :
int img_id;
if (bundle != null ){
img_id = bundle.getInt("IMAGE");
imageview.setImageResource(img_id);
}
That's it :D
Sending Image from recycler view to Other Activity
Instead of using img_grid.getId() to get image in next Activity, use setTag method of img_grid ImageView to store image resource id with each cell like change onBindViewHolder method as:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
....your code here
//download image
int drawableID=arsha.get(position).getars_img();
holder.img_grid.setTag(String.valueOf(drawableID));
Glide.with(context).load(drawableID).into(holder.img_grid);
}
Now pass drawableID to next Activity as:
intent.putExtra("drawableID",Integer.parseInt(img_grid.getTag().toString()));
v.getContext().startActivity(intent);
In DetailActivity Activity show image as:
int drawableID = bundle.getInt("drawableID"));
imageView.setImageResource(drawableID);

Related Links

Android AudioTrack only plays first 2 seconds back, then stops
Embedd inline images using JAVA Mail in android?
Facebook MultiLogin in Android Webview
Is it possible to detect that the user will turn off the Bluetooth on Android?
Android Abort BroadcastReceiver
Opening Arabic-named files in Android
How is an Intent Service Declared in the Android Manifest?
How to create sub AlertDialog/Dialog box in Android?
how to force close the currently running application in android
Interface in android same as Persistable in Blackberry
ViewRoot$CalledFromWrongThreadException when trying to make spinner visible
is there a panorama or pivot UI control on android similar to the ones on windows phone 7?
Storing images - mysql versus file system
Install .apk without User action
Android Load and run apk file on sdcard
Error deserializing parameter in WCF service from Android kSOAP

Categories

HOME
batch-file
c++11
time-complexity
c99
datastore
angular-formly
angular2-material
devise
opendj
onclick
missing-data
webix
qpython
owin
pega
code-generation
entitymanager
glyphicons
aws-sdk
red5
soci
charles
spring-async
angular-routing
roundup
jboss6.x
tiki-wiki
siri
slidetoggle
licensing
appirater
vex
ntp
alm
kudu
hibernate-validator
android-sugarorm
android-gridview
twos-complement
getpixel
sql-delete
sql-server-ce
metawidget
plaintext
claims
congestion-control
outputcache
drupal-theming
uiimage
jboss-4.2.x
identification
trojan
corruption
context-sensitive-grammar
pybrain
mathcad
baidu
vibrate
rxtx
branch-prediction
change-tracking
chromium-os
winusb
qtplugin
beamer
mapinfo
xdoclet
nss
php-socket
aurora
ascii-art
crouton-os
jbase
ess
pax-exam
rdflib
matlab-deployment
android-broadcast
xmltype
qtwebengine
cryptographichashfunction
grouping-sets
contrast
paw
ios8-extension
vsx
decoupling
ecslidingviewcontroller
mbunit
cffile
flex++
octal
diem
pagedown
onload-event
tree-balancing
cakephp-1.2
josql
jdb
predicatebuilder
apache-commons-dbutils
promotion-code
google-ajax-api
cuteeditor
socketserver
qglwidget
mathematical-notation
int64
xslcompiledtransform
movieplayer
netbeans6.1

Resources

Encrypt Message