Why is startActivityForResult not showing up in recycler view adapter? - java

I am new to android programming. I am unable to find startActivityForResult(intent) within onBindViewHolder in the recycler adapter.
I have a recycler view adapter containing some items for MainActivity(1). This Activity 1 contains some elements which are hidden. If the user clicks on any item, he/she is taken to a new activity(2). Activity2 contains a button, which when clicked, changes the items in Activity 1 to be visible.
On doing some research, I found that I need to use startActivityForResult instead of startActivity if I am expecting results back from Activity2. But I am unable to find startActivityForResult(intent) within onBindViewHolder in the recycler adapter for Activity1.
Am I doing something wrong? Below is the recycler view code
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
holder.txtItem.setText(items.get(position).getItemName());
holder.txtShortDesc.setText(items.get(position).getItemShortDesc());
holder.txtShortDesc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, LongDesc.class);
intent.putExtra(ITEM_ID_KEY, items.get(position).getId());
startActivityForResult(intent);

Related

Cannot makeSceneTransitionAnimation in RecyclerView Adapter, Android

I'm trying to make a transition between my RecyclewView and a activity when onClick, but the Adapter doesn't let it. Any suggestion in how could I solve this problem?
My onBindViewHolder that contains the target onClick method which I want to implement the transition animation:
public void onBindViewHolder(#NonNull DataRVAdapter.ViewHolder holder, int position) {
// setting data to our views in Recycler view items.
DataModal modal = dataModalArrayList.get(position);
holder.bookName.setText(modal.getName());
// we are using Picasso to load images
// from URL inside our image view.
Picasso.get().load(modal.getCover()).placeholder(R.drawable.book_placeholder).into(holder.bookCover);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// setting on click listener
// for our items of recycler items.
//Toast.makeText(context, "Clicked item is " + modal.getName(), Toast.LENGTH_SHORT).show();
// open activity with our book information
Intent intent = new Intent(holder.itemView.getContext(), BookDetailsActivity.class);
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(holder.itemView.getContext(),holder.bookCover, ViewCompat.getTransitionName(holder.bookCover));
intent.putExtra("name", dataModalArrayList.get(position).getName());
intent.putExtra("cover", dataModalArrayList.get(position).getCover());
intent.putExtra("author", dataModalArrayList.get(position).getAuthor());
intent.putExtra("date", dataModalArrayList.get(position).getDate());
intent.putExtra("publisher", dataModalArrayList.get(position).getPublisher());
intent.putExtra("pages", dataModalArrayList.get(position).getPages());
intent.putExtra("genre", dataModalArrayList.get(position).getGenre());
intent.putExtra("isbn", dataModalArrayList.get(position).getIsbn());
intent.putExtra("synopsis", dataModalArrayList.get(position).getSynopsis());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
holder.itemView.getContext().startActivity(intent, optionsCompat.toBundle());
}
});
}
And here's my ViewHolder:
// creating variables for our
// views of recycler items.
private TextView bookName;
private ImageView bookCover;
public ViewHolder(#NonNull View itemView) {
super(itemView);
// initializing the views of recycler views.
bookName = itemView.findViewById(R.id.idTVtext);
bookCover = itemView.findViewById(R.id.idIVimage);
}
}
These codes are all inside my RecyclewView Adapter
So thank you in advance!

How can I pass the entire ListView item from one Activity to another in android

I want to pass the entire ListView item from one Activity to another.
My code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
Well you should get the list (ArrayList not the ListView) from the adapter and give it to the current activity. From there you should put it in the Intent you are using to start the second activity. Then get it from the second activity's intent and pass it to the adapter of the second activty.
Simply, just send the list from firstActivity to secondActivity.
FirstActivity
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
In secondActivity recieve the list and set it to listView or send it to your customAdatper class If you have made CustomListView. Make sure you have also declear ListView in your secondActivity.
SecondActivity
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");

Get click event from gridview's child item in main activity from adapter

I have a GridView in my activity. I am having 2 elements in the GridView. One is an ImageView and the other is a TextView.
I want to perform an action when clicking the ImageView only, but I want this to happen in the activity and not in the GridView adapter.
I handle the ImageView click in the getView() of my adapter, but I do not want it that way. I want to handle it in the activity when calling:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, items));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//THIS WORKS FINE
String string = ((TextView) v.findViewById(R.id.text)).getText().toString();
Log.d("string",string);
//THE PROBLEM OCCURS HERE
ImageView capture = (ImageView) v.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
});
//THE PROBLEM OCCURS HERE
The action is supposed to happen the first time I click the ImageView, but it only happens on the second click and further.
This is where I am stuck with the issue. I want this action to occur on the first click and not on the second click.
You can see the code block-
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String string = ((TextView) v.findViewById(R.id.text)).getText().toString();
Log.d("string",string);
//THE PROBLEM OCCURS HERE
ImageView capture = (ImageView) v.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
Your imageview capture is adding any action after gridview is being clicked. So, after clicking first time it is executing and then setting click listener to imageview and then next click its executing its onClick block. So better, handle imageview click event inside adapter.
You can call method inside your activity from adapter class but you should implement setOnClickListener inside your adapter class.
with the help of interface, you can do that do one thing create an interface and implement it on your activity and get imageview click on the adapter and here initiate the interface and you will get the click inside the activity
I think the problem is that on the first click you just set OnClickListener on your imageView, and so its onClick() method is not getting called. On the second click though, as the listener is already set, onClick() is invoked.
Instead of setting new Listener to your imageView each time an item in the grid clicked (which does not make any sense by the way), you should do either of these:
If imageView in each item has to be handled the same way, set OnClickListener to the imageView in the adapter, when creating a view.
If not, pass the interface for handling imageView clicks to the constructor of the adapter, and then, in the activity, implement this interface when creating an adapter.
Create a method in you activity.
Now, in adapter, onClick call that method using
((Activityname)context).methodname(parameters);

How to get information of an Item in RecyclerView CardView Android

I have a RecyclerView which is constructed by inflating CardView Layout. This is acheived by onCreateViewHolder method. I am passing a ArrayList of data to the RecyclerView.
My requirement is, on clicking the list item, I have to load a new page which shows the details of the item clicked. For this, I need to get the id of the item clicked which is not showed in the view. In the view I have only Name and email of the item.
Let's say, the ArrayList I am passing is of type Person and Person has member variables id,name,email,education,location.
In the RecyclerView onBindViewHolderfunction, I am setting only name and email.
I need to get the id of the item, then only I can do a db command and get details of the particular person.
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerList);
rv.setAdapter(new NGOViewHolder(ArrayList()));
rv.addOnItemTouchListener( // and the click is handled
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getActivity(),NGODetails.class);
intent.putExtra("ID", (IDOfTheItemClicked));
}));
I requirement is IDOfTheItemClicked, that is passing in the above
Intent(getActivity(),NGODetails.class);
intent.putExtra("ID", (IDOfTheItemClicked));
should be the id of the item that is in the ArrayList that I have passed initially.
How can I get to send the id of the Person object which is currently showing in the view, to the next screen, on clicking the list item.
set position for each item using setTag inside onCreateViewHolder method after that when you'll click then inside onclick you get a view reference from there, get that tag and convert to int( this will give you position of view)
onCreateViewHolder(............){
View view;
view.setTag(id, position);
view.setOnTouch or Onclick(View v){
String position = v.getTag(),toString();
// get your id using this position
}
}
If you don't change the order of the items in your adapter, simply call yourArrayList.get(position).yourgetIDOfTheItemClickedMethod in the onClick.

RecyclerView Onclick (call non-static method)

I've got stuck with an issue about setting an OnItemClickListener to my RecyclerView items. I tried to set a listener the way described in the RecyclerView sample of Android Studio. So a listener is set in the ViewHolder class for my RecyclerView.
public class ProgramViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected ImageView vProgramImage;
public ProgramViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.programName);
vProgramImage = (ImageView) v.findViewById(R.id.programImage);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE PROBLEM !!
MainActivity.openSettings(1);
}
});
}
}
Now I want to call a method of my MainActivity openSettings(int ) to load a fragment:
public void openSettings(int layoutId) {
settingsFragment setFrag = new settingsFragment();
Bundle information = new Bundle();
information.putInt("layoutId", layoutId);
setFrag.setArguments(information);
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, setFrag)
.commit();
}
But now the problem. When I try to compile, it says "Non-static method 'openSettings(int )' cannot be referenced from a static context."
I quite not understand this error. Why is it a static context? The class ProgramViewHolder ist not declared static.
And the most important part: How can I fix it? I want to set a OnClickListener to every item of RecyclerView and call a public method of MainActivity.
Thanks a lot to you, for your time spending to help me.
It's not that ProgramViewHolder is static, it's because attempting to call your activity from a static context (you aren't calling a specific instance of the activity).
What you should do is pass the activity into your recyclerViewAdapter so that you have access to it.
For example
MainActivity mainActivity;
public CustomRecyclerViewAdapter(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
And to create the recyclerViewAdapter from MainActivity
CustomRecyclerView recyclerViewAdapter = new CustomRecyclerViewAdapter(this);
recyclerViewAdapter.setAdapter(recyclerViewAdapter);
You should then be able to access your method like this
mainActivity.openSettings(1);
Let me know if you have any trouble
//Edit
Here's how you would set onClick from bindViewHolder. You want to set up any onClickListeners here due to the way RecyclerView "recycles" data. For example, if each row should perform a different action on click, you need to make sure the click listener is tied to the specific row. Creating this in onBindViewHolder ensures this. If you want an entire row to be clickable, rather than elements inside, just create an outer view that fills the entire row. Then tie the onClickListener to that.
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
ProgramViewHolder programViewHolder = (ProgramViewHolder) holder;
programViewHolder.vName.setOnClicklistener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainActivity.openSettings(1);
}
});
}
if you have context of the activity containing recyclerView, then you can simply do this:
your_view_holder.v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE SOLUTION!!
((MainActivity)context).openSettings(1);
}
});
You can place this in onBindViewHolder(...)
How to get context:
Create another parameter of context in your Adapter's constructor , and pass the context from your activity once instantiating Adapter .
why pass the context:
i would recommend you to always pass context and assign it to any adapter's variable because this is something you would require every now and then while working with your adapter, so instead of using a workaround every time for context, just save it once .

Categories