I have created multiple set of checkbox(depending on server response ).Now i want to know which checkboxs are clicked,
What i have done is to create checkbox is
for( k = 0; k < stringList.size(); k++) {
cb[k] = new CheckBox(this);
cb[k].setText(stringList.get(k));
cb[k].setTag(feedbackdetails.get(i).getLabel());
cb[k].setId(k);
cb[k].setTextSize(ws.get_width());
ll.addView(cb[k]);
allcheckbox.add(cb[k]); }
i try to get the id on another button click
for(int i=0; i < allcheckbox.size(); i++){
stringcheckbox[i] = allcheckbox.get(i).getText().toString();
try {
if(cb[i].isChecked()){
checkid = cb[i].getId();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i am able to get id of last checkboxgroup.not getting any other check box id.
is the checkid, in checkid = cb[i].getId(); mean string???
if you declare the checkid is string, ofcourse you`ll only get the last id of checked checkbox. change the checkid to array string or array list. it can hold many value, so every checked checkbox id can be stored.
Related
Following code gives me numberformatexception. can you please tell me where to put exepcions or what to do about following lines? logcat tells that there is a problem on commented line
public void zaplatiti(){
EditText zbrojka = (EditText) findViewById(R.id.editText7);
Float[] strings = new Float[allTexts.size()];
Float zbroj=(float) 0;
for(int i=0; i < allTexts.size(); i++){
strings[i] = Float.valueOf(allTexts.get(i).getText().toString());////problem
}
for (int k=0;k<strings.length;k++){
zbroj =zbroj+ strings[k];}
}
zbrojka.setText(String.valueOf(zbroj)+"KN");
}
for(int i=0; i < allTexts.size(); i++){
try {
strings[i] = Float.valueOf(allTexts.get(i).getText().toString());////problem
}
catch (NumberFormatException e) {
// Code to execute if the entered value is not a number
}
}
This is also possible to avoid by only allowing your EditTexts to have numerical values. That will force the user to only enter a numerical value in the first place, then you don't have to catch the exception. But then you should check if the entered text is empty first.
EDIT:
For doing this you must make an if statement, but the correct comparison beetween String values is:
if ( mEditText.getText().toString().equals("String to compare with")){ ... }
the "numerical mode" of an editText could be set up in the XML file as:
android:inputType = "number"
Other valid numerical modes are numberSigned and numberDecimal.
So after changing the input type your code could look like:
for(int i=0; i < allTexts.size(); i++){
if (!allTexts.get(i).getText().toString().equals("")) {
strings[i] = Float.valueOf(allTexts.get(i).getText().toString());////problem
}
else {
// Code to execute if there is no entered value
}
}
Did you check the allTexts Array type?
My app has 4-5 EditTexts on each activity and I have 10 or so activities. I need to parse each of these to a double. So I figured I want to create a method in which I enter an EditText Array and it returns a double array with the parsed numbers.
One of the EditTexts will always be empty so I would need that specific position in the array of Doubles that are supposed to be returned to 0.
This is what I have been fiddling around with (without success so far).
public double[] parser(EditText[] editArray) {
EditText toBeParsed[] = null;
double parsed[] = null;
for (int i = 0; i < editArray.length; i++) {
try {
parsed[i] = Double.parseDouble(toBeParsed[i].getText().toString());
} catch (Exception e) {
parsed[i] = 0;}
}
Ho do i need to set this up?
Next in one of my activities I have this where I call the method (from a MiscMethods.java file)
EditText inputs[] = {i1,i2,i3,i4}
Double parsed[];
for (int i = 0; i < inputs.length; i++) {
parsed[i] = MiscMethods.parser(inputs);
}
But get a type mismatch... Why? the method returns an array of doubles and should put them into the double parsed[] array.?
It seems like you are using toBeParsed array in stead of your editArray
so
parsed[i] = Double.parseDouble(toBeParsed[i].getText().toString());
should be
parsed[i] = Double.parseDouble(editArray[i].getText().toString());
Hi im creating a list that has headers of dates and then content underneath. i have a JSON feed that contains fixtures inside each is an array containing the data for each fixture one string i need is matchdate to create my headers however if i was to just run through it it will create multiple instances of the same match day so id have 3 headers with the same date for example. how can i extract that information and the create another array that says if this date already exists go through the next one and so on. i know it's pretty specific question but if someone could at least point me in the right direction. thanks in advance.
heres my feed
fixturesArray = [{"awayteam":"Team 1","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:30:00","awayteam_id":"64930","matchdate":"2012-07-07","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 3","hometeam_id":"64932"},{"awayteam":"Team 2","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:00:00","awayteam_id":"64931","matchdate":"2012-07-07","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 4","hometeam_id":"64933"},{"awayteam":"Team 4","comp":"LGE","location":null,"attendance":null,"awayscore":null,"division":"Testing 1","homescore":null,"fixture_note":null,"kickoff":"15:00:00","awayteam_id":"64933","matchdate":"2012-07-14","awaypens":null,"homepens":null,"division_id":"5059","hometeam":"Team 1","hometeam_id":"64930"}]
heres what i have tried so far
Log.v("MyFix", "fixturesArray = " + fixturesArray);
if(fixturesArray.length() < 1){
TextView emptytext = (TextView) fixturesView.findViewById(R.id.TextView02);
emptytext.setText("No Upcoming Fixtures Available");
}else{
try{
JSONArray datesArray = null;
fixturesInfo = null;
String matchDateTemp = "";
for(int t = 0; t < fixturesArray.length(); t++){
JSONObject matchDateDict = fixturesArray.getJSONObject(t);
String matchDate = matchDateDict.getString("matchdate");
JSONArray matchdates = matchdates.put(matchDate);
Log.v("MyFix", "matchdate = " + matchDate);
tempArray.put(t, fixturesArray);
fixturesInfo.put(matchDate, tempArray);
}
Log.v("MyFix", "fixturesInfo = " + fixturesInfo);
Log.v("MyFix", "tempArray = " + tempArray);
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Just get the piece of data then iterate through the new arraylist checking for a duplicate before adding.
int matchCount=0; // set count of matches to 0
String matchDate = matchDateDict.getString("matchdate"); // get the data to compare
for (int i = 0; i < uniqueDateArrayList.size(); i++){ // set loop to iterate through unique date arraylist
if (matchDate.equals(uniqueDateArray[i])){ // compare the date to the date stored at position i
matchCount++; // if it matches increase the match count
}
}
if (matchCount == 0) {
uniqueDateArrayList.add(matchDate) // if there is no matching date, add it to the unique date arraylist
}
That should give you an array list containing all of the unique dates in your data.
I'm trying to cycle through a JSONObject to see how many passengers there are, and then I want to display them all nicely in a dialog window. It looks like there are a few ways to do it, but overall I'm just confused on how to go about it. This is the closest I've gotten, it works but as you can see I'm only adding one item. So there is only one item displaying in my alertdialog, and there should be a couple more. My alertdialog (called ticketbuilder) is created somewhere else, I'm just trying to add everything in this for loop. How do I add all of my passengers to the list to display? Thanks a ton in advance!
for (int i = 0; i < tickets.length(); i++) {
final int ticketCount = i;
JSONObject ticket;
try {
ticket = tickets.getJSONObject(ticketCount);
passengername = ticket.getString("passengername");
ticketnumber = ticket.getString("ticketnumber");
CharSequence[] array = {passengername + " \n" + ticketnumber};
ticketBuilder.setItems(array, null); //adding to my dialog
} catch (JSONException e) {
System.out.println(e);
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try putting the CharSequence outside the loop and init it like this:
CharSequence[] array = new CharSequence[tickets.length()];
Then in the loop add thing to the array:
array[i] = {passengername + " \n" + ticketnumber};
Move ticketBuilder.setItems so it is after, outside the loop.
I am trying to create dynamic buttons. When clicking a button it should go to the specified url assigned to the text of the button.
For testing, first I tried to get that ID, if it is equal it prints the value of i. But whenever I clicked any one button, instead of telling that particular i value, it enters into whole loop, and prints all the values of i starting from 1 to 19 (the number of buttons that are dynamically created)
And after printing all values from 1 to 19, the program is getting force closed saying Null pointer exception.
I even tried by placing the handler code outside onCreate(), but I'm still getting the same error.
for ( i = 0; i <itemList.getTitle().size()-1; i++) {
title[i] = new TextView(this);
title[i].setTextColor( -16711936 );
title[i].setTextSize(18);
title[i].setText("Title = "+itemList.getTitle().get(i));
description[i] = new TextView(this);
description[i].setTextColor(-16776961);
description[i].setText("Description = "+itemList.getDescription().get(i)+"......");
more[i]=new Button(this);
more[i].setText(itemList.getLink().get(i));
layout.addView(title[i]);
System.out.println("Title view is set");
layout.addView(description[i]);
//System.out.println("Description view is set");
layout.addView(more[i]);
more[i].setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener(){
public void onClick(View arg) {
int index = 0;
for (i = 0; i < more.length; i++)
{
if (more[i].getId() == arg.getId())
{
index = i;
System.out.println("Value of i onclick is"+i);
}
}
//System.out.println("Vlaue of I in onclick"+i);
//Uri uri=Uri.parse(itemList.getLink().get(i));
//startActivity(new Intent(Intent.ACTION_VIEW,uri));
//Toast.makeText(getApplicationContext(), "This button is clicked"+i+more[i].getText()+itemList.getLink().get(i),Toast.LENGTH_LONG).show();
}
}
You can use setTag() and getTag() method of View to identify different button.
for (i = 0; i < itemList.getTitle().size()-1; i++) {
...
more[i].setTag(i); // Use index of itemList as the tag
}
In onClick:
int index = (Integer)arg.getTag();
you can also set the id of button
more[i].setid(i);
int index = 0;
for (i = 0; i < more.length; i++)
{
if (more[i].getId() == arg.getId())
{
index = i;
System.out.println("Value of i onclick is"+i);
}
}
As you can see here, i is still in your for loop.
Put the System.out.println("Value of i onclick is"+i); outside of your for loop and it should work
PS: format your code, it's easier to read that way and you'll notice small mistakes like these more easily
I think this will help you..
set button tag also dynamic like
more[i].setId(i);
and also changed condition like
if (more[i].getId() == i) {
index = i;
}
hope this will help you...