I got an ArrayList:
ArrayList<CheckBox> swmsInfo = new ArrayList<CheckBox>();
and some checkboxes:
checkBoxCompany = createCheckBox(R.id.checkBoxCompany);
checkBoxName = createCheckBox(R.id.checkBoxName);
checkBoxPhone = createCheckBox(R.id.checkBoxPhone);
checkBoxAdress = createCheckBox(R.id.checkBoxAdress);
I want to get their values 0 or 1 if some of the checkboxes are checked or not , and when i got their values i want to put them into a array. My checkboxes are inside a array
ArrayList<CheckBox> boxes = new ArrayList<CheckBox>();
You can loop through the arraylist and get the value. Something like
for (int i = 0; i<boxes.size(); i++){
CheckBox cb = boxes.get(i);
if (cb.isChecked()){
System.out.println("checked");
//from here you can populate you array
}else{
System.out.println("not checked");
//from here you can populate you array
}
}
======= Edit after the comment =======
public class CheckBoxStatus{
CheckBox cb;
boolean blnIsChecked;
public CheckBoxStatus(CheckBox cb, boolean blnIsChecked){
this.cb = cb;
this.blnIsChecked = blnIsChecked;
}
public CheckBox getCheckbox(){
return this.cb;
}
public boolean getStatus(){
return this.blnIsChecked;
}
public boolean setStatus(boolean blnIsChecked){
this.blnIsChecked = blnIsChecked;
}
}
//Now in your code instead of ArrayList<CheckBox>, use ArrayList<CheckBoxStatus>
ArrayList<CheckBoxStatus> alCBS = new ArrayList<CheckBoxStatus>();
CheckBoxStatus cbs = new CheckBoxStatus(<checkbox instance>, false); //pass false as default value;
alCBS.add(cbs);
//Modified loop
for (int i = 0; i<boxes.size(); i++){
CheckBox cb = boxes.get(i);
CheckBoxStatus cbs = new CheckBoxStatus(cb, cb.isChecked());
boxes.set(i, cbs);
}
Related
I have these two methods:
one of which creates RadioButtons, gets their names from an arraylist and adds them to a togglegroup and Vbox
second that's supposed to get the name of the selected radiobutton and returns it as a string.
I keep getting nullpointerexception, even though the names print out normally...
private void radioButtons() {
for (int i = 0; i < kaudet.size() - 1; i++) {
RadioButton btn = new RadioButton();
String btnText = kaudet.get(i).getKausiID();
btn.setText(btnText);
System.out.println(btnText);
btn.setToggleGroup(contSeason);
vBoxSeasons.getChildren().add(btn);
}
}
private String kilpKausiget() {
String kausiID = "";
try{
RadioButton chk = (RadioButton)contSeason.getSelectedToggle();
kausiID = chk.getText();
System.out.println(kausiID);
}catch(Error e){
e.printStackTrace();
}
return kausiID;
}
The latter method worked fine, when I had hardcoded the names, but that doesn't really work with what I need.
Thank you in advance!
I have three array
int[] image = {R.drawable.img1,R.drawable.img2} int[] sound= {R.raw.m1,R.raw.m2} String[] nom ={"el1","el2"}
I tryed to change a view with imageview, text from those array when click button but i got only the last value from three array these my method i call when onclick method
private void updateData() {
while (i<nom.length) {
ImageView.setImageResource(image[i]);
textview.setText(nom[i]);
mysong = MediaPlayer.create(Activity.this, sound[i]);
mysong.start();
i++;
}
}
Try this....
Random random = new Random();
private void updateData() {
int random_number = random.nextInt(image.length);
ImageView.setImageResource(image[random_number]);
textview.setText(nom[random_number]);
mysong = MediaPlayer.create(Activity.this, sound[random_number]);
mysong.start();
}
Note : Make Sure Your All Three Array Same Size...
Try this....
int anInt = 0;
And After OnClick
private void updateData() {
if (anInt < image.length - 1) {
anInt++;
} else {
anInt = 0;
}
ImageView.setImageResource(image[anInt]);
textview.setText(nom[anInt]);
mysong = MediaPlayer.create(Activity.this, sound[anInt]);
mysong.start();
}
Note : Make Sure Your All Three Array Same Size...
In my app have twenty edit text,but I want to count filled edit text and that data goes in anther activity through an array. Like when I filled 3 edit text from twenty, that 3 edit text data goes next page and that 3 count goes to next page as an int.
this is my 1st java class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
mspin=(Spinner) findViewById(R.id.spinner1);
submit = (Button) findViewById(R.id.btn1);
layout = (LinearLayout) findViewById(R.id.linear);
lay = (LinearLayout) findViewById(R.id.li);
edt=(EditText) findViewById(R.id.ed2);
sc = (ScrollView) findViewById(R.id.sc1);
int no = 20;
allEds = new ArrayList<EditText>();
for (int i=1;i<=no;i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
edtAdd = new EditText(SelectActivity.this);
layout.setLayoutParams(params);
allEds.add(edtAdd);
edtAdd.setHint("Enter Name" + i);
edtAdd.setId(i);
layout.addView(edtAdd);
}
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (allEds.size()<=9) {
Intent data = new Intent(SelectActivity.this, HalfPieChartActivity.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
data.putExtra("size", str); //you don't need to keep this in loop as its same.
data.putExtra("edt", edt.getText().toString());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData" + j, items[j]);
}
startActivity(data);
}
else {
Intent data = new Intent(SelectActivity.this, FullPieChartActivity.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
data.putExtra("size", str);// this is the line where I sent that count
data.putExtra("edt", edt.getText().toString());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData" + j, items[j]);//here is filled data send line
}
startActivity(data);
}
}
});
}
}
When I click submit 20 show in next page as an int.I want to sent that 3data and 3 as an int.
Please help me
As you have asked for sample in comment, i am posting my answer with sample.
In onclicklistener of your submit button you can add following code.
myNoneEmptyEdittextCounter = 0;
for (int i=1;i<=no;i++)
{
myEt = (EditText) findViewById(i);
if(!TextUtils.isEmpty(myEt.getText().toString()))
{
myNoneEmptyEdittextCounter +=1;
}
}
//myNoneEmptyEdittextCounter is count of your filled edittexts
This method will return list of data and its size will give you the int you are looking for which is number of filled items.
In submit button click
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edittextValues().isEmpty()) {
//nothing has been filled
} else {
//items in edittextValues() are your data.
edittextValues().size();//this is your int which is number of edit text filled.
}
}
}
I have mentioned in comments what each step does. Hope this helps.
Method edittextValues()
ArrayList<String> edittextValues() {
ArrayList<String> filledData = new ArrayList<>(); //initialise empty string array.
for (int i=1; i<=20; i++){ //loop through ids from 1 to 20
for (int j = 0; j< layout.getChildCount(); j++) { //loop through all the children of root layout
if(layout.getChildAt(j) instanceof EditText) { //filter EditText
EditText editText = (EditText) layout.getChildAt(j);
if(editText.getId() == i) { // filter the one which u programmatically added
if(!editText.getText().toString().isEmpty()) { // check if its not empty, that's the one you are looking
filledData.add(editText.getText().toString()); //add it to the list
}
}
}
}
}
return filledData; //return string list
}
I am working on my project where I want to show when application starts then calendar display, which date contain events, for instance if the date contain events, then the day button contains * symbol and day, And if the date doesn't contain any event then it only displays a day.
I wrote following code, but it only displays * symbol when I am clicking on that button, So how can I manage this code that display * symbol on the date which only contain events when the application starts or that page gonna be loaded.
Following is my code:-
public class Customised extends Calendar{
ArrayList<String[]> data = new ArrayList<>();
int i,j,columns;
#Override
protected void updateButtonDayDate(Button dayButton,int currentMonth, int day) {
dayButton.setText(""+day);
dayButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
//Check which date having how many number of events===============================================================
try{
ShowEvent.removeAll();
cur = db.executeQuery("SELECT Event, Description from CalendarData WHERE Date = ? ", dateLabel.getText());
columns = cur.getColumnCount();
if(columns > 0) {
boolean next = cur.next();
if(next) {
String[] columnNames = new String[columns];
for(int iter = 0 ; iter < columns ; iter++) {
columnNames[iter] = cur.getColumnName(iter);
}
while(next) {
Row currentRow = cur.getRow();
String[] currentRowArray = new String[columns];
for(int iter = 0 ; iter < columns ; iter++) {
currentRowArray[iter] = currentRow.getString(iter);
}
data.add(currentRowArray);
next = cur.next();
}
Object[][] arr = new Object[data.size()][];
data.toArray(arr);
}
}
}catch(IOException e){
e.printStackTrace();
}
for(i = 0 ; i< data.size(); i++){
Log.p(data.get(i)[0]);
}
Label a = new Label(dateLabel.getText());
Label b = new Label(" "+i);
Container container1 = TableLayout.encloseIn(2, a,b);
container1.setUIID("container1");
ShowEvent.add(container1);
for( i = 0 ; i< data.size(); i++){
for(j = 0; j<columns; j++){
Log.p(data.get(i)[j]);
SpanLabel spanData = new SpanLabel(data.get(i)[j]);
spanData.setUIID("SpanLabel");
ShowEvent.add(spanData);
}
Label space = new Label("=======================");
ShowEvent.add(space);
Log.p("###################");
}
data.clear();
if(i>0){
if(Dialog.show("Choose action", "What you want to do?", "Add Events","View Events")){
calendar.show();
}
else{
ShowEvent.show();
}
}else{
Dialog.show("Add event","There is no event to display, Please add events first","OK","");
}
//============================================================================================================
}
});
}
#Override
protected void initComponent(){
ArrayList<String[]> data1 = new ArrayList<>();
int k;
Log.p("initComponent");
try{
cur = db.executeQuery("select Date from CalendarData");
columns = cur.getColumnCount();
if(columns > 0) {
boolean next = cur.next();
if(next) {
String[] columnNames = new String[columns];
for(int iter = 0 ; iter < columns ; iter++) {
columnNames[iter] = cur.getColumnName(iter);
}
while(next) {
Row currentRow = cur.getRow();
String[] currentRowArray = new String[columns];
for(int iter = 0 ; iter < columns ; iter++) {
currentRowArray[iter] = currentRow.getString(iter);
}
data1.add(currentRowArray);
next = cur.next();
}
Object[][] arr = new Object[data1.size()][];
data1.toArray(arr);
}
}
}catch(IOException e){
e.printStackTrace();
}
for(k = 0 ; k< data1.size(); k++){
Log.p(data1.get(k)[0]);
}
if(k>0){
//cal.setUIID("CalendarSelectedDay");
}
}
/*
#Override
protected boolean isInitialized(){
boolean result = false;
Log.p("isInitialised");
return result;
}*/
public Customised(){
}
#Override
protected Button createDay() {
Button day = new Button();
day.setAlignment(CENTER);
day.setUIID("CalendarDay1");
day.setEndsWith3Points(false);
day.setTickerEnabled(false);
return day;
}
}
And the expected result will be:-
That's because you placed the code inside the actionPerformed method which is only triggered upon Button pressed/released.
Move your code to the updateButtonDayDate scope
Hi I have two array values
loncountries[index] = [80.2116001, 80.173399]
latcountries[index] = [13.042699, 13.0409047]
Now I want them in a new Array like
loncountries[index] = [13.042699,80.2116001]
latcountries[index] = [13.0409047,80.173399]
Just the first columns as a new Array. Also I am fetcihg it from Sqlite Database and attaching the code below. Is it possible to do it during fetching from db.
String[] latcountries;
String[] loncountries;
if (cJoin != null) {
int countJoin = cJoin.getCount();
if (countJoin > 0) {
latcountries = new String[countJoin];
loncountries = new String[countJoin];
int index = 0;
cJoin.moveToFirst();
while (!cJoin.isAfterLast()) {
loncountries[index] = cJoin.getString(0);
latcountries[index] = cJoin.getString(1);
index++;
cJoin.moveToNext();
}
}
}
one approach could be to declare a bean-like class,
public class Position {
public String mLat;
public String mLon;
}
and then declare an array of position:
Position[] latLon = new Position[countJoin];
at every iteration then
while (!cJoin.isAfterLast()) {
Position p = new Position();
p.mLat = cJoin.getString(1);
p.mLon = cJoin.getString(0);
latLon[index++] = p;
}