Change Different Button Text from Recycler View Button Click - java

In my main activity page, i have a recycler view that has buttons for each item. As i click an item button, i want it to update the text on the Shopping cart button to show the count of items in the cart.The shopping cart button is outside of the recycler view.
I already setup a global variable that records the count of items in the cart, but i can't figure out how to display that count on the button. Mostly, i can't figure out how that button text can update when i click another item in the recycler view.
Thanks in advance!
In my Main Activity (where the recycler view is located and the button i want the text to change on is located), I've created a method init2.
public void init2() {
shopbutton = (Button) findViewById(R.id.button2);
String countshopS = "hello";
shopbutton.setText(countshopS);
}
then i try to call the method init2 from the Onclick Listener of the Adapter where the button rows are.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private MainActivity main = new MainActivity();
holder.priceset.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
main.init2();
I get the response "Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference" on the row where the Id of the button is identified in init2();

Use callback in your Adapter and increase for every click on row's button .
Then implement callback in your activity and do all things you need for example show finial price or count or...
Do not use statics and handle every rows count with data model (transiant maybe useful) and callbacks and interfaces.

Related

How can i change Recycler View ViewHolder editext when i select menu icon from toolbar ?

I am trying to develop a result system where for Absent student i have to select a string to define that student is absent.
Absent selection icon is at Toolbar.
And input text for marks of student is listed in recycler view and edit text is at recyclerview where i need to change the value of edittext to absent.
So can i change the value of edittext of recyclerview from toolbar menu item ?
Thank you in advance.
Create an interface to take your EditText out of the adapter as such;
public interface OnEditTextChangeNecessaryListener {
void onEditTextChangeNecessary(EditText editText);
}
After that just add your interface to the constructor and implment what you want to achieve as in
recyclerView.setadapter(new BlaBlaAdapter(context, etc, new OnEditTextChangeNecessaryListener(){
#Override
onEditTextChangeNecessary(EditText editText){
editText.setText("Something");
}
}));
Dont forget to set the listener in the adapter. As in;
holder.someButton.setOnClickListener(new OnClickListener(){
#Override
void onClick(){
necessaryListener.onEditTextChangeNecessary(holder.editText);
}
});

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.

Update ListView item on click/select

I'm new in android and I've got problem with ListView. My ListView loads data from REST service, then using this data ListView is filled. I want to add something lke OnItemClickListener/OnSelectedItemListener on each item and when user cliks on item want to add button. Button should disappear when user clicks on another item. Any ideas?
[EDIT]
What have I tried?
I've create LinearView which holds ListView and Button
ListView is filled in Activity using REST service data (TextView is used for view)
I think that my OnClickListener class may look like this but I don't know how I can add Button (the best will be create one Button object amd show it only on selected item - I think it will bebetter performance)
public class RoomClickListener implements OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Button b = new Button(arg0.getContext());
b.setText("Join");
}
}
Did you tried to play with the android:visibility of your Button?
Set it to visible/invisible can be a way to do this i guess.
Add button and call invalidate() on the view which changed.
This should work.

Listen to click on button in listview

I have a fragment with a listview in it.
The listview uses a custom adapter. Each item in the listview contains a button.
What I need is for the activity to know what button I press in the listview. I need this because the fragment needs too send a message to its activity depending on what button is pressed. So basically I want the OnClickListener to be in the fragment and not in the adapter.
I have tried to get the buttons from the list by itterating over all its elements and do a getChild(), but it does not react on click.
Button theButton = (Button)theListView.getChildAt(i).findViewById(R.id.button_id);
I have also tried with the getView on the adapter.
Button theButton = (Button) adapter.getView(i, null, null).findViewById(R.id.button_id);
Set the OnClickListener in the Adapter as usual but keep a reference to your fragment!
Let us say that your fragment class is MyFragment. Pass your fragment to the adapter; -i.e. in the constructor
private MyFragment theFragment;
public YourAdapter(MyFragment f) {
this.theFragment = f;
}
In this way you can use theFragment in every OnClickListener you set.

Categories