I have added checkboxes dynamically in my onCreate() method. When one or more of them are checked I want two buttons to be displayed. I am new to working with Android, but from what I can tell it won't work to do this check (see if checkboxes are checked) in the same method as the checkboxes are added, i.e. onCreate(), because they can't be found, i.e. haven't been created yet? If this is the case, in what method should I do the check? I want to add the checkboxes at startup of the activity and to check if the boxes are checked all the time.
The checkboxes are added in displayEntries(db).
Here is my onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DatabaseHandler db = new DatabaseHandler(this);
displayEntries(db);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, AddActivity.class); //FromActivity.this
startActivity(i);
}
});
checkChecked();
}
checkChecked():
public void checkChecked() {
//Check whether items have been checked
TableLayout tl = (TableLayout) findViewById(R.id.entry_table);
for (int i = 0; i < tl.getChildCount(); i++) {
View child = tl.getChildAt(i);
if (child instanceof CheckBox) {
CheckBox checkBoxChild = (CheckBox) child;
Log.d("Checkbox", "Checkbox is found");
if (checkBoxChild.isChecked()) {
Log.d("Checkbox", "working!");
Button button1 = (Button) findViewById(R.id.button_edit);
button1.setVisibility(View.INVISIBLE);
Button button2 = (Button) findViewById(R.id.button_delete);
button2.setVisibility(View.INVISIBLE);
}
}
}
}
(At the moment I am trying to make the buttons invisible as you could see above, since I didn't want to "hardcode" in the layout file that they should be invisible to start with - thought that might make it impossible to change? But that's a later question...)
EDIT: I realised my mistake was thinking that the checkboxes where children of my TableLayout. They were in fact children of the rows in the TableLayout. Now that I managed to locate the actual checkbox, I can make the buttons disappear by setting them as invisible when creating them and visible onClick. The remaining problem is that the buttons of course stay visible even if all buttons are unchecked again. Any ideas on how to toggle the buttons on and off depending on if any of the checkboxes are checked or not?
Did not understand your question at first. Please ignore my first answer.
If you want to listen on a check box you might want to try
myCheckBox.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
//do something
}
});
Anything you do in onCreate() will be re-created with each activity cycle.
Therefore you might want to store the state of the check box, and change that state when someone clicks on it.
EDIT after #Ingrid comment:
Looks like there are two problems that need to be solved. One is saving the state and restoring it in on activity/fragment cycle, the other is setting the buttons visible based on condition of N checkboxes.
Assuming you are working with a database object you have an option to use dynamically created check boxes to manipulate the data in your Database Object. The checkboxes represent Boolean values in your DB object or list of objects. Since the checkboxes are dynamic and represent things in your DB, it does not make much sense to store their state in two places. If you do that, things will get out of sync and you will have a nasty bug. Rather rely on the DB object instead. Thus, update your DB object when you click on a checkbox. In onResume() retrieve that DB object (from bundle or DB depending on your architecture) and set your checkboxes / button state based on the state of the DB object.
You can use onPause() to persist your db object. or persist it on click of the check box so your don't have to wary about losing the state in-between. use onResume to setup your UI component base on the current state of the db object. Last but not least, when you update your db object check if you need to show/hide the buttons. (That is if there are on fixed number of buttons but N number of check boxes.
Hope this helps.
//Use shared preference to save.
SharedPreferences preferedName=getSharedPreferences("SavedName", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferedName.edit();
editor.putboolean("SavedName",value);
editor.apply();
editor.commit();
//and to retrive.
SharedPreferences preferedName=getSharedPreferences("SavedName", Activity.MODE_PRIVATE);
boolean chkNull =preferedName.getString("SavedName",false);
and use the boolean as the value of the checkbox.
you can see check CompoundButton because CheckBox inherit from CompoundButton
https://developer.android.com/reference/android/widget/CompoundButton.html#setOnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener)
checkBoxChild.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
Related
This might sound a bit convoluted.
I have roughly 15 Spinners in one activity and made a distinct method for each of these spinners. I then initiate the methods in the onCreate method.
Method example:
//Relative Position Spinner
public void relativePositionSpinner() {
Spinner relativePositionSpinner = (Spinner) findViewById(R.id.spinner_relativePosition);
ArrayAdapter relativePositionAdapter = ArrayAdapter.createFromResource(this, R.array.relativePosition, R.layout.spinner_item);
relativePositionSpinner.setAdapter(relativePositionAdapter);
//what happens when selected
relativePositionSpinner.setOnItemSelectedListener(this);
}
OnCreate Method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_hand);
//initiate all Spinners
relativePositionSpinner();
absolutePositionSpinner();
etc.
Now what I want is to send the data of each Spinner to another Activity with the click of a Button. I know that I can do this with an intent and using putExtra in the Button method like this:
public void openHandSummary() {
//Find the Button that gives option to enter new hand
Button handInputButton = (Button) findViewById(R.id.hand_input_button);
//set a click listener on Hand Analyzer Button
handInputButton.setOnClickListener(new View.OnClickListener() {
//below code will be executed when the new Hand Button is clicked
#Override
public void onClick(View view) {
Intent handSummaryIntent = new Intent(NewHandActivity.this, HandSummaryActivity.class);
handSummaryIntent.putExtra("RelPosString", WHATTOENTERHERE??)
startActivity(handSummaryIntent);
}
});
}
However I do not know how to retrieve the value/variable out of my Spinners to put them into the Button/intent method? Because if I make a String in the Spinner method, then I can't access this in the Button method.
So I feel like I have too many methods? So is there a way to pass data from one method to another method, or do I have to cancel some methods? What would be the easiest way to set this up?
I also made an onItemSelected to make some toasts, which worked. Can I use OnItemSelected somehow to create variables or initiate a data transfer to another Activity?
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
switch (parent.getId()) {
case R.id.spinner_relativePosition:
makeText(NewHandActivity.this, "Relative Position is " + myText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner_absolutePosition:
makeText(NewHandActivity.this, "Absolute Position is " + myText.getText(), Toast.LENGTH_SHORT).show();
break;
I'm very new to coding, and I just can't figure out the logic how I get the Spinner methods, Button/iniate method and OnItemSelected method to work together and exchange variables. Would appreciate if someone can point me in the right direction. Have already browsed the internet a day or so to find an answer, with no success.
I like your idea of separating out the code to create each spinner into its own method. However, if you are copying and pasting the whole method and making a few changes, you should step back and think about how you can do it even more easily. Often the changes you make after copy and paste give a hint that you should add some parameters to the method. If you do it correctly, you can write just a single method and then copy and paste the method call instead of the entire method and then make appropriate changes to the arguments.
As for your actual question, you are making this much more complicated than necessary. Specifically, you do not need to setOnItemSelectedListener() on any of the Spinners. Instead, the OnClickListener for the button should just get the selected item from each spinner and send it to the new activity in the Intent.
Hello I'm having an issue getting all of the items from my adapter in my fragment. To be more specific I am using a ScaleInAnimatorAdapter along with my Customer Adapter and when I attempt to get my checkbox items from the below posted code, within my Fragment, I only seem to get the visible items on screen.
private View.OnClickListener onAllClick = new View.OnClickListener() {
#Override public void onClick(View v) {
int count = listAdapter.getItemCount();
for (int i = 0; i < count; i++) {
View mChild = listTopics.getChildAt(i);
if( mChild != null ) {
Log.d(TAG,"getItemCount(): " + i );
CheckBox cBox = (CheckBox)mChild.findViewById(R.id.topic_chk);
cBox.setChecked(((CheckBox) v).isChecked());
Log.d(TAG,"isChecked" + cBox.getTag());
cBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox) v).isChecked()) {
checkboxAll.setChecked(false);
}
}
});
}
}
}
};
Essentially I am attempting to create a check all feature from the fragment, so that when this is clicked all of the checkbox items from the adapter are checked. I got that to work with the code I presented however the main issue is that I only get the items visible on the screen, so when I scroll to my other items they are not checked. Thus I am wondering if there is a better way or another way for me to get all of the items
Adapters are meant to bind underlying data stores to views; they generally shouldn't be used to store data themselves (except for having a copy of the data for view binding purposes) nor should they perform actions on data.
Instead, you should be modifying the underlying data, then updating the adapter through whatever mechanism you are already using. (Loaders, custom setters with notifyDataItemChanged, etc.).
Basically you can't. It'll be reusing the UI views.
You need to set a flag in all your data list objects, call notifyDataSetChanged() and onBindViewHolder check that flag and use it to check uncheck
if(listAdapter.getItem(position).getIsChecked())
viewHolder.checkBox.setChecked(true);
else
viewHolder.checkBox.setChecked(false);
Ok. Thank you all for those who have replied you helped me grasp something really basic and important that had alluded me.
I got it to work, so for anyone who might read this post, here is my solution:
Create necessary methods and field to update your data in a model (ie, private is_checked, set_checked(), is_checked(), etc)
In your adapter onBindViewHolder, you will set your holder checkbox to be associated to the data so something like:
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
obj.setSelected(buttonView.isChecked());
}
});
holder.checkBox.setChecked(obj.isSelected());
Then in your fragment or activity I just have a click listener on my main checkbox, and then when checked I loop through my data list, and using the model method I update my data and just simply use listAdapter.notifyDataSetChanged()
You are doing it wrongly.
You should update the adapter instead of updating the check box view.
What you can do , you can create a field in adapter data holder .
And whenever you change the selection,just update the field or multiple field and finally refresh the view.
I have looked other questions but none of them fulfill what I need.
I have a listview showing a list of objects (alarms) that have some textviews and a checkbox. As you can imagine, this checkbox is to activate and deactivate that alarm.
When I arrive to this screen (alarms list), it checks the active state in the database, so some of the checkboxes are checked and some other not.
What I need is to click in the checkbox and change the state in the database for that alarm at the moment. Not checking the state of all checkboxes before leaving the screen!!! I have read solutions using selectedItems or Holder class, but are not suitable for me.
I think I need to define the onClick event in the checkbox (in the AlarmAdapter), but the list of alarms is in the activity (ListAlarms), so I don't know how to "tell" the list of alarms "this is the checkbox (alarm) that has been clicked, then update its active state".
Any idea? Maybe controlling the action in the row (with OnItemClickListener) instead of clicking the checkbox?
Thanks a lot!!!
I think if you read the response here you will have your answer:
How to get selected list items from a Listview with checkBox and Custom Adapter?
Finally, I have done a non-clean solution:
This code in the adapter:
CheckBox active = (CheckBox) view.findViewById(R.id.cb_active);
active.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
a.toggle();
boolean exists = false;
for(Alarm aux : modifiedAlarms){
if(aux.getId() == a.getId()){
exists = true;
}
}
if(!exists){
modifiedAlarms.add(a);
}
else{
//this is to guarantee that the alarm has the same state than the added one.
a.toggle();
modifiedAlarms.remove(a);
a.toggle();
}
}
});
active.setTag(a);
And this code in the activity, when leaving the screen:
ImageView back = (ImageView)findViewById(R.id.iv_back);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<Alarm> modified = adapter.getModifiedAlarms();
for(Alarm a : modified){
dataalarms.activeAlarm(a.getId(), a.getActive());
}
finish();
}
});
If anybody knows how to get the onClick event in the activity, it would be nice to answer here.
Thanks! :)
This is where the custom object comes in. You create an array of these objects, and then you set a flag in the onClick event of your checkbox in the adapter. Then you can access this object and check the flag. Sorry for creating new answers, but my rep isn't high enough to comment.
I have a set of View States that each contain a CheckBox view and an EditText View. When the user checks the CheckBox, I'd like to set the visibility of the EditText.
To get a handle of the EditText from the CheckBox, I just put the EditText as a tag on the CheckBox in OnCreate():
// In OnCreate()
v = f1.findViewById(R.id.imgNotes);
v.setTag(f1.findViewById(R.id.noteText));
v.setOnClickListener(this);
Later, in onClick() I retrieve the EditText from the tag:
public void onClick(View v)
{
super.onClick(v);
switch (v.getId())
{
case R.id.imgNotes:
View noteText = (View) v.getTag();
if (null != noteText)
{
if (noteText.getVisibility() == View.VISIBLE)
noteText.setVisibility(View.GONE);
else
noteText.setVisibility(View.VISIBLE);
}
break;
…
Everything is working fine. I was just wondering if there is a better way. Or is this okay? Or is this the ideal way?
Thanks!
This would work, no issues, but as a practice, avoid tagging views to other views. Few other ways you can try.
Field variable
Declare two private members, for EditText and CheckBox, and directly access them, whenever a checkbox is clicked. It works fine if lets say you have one pair of CheckBox and EditText, but the code gets messy if too many check_box and edit_text ids are wired around.
Custom view
Define a custom view class which holds such View state pairs, that way you can manage multiple such pairs, with each custom view holding business logic inside that view, thereby providing flexibility to add more behaviours in future, just edit this class.
Hope that helps. :)
In onCreate save EditText to a global variable likes this:
mEditText = (EditText) f1.findViewById(R.id.noteText)
Then in your onClick just use mEditText instead of noteText
I want to show an empty list view, which is then populated by user input. I have the UI flow working, and I populate a list of my custom objects after the user enters some information via a view which is invoked through setContentView (i.e. no a new Activity).
I take the input and add it to a list, which I want to be summarised on the ListView. However, whenever I add to the list and/or the ArrayAdapter and call adapter.notifyDataSetChanged() it does not do what I want. The ListView is still empty. Argh! It's driving me insane!
#Override
public void onCreate(Bundle blah) {
ListView listView = (ListView) findViewById(R.id.results_list);
listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnu_add:
final Activity act = this;
setContentView(R.layout.record_details);// the sub-view that takes the user input
// the button on the form to 'add' details:-
((Button) findViewById(R.id.recored_details_add_btn))
.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// get input from widgets
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);
}
}
);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
break;
}
return true;
}
Please, save me from my misery and inform me of my stupidty?
Thanks in advance.
public void onClick(View v) {
// get input from widgets
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);
Is it possible that this setContentView in the onClick handler is creating a new instance of the list view widget (with no adapter) or reinitializing the list view (clearing the adapter)?
Try putting something in the list initially in onCreate and then see if it disappears when you hit the button.
I haven't seen any code (although I'm a relative newbie) that switches views within the activity's lifetime to bring up essentially bring up different pages - most use a separate activity.
Edit:
OP asks:
Thanks...So how can I get what I want? The list I'm backing the adapter with is static; should I just use activities instead and rely on onCreate loading from the static field?
Some options:
Use separate activities
Re-associate the adapter (call setAdapter again) - probably a bad idea
Declare both layouts in the same file. You'll hide one and unhide the other to switch between views rather can calling setContentView. This is similar to how ListView layout works (one for when the list is empty and one for when it is not). I think I've seen an example of this somewhere on the net, but I don't have a reference right now.
You could relaunch the same activity by using Intent.FLAG_ACTIVITY_SINGLE_TOP flag while creating the intent and override the onNewIntent() method.
Inside the onNewIntent() you create the adapter with updated data and call setAdapter.
I think this will give you the intended behaviour.