This is a duplicate of: Android dynamically generated radio buttons not unchecking once setChecked programmatically
However in that question the answers didn't solve the question because all the code I added to try to solve the problem muddied the issue. In addition I have also applied several potential solutions below since then.
The problem is that if I check a RadioButton programmatically, it stays checked when I select another RadioButton in the same RadioGroup.
I have learned several potential solutions from Googling that I have applied but have not worked, such as:
Using RadioGroup.Check instead of RadioButton.setCheck
specifying each RadioButton's Id explicitly
Adding the RadioButton to the RadioGroup before checking it
Adding all the RadioButtons to the RadioGroup before checking one of them
despite all this, the RadioButton still stays checked when you click on another.
private void PopulateAccessPoints(List<clsAccessPoint> accessPoints){
try{
rg = new RadioGroup(this);
rlaAccessPoints.addView(rg);
int i = 0;
for (clsAccessPoint acp : accessPoints) {
i++;
RadioButton rd = new RadioButton(this);
rg.addView(rd);
rd.setId(i);
rd.setText(acp.ID + ": " + acp.Name);
}
rg.check(rg.getChildAt(1).getId());
} catch (Exception ex) {
ex.printStackTrace();
}
Note: The RadioButton that should be checked will be determined dynamically, but I have hardcoded it as the above exhibits the same symptoms and is simpler.
I've spent several hours on this now so will appreciate any help for this apparently very simple problem!
RadioButton rd = new RadioButton(this);
rg.addView(rd);
rd.setId(i);
rd.setText(acp.ID + ": " + acp.Name);
try moving this code around to be like this:
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(acp.ID + ": " + acp.Name);
rg.addView(rd);
setting the ids before you add it to the group?
Related
I want to create the following view:
For above picture, I declared a vertical radio group in xml, and rows were added at run time. In a single radio button , i want to add two other buttons,as shown in picture,
This code is to add address (radio buttons) to radiogroup.
private void add_to_address_list() {
for (int i = 0; i < addresses_list.size(); i++) {
RadioButton rdbtn = new RadioButton(this);
Log.d("mytag", "id : " + i + "name :" + i);
addresses_list.get(i).getUser_line2());
String line1 = addresses_list.get(i).getUser_line1();
String line2 = addresses_list.get(i).getUser_line2();
String line3 = addresses_list.get(i).getGoogle_line1();
rdbtn.setId(i);
rdbtn.setText(Html.fromHtml("<font color=black size=3em>" +line1+"<br>"+line2+"<br></font>" +"<font color=grey size=3em>"+ line3+"</font><br>"));
((ViewGroup) findViewById(R.id.address_radio_group)).addView(rdbtn);
}
}
How can i add two other buttons, one is for delete the address and another is for edit address, is it possible ? or can i go with simple list view?
if i use listview to display it, is it possible to select one only. i think it may possible with check boxes. suggest any correct method. pleas help me.Thanks.
I am trying to take a string (which is formatted HTML) and split it at every double enter. I know that before and after a double enter will be a p block tag, and I'm using a for loop to split it in this way. Here is my code
for(String s : rawHTML.split("\\n\\n")){
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
comm.setText(Html.fromHtml(s) );
Expected behavior from this is every "Currently is" in the log should be blank. If there are two or more p blocks, though, when I setText, it overwrites the previous text.
For example, I have a string
<p>Test</p>\n\n<p>Hello</p>
I should be seeing two TextViews like so
[Test]
[Hello]
Instead, I see this
[Hello]
[]
I am really stumped and can't figure out this strange issue.
Thank you!
The following code snippet shoud work for you.
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
for(String s : rawHTML.split("\\n\\n")){
comm.append(Html.fromHtml(s) );
}
I've a ListView allowing multiple selection configured with an ArrayAdapter, everything works fine but when I try to set to false some selected items the getCheckedItemCount value doesn't change
Just to replicate the behavior I unselect all items using the code shown below and the printed value is the same before and after the getCheckedItemCount call
Why?
for (MyObject post : list) {
int position = photoAdapter.getPosition(post);
System.out.println("count before " + photoListView.getCheckedItemCount());
photoListView.setItemChecked(position, false);
System.out.println("count after " + photoListView.getCheckedItemCount());
}
Obviously to unselect all items we must call clearChoices() but the code above is used only to demonstrate the problem
#dafi i am not sure this is working in your case but below solution give me right count. The easiest solution is to clear the checked count manually.You just try it.
this.markersList.clearChoices();
for(int i = 0; i < this.markersList.getCount(); i++)
{
this.markersList.setItemChecked(i, true);
}
Try out this solution.
I'm new, so sorry if this turns out so simple that I should have solved myself. I've spent a couple days thinking about it. I've researched a ton and searched many other posts here, but no success.
I have a dialog with DatePicker, Buttons, EditText fields, and Spinners. I can populate everything i need from the stored items in my DB to the dialog. But when i try to get the numbers entered into some of the EditText fields, it throws NumberFormatException. I can hard code values into variables to store, and hide my attempt to get the value in a try and it will run fine. The values get stored and when I open the dialog again they populate into the right areas.
Here's partial code-
EditText et = new EditText(getContext());
for(int i=0; i<=etcount; i++){
placed = -1; //reset for next iteration
//try to get number from edittext
et.findViewById(i);
placed = Integer.parseInt(et.getText().toString());
awake++;
/*
try {
//et.findViewById(i);
placed = Integer.parseInt(findViewById(i).toString());
//placed = Integer.parseInt(et.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}*/
I commented out the try block cause I wanted to troubleshoot quickly.
etcount variable is initialized at onCreate with -1 value, before getting values from DB. If there are values stored in the DB it gets incremented by 1, then code is called to dynamically add an EditText and Spinner to the layout. Also the EditText id is set to the value of etcount. Here is that code-
//this will be ran when +placement button pressed, or if there are items in db stored
//adds 2 rows to dialog, one for textview to label items, one row for edittext with number
//and spinner with what item it is
private void createTableRow(int numPlaced, int itmPlaced){
etcount++; //used to count how many edittext fields there are so that they can be saved later
spincount++; //to count how many spinner there are
tl = (TableLayout)findViewById(R.id.tableLayoutVisit); //the tablelayout name
//need to create row for textview, then another row for edittext and spinner
tr1 = new TableRow(this.getContext()); //table row 1, for textview
tr1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this.getContext());
tv.setText("Amount Placed");
tv.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr1.addView(tv); //add textview to tablerow1
tl.addView(tr1, new TableLayout.LayoutParams( //add row to tablelayout
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr2 = new TableRow(this.getContext()); //tablerow2: edittext and spinner
tr2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
EditText et = new EditText(this.getContext());
et.setId(etcount);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
if(numPlaced!=0){ //if being populated from previous visit
et.setText(Integer.toString(numPlaced));
//et.setText("" +numPlaced);
}
//need to have listener to read data
et.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Spinner spin = new Spinner(this.getContext());
spin.setId(spincount);
spinArray = getContext().getResources().getStringArray(R.array.itemplaced);
ArrayAdapter adapter = new ArrayAdapter(getContext(),
android.R.layout.simple_spinner_item, spinArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
if(itmPlaced!=-1){
spin.setSelection(itmPlaced); //assign correct value
}
spin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr2.addView(et);
tr2.addView(spin);
tl.addView(tr2, new TableLayout.LayoutParams( //add row to tablelayout
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
I pass it (0,-1) if it is a new item and not currently stored in DB. Also you probably noticed I forced it to accept TYPE_CLASS_NUMBER. But I don't think the issue is there. Again, if I hard code values it will save into the DB and the next time I open it it dynamically creates EditText/Spinner row in layout and populates the EditText with the value in the DB.
So... something is wrong with the first section I think, the et.findViewById(i); or placed = Integer.parseInt(et.getText().toString());. The value of "etcount" should be -1 if nothing was populated from DB, and 0 etc(etcount++) each time something is populated from DB.
Sorry if this is longwinded, wanted to be specific. Hope this is enough info! Any help would be great!!! Thanks in advance.
Try this:
EditText et = new EditText(this);
for (int i = 0; i <= etcount; i++) {
placed = -1;
et = (EditText) this.findViewById(i);
placed = Integer.parseInt(et.getText().toString());
//...
}
It seems to me that I've found an error.
The main problem in your code that you do not assign unique values in setId methods. In your case some editTexts and spins can have the same IDs. This is wrong. Every element must have a unique identifier. This is the first what you should correct in your code.
After that you can use my approach. But it seems to me that something wrong in this approach. It is not beautiful ) Try to find best practices in this field.
Few suggestions from my side:
1) try to check whether getText method not giving you empty string"" or null.
2) place your EditText et; to class variable not a local instance.
example public class ExampleActivity extends Activity
{
EditText et;//place it here instead in oncreate methods.
}// or make it final if defining in oncreate.
according to your code, issues seems in your for loop
for(int i=0; i<=etcount; i++){
placed = -1; //reset for next iteration
//try to get number from edittext // which edit text you are trying to access. am sure it is returning null to you.
et.findViewById(i);
placed = Integer.parseInt(et.getText().toString());
Here's my issue:
I have a database and it is full of episodes of a tv show. One column denotes the episode number. I want to display the episodes in a list like this:
Episode 1
Episode 2
Episode 3
etc.
I'm using my own adapter class that extends SimpleCursorAdapter to do this...
Since I had formatting errors I am using Android.R.layout.simple_list_item_1 and Android.R.id.text1
Basically the only reason I have a custom adapter is so I can do something like this:
textView.setText("Episode " + cursor.getString("column_for_episode_number");
The problem is, I get a list that looks like this:
Episode
1
Episode
2
Episode
3
When I try something like this(which worked in a different portion of my code):
String text = "Episode " + cursor.getString("blah");
text = text.replaceAll("\\n","");
I get the exact same list output :(
Why don't I use create a custom view with two textboxes next to each other? It is hard for me to get that to look pretty :/
text.replaceAll(System.getProperty("line.separator"), "");
There is a mistake in your code. Use "\n" instead of "\\n"
String myString = "a string\n with new line"
myString = myString.replaceAll("\n","");
Log.d("myString",myString);
Check if there is new line at the beginning before you replace and do the same test again:
for(int i=0; cursor.getString("blah").length()-1; i++)
{
if(cursor.getString("blah").charAt(i)=='\\n') <-- use the constant for the line separator
{
Log.i("NEW LINE?", "YES, WE HAVE");
}
}
Or use the .contains("\n"); method:
Check the xml for the width of the textview as well.
Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.
This could help you:
response = response.replaceAll("\\s+","");
It sounds like you are hitting wrapping issues rather than newline issues. Change this:
String text = "Episode " + cursor.getString("blah");
To this:
String text = "Episode" + cursor.getString("blah");
And see if that changes the output. Post your layout xml please?
this worked for my (on android 4.4):
(where body is a string with a newline entered from an EditText view on handset)
for (int i=0; i<body.length(); i++) {
if (body.charAt(i) == '\n' || body.charAt(i) == '\t') {
body = body.substring(0, i) + " " + body.substring(i+1, body.length());
}
}
have you tried
cursor.getString("blah").trim()