I have a class called LabelButton with an onClickListener. In my Activity class, when the user clicks a button it creates a LabelButton and a String. Then adds the LabelButton to the Layout.
Inside the onClickListener attached to the LabelButton, it calls setVisibility(View.GONE), which takes care of the LabelButton, but I also need to delete that String that was also created earlier.
Is this possible?
Related
I have a database and adapter for the first activity showing the name and description. There is a button on each list item which takes you to the second activity displaying a unique image related to that item.
I have included an Intent from the first activity to the second activity.
So on the second activity I would like to add the image related to the item clicked.
Question:
(a)Do I include the image in the same database for the first activity or do I need a separate database and adapter for the second activity?
(b)Also do I need to create a separate intent for each item in the first activity as each item has a separate image that it will link to via the button which will be displayed on the second activity.
Your click listener will be one and generic as same lite item view is inflated for all items of adapter.
2.on click you need to pass the Uri string to second activity via intent and display the image Uri in second activity after receiving it from getIntent() in oncreateview() of second activity.
I would like to answer this in two parts.
Part 1: the database: You should use the same table for the image as well. You can always talk to the same database and all the tables from any activity, so no need to have a separate database. So the name, description and image in the same activity.
Part 2: The intent
If you are in a scenario where you have to add any action on click of an adapter item, always use a callback.
When you click on any item, this callback will tell you in the activity that which item in the adapter is clicked.
This medium blog is a very good example to demonstrate this.
In this blog's code, there is a block in adapter where you pass the values from the activity. It is the constructor.
RecyclerViewAdapter(RecyclerViewClickListener listener) {
mListener = listener;
}
If you had added some code, it would help, but I am sure you also have this constructor in your code, so add this listener along with the other data and see how it works out.
Thanks
I have an activity like this:
There are 2 buttons A and B on toolbar and a frame for fragment to take over. Say I have a string variable named button_type in fragment.
I want to have a system so that when I click button A in activity, the button_type in fragment sets to A and when I click button B in activity, the button_type in fragment sets to B.
How to do this?
Please note that I may click the buttons (A,B) when the fragment is already active (its not like after I click one button, the fragment comes.)
Thanks in advance.
Edit:
Currently I am trying this:
In MainActivity I get similar string button_type and set it as A or B according as button click and use the method:
public String getData(){return button_type;}
And in fragment I use: button_type= activity.getData(); in onViewCreated.
But it only seems to have the initial set value of A and B (which is A) and does not change when another button is clicked.
I think the best way if you use an interface for managing text in the fragment. the fragment will implement the interface. When the button click call the function in the fragment which is implemented by fragment.
Another way you can create an object of the fragment using findFragmentByTag() or findFragmentById() and then call the function in the fragment which handles the text.
Create an interface with methods onClickA(String buttonType) and onClickB(String buttonType).
Then create an object that implements this interface (or make fragment implement this interface by itself). I'll call this object listener.
call setButtonType(String buttonType) in listener methods implementation.
Then pass your listener to activity (you can get an instance of parent activity in fragment with getActivity() and cast it to your activity class) and in onClickListener of button A (in activity) call listener.onClickA(yourString) and do the same thing for button B.
I have an activity, this activity name is Game, it's xml file is composed by two views control, first of them it's a TextView called Texto and the second one is a SurfaceView created by me called Juego.
The Juego view has an onTouchListener event, and i want to send a text to the control called Texto everytime the user clicks on the control Juego.
I have all the "structure" created but i can't "communicate" from Juego to Texto, every thing i try i get an error.
Thx for help in advance,
Create a listener such as
onViewClick(String text). Create instance of this on your SurfaceView and in constructor get this listener as parameter. Now call this listener wherever you want to update the textView.
In you activity class, implement onViewClick listener and in method do:
public void onViewClick(String text){
TextTo.setText(text);
}
I have a listview which I am populating with a custom SimpleCursorAdapter, each row contains a button which, when clicked should open a new activity and pass the ID of the original data object so I can display the related image on screen.
I am having problems implementing the onclick event for the button and I understand you can only use startActivity() within an activity - is this correct? if so, is there a workaround as my cursoradapter code is in it's own class which extends SimpleCursorAdapter (ie. not in an activity!)
Just one more question if I may? - how can I pass the dataobject ID (ImageID) of the button clicked to the new activity?
set click listener over Button in getView() method in your adapter and use startActivity from there.
public class ActivityForShow extends Activity
{
//have a ListView layout
ListView.setAdapter(SingleRowAdapter);
}
public class SingleRowAdapter extends ArrayAdapter
{
//every row has a CheckBox ,a TextView and a Button
}
Now I want to handle CheckBox and Button events,do some background work and show result in a dialog.
But how to getRefences of Checkbox and Button in ActivityForShow?
findViewById() will cause Exception.
I don't think you want to do that in the ActivityForShow class. Instead, register all event handlers inside of your SingleRowAdapter#getItem(int) method (you will have have to override this). In that method you create the view for the given row, so you know what the row is (position) and you can register event handlers for the CheckBox, Button, etc
It sounds like you want to handle the onCheckedChanged event or onClick event in ActivityForShow.
If so, have ActivityForShow create a OnCheckedChangeListener instance and OnClickListener instance (e.g. as local anonymous inner classes) and have ActivityForShow give them to SingleRowAdapter (e.g. as constructor params) to attach to the checkbox and button widgets as their callbacks.
Alternatively, define your own custom handler interface(s) to be called when the button is clicked or the checkbox is checked. Have ActivityForShow to instantiate an instance of the custom interface(s) with code to process the event. Have ActivityForShow give them to SingleRowAdapter. Finally, have SingleRowAdapter create event handlers to attach to the checkboxes and buttons - these event handlers simply call your own custom handlers.
Hope the following link may help you, event on an item of ListView custom row layout.
It's obvious it gives error with finviewbyid method because it is used for when reference by xml ele. You can use this link for study:
http://developer.android.com/reference/android/widget/ArrayAdapter.html