I am creating an application consists of two drop down spinners(Example:Spinner A and Spinner B). By clicking button i want to get subtracted value from spinner A and B with that final subtracted value i want to create table rows with inside edit text fields.i had written some code. When i clicks the button no error was prompting but there was no table creation happening.This is what i did below when user clicks button please help me with this where i did them mistake
get_sheet = (Button)findViewById(R.id.button_get_sheet);
get_sheet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinner_from_date_text = from_Date.getSelectedItem().toString();
spinner_from_month_text = from_month.getSelectedItem().toString();
spinner_from_year_text = from_year.getSelectedItem().toString();
spinner_to_date_text = to_Date.getSelectedItem().toString();
spinner_to_month_text = to_month.getSelectedItem().toString();
spinner_to_year_text = to_year.getSelectedItem().toString();
from_date.setText(spinner_from_date_text+"/"+spinner_from_month_text+"/"+spinner_from_year_text);
to_date.setText(spinner_to_date_text+"/"+spinner_to_month_text+"/"+spinner_to_year_text);
int value_from = Integer.valueOf(spinner_from_date_text);
int value_to = Integer.valueOf(spinner_to_date_text);
int result_table = value_to-value_from;
sheet.setColumnStretchable(5, true);
for(int i=0;i<result_table;i++)
{
sheet_row = new TableRow(MainActivity.this);
sheet_row.setLayoutParams(new LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,android.widget.TableRow.LayoutParams.WRAP_CONTENT));
for(int j=0 ;j<5;j++)
{
edt_text = new EditText(MainActivity.this);
edt_text.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
edt_text.setBackgroundResource(R.drawable.edt);
}
sheet_row.addView(edt_text);
}
sheet.addView(sheet_row);
Related
I am having trouble with my output screen!
When I click on Click button with these inputs output screen looks like attached image, which is so far great!
But if I change my input, the program gives new answer by adding more rows with previous answers! I want only new answers on screen to be shown!
Also without updating input if I click on button same way screen adds up new rows!
I am including a picture of this also..
I used this code given below,
clickbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
aI = Double.parseDouble(Ia.getText().toString());
bI = Double.parseDouble(Ib.getText().toString());
cI = Double.parseDouble(Ic.getText().toString());
bisection();
}
});
private void bisection() {
if ((f(aI) < 0 && f(bI) > 0) || (f(aI) > 0 && f(bI) < 0)) {
for (int i = 0; i < cI; i++) {
View tableRow = LayoutInflater.from(this).inflate(R.layout.table_item, null, false);
TextView iteration = (TextView) tableRow.findViewById(R.id.index);
TextView a = (TextView) tableRow.findViewById(R.id.a);
TextView b = (TextView) tableRow.findViewById(R.id.b);
TextView x = (TextView) tableRow.findViewById(R.id.x);
TextView fx = (TextView) tableRow.findViewById(R.id.fx);
double root = (aI+bI)/2;
iteration.setText(" " + (i + 1));
a.setText(Double.toString(aI));
b.setText(Double.toString(bI));
x.setText(Double.toString(root));
fx.setText(Double.toString(f(root)));
tableLayout.addView(tableRow);
if(f(aI)*f(root) < 0){
bI = root;
}else if (f(aI)*f(root) >0) {
aI = root;
}
}
} else {
Toast.makeText(getApplicationContext(), R.string.popUpMsg,Toast.LENGTH_SHORT).show();
}
}
public static double f(double x){
return ((Math.pow(x,3))-x-4);
}
I have already found almost same problem has been solved in a post previously asked by someone else but I couldn't fix mine! Help me. Thanks!
In your button click listener method, add following line of code before calling bisection().
tableLayout.removeAllViews();
Whenever, you will click the button, previous output will be removed before calculating new input values.
Your code would look like this:
clickbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
aI = Double.parseDouble(Ia.getText().toString());
bI = Double.parseDouble(Ib.getText().toString());
cI = Double.parseDouble(Ic.getText().toString());
tableLayout.removeAllViews(); // Remove previous output
bisection();
}
});
first way; You have to use
removeAllViews();
Before adding. To do this, you have to have a layout variable around. ex;
linearLayout.removeAllViews();
Another way is adding the Textviews in the beginning with VIEW.INVISIBLE or VIEW.GONE
Whichever suits you.
Hello, I create buttons dynamic and now I want when one of this buttons click, pass to another layout, but I can't! maybe somebody can give me solution, thank you
onCreate:
enter code here//-------------------Creating Buttons as per in server -------------------------------
final Button[] myButton = new Button[jsonArray.length()];
int count = jsonArray.length();
for (int i = 0; i < jsonArray.length(); i++) { //upside down -> HAFUH
// for (int i = jsonArray.length(); i > 0 ; i--) {
final Button rowButton = new Button(this);// create a new button
--count;//to upside down the buttons, that mean: check last will be on Top
// rowButton.setText(dateIn[count] + " KM: "+ kmIn[count]+" GAS: "+ gasIn[count]);
rowButton.setText( "KM: "+ kmIn[count]+" GAS: "+ gasIn[count]+" "+dateIn[count]);
rowButton.setId(Integer.parseInt(idIn[count]));//string convert to int "ID"
/*-----------Build onClick and what happened after ------------*/
rowButton.setOnClickListener(btnClick(myButton[i]));//WHEN Click on button
/*-------------FINISH Build onClick and what happened after---*/
b.addView(rowButton);
// save a reference to the button for later
myButton[i] = rowButton;
}//end for jsonArray
/************Finish Creating Buttons as per in server**********************************/
}//END IF FOR JSON AND BUILD BUTTON
}//End Create Button
outside onCreate method, in the class:
enter code hereprivate View.OnClickListener btnClick(final Button button) {
return new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(this,GalleryHistoryCheckIn.class);
startActivity(intent);
}
};
}
I expect just pass to another class
HAHA I understand what was my problem, this is answer
method onCreate:
final Button[] myButton = new Button[jsonArray.length()];
int count = jsonArray.length();
for (int i = 0; i < jsonArray.length(); i++) { //upside down -> HAFUH
// for (int i = jsonArray.length(); i > 0 ; i--) {
final Button rowButton = new Button(this);// create a new button
--count;//to upside down the buttons, that mean: check last will be on Top
// rowButton.setText(dateIn[count] + " KM: "+ kmIn[count]+" GAS: "+ gasIn[count]);
rowButton.setText( "KM: "+ kmIn[count]+" GAS: "+ gasIn[count]+" "+dateIn[count]);
rowButton.setId(Integer.parseInt(idIn[count]));//string convert to int "ID"
/*-----------Build onClick and what happened after ------------*/
// rowButton.setOnClickListener(btnClick(myButton[i]));//WHEN Click on button
/*-------------FINISH Build onClick and what happened after---*/
b.addView(rowButton);
// save a reference to the button for later
myButton[i] = rowButton;
myButton[i].setOnClickListener(btnClick(rowButton));
}//end for jsonArray
/************Finish Creating Buttons as per in server**********************************/
}//END IF FOR JSON AND BUILD BUTTON
}//End Create Button
outside of on create but in class:
private View.OnClickListener btnClick(final Button button) {
return new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(v.getContext(),GalleryHistoryCheckIn.class);
intent.putExtra("result", v.getId());
startActivity(intent);
}
};
}
I am developing a simple questionnaire-like app which includes lots of radio buttons joined into groups and spinners. I have multiple activities (6); some of them having RBs and some Spinners to let the user answer the questions.
The following step, which I have trouble with, is how to fetch lots of selections (of all the radio buttons/choices) and possibly do that in a for loop (so I don't have to initialize each new variable 30+ times in a row for just one activity). I've already assigned IDs to all of the views, but am having a hard time how to actually fetch the selection, initialize a new var corresponding to the selection (let's say radio button 1 in radio group 1 gives me a new variable with a value of 1) and then make the variables available to all of the activities (should I use global when initializing?).
My failed attempt on generating 10 variables for the first "page"
public void goTo2(View v) {
checkRB();
Intent intent1 = new Intent(Vprasalnik1.this, Vprasalnik2.class);
startActivity(intent1);
finish();
}
public void checkRB()
{
for (int i=0;i<9;i++)
{
RadioButton "vRB" + i; //I'd like to loop and initialize vars by adding a number to them (vRB1, vRB2, ...)
}
}
Put variables into array like a
int size = 9;
RadioButton[] views = new RadioButton[size];
public static checkRB()
{
for(int i=0;i<size;i++)
{
views[i] = (RadioButton)findViewByID(...);//For example
}
}
Or make a structure :
public class Choise
{
int mRadioButtonChoise;
int mSpinnerChoise;
}
And use something like this:
...
Choise c = new Choise();
c.mRadioButtonChoise = yourRadioButtonID;
c.mSpinnerChoise = youtSpinnerChoiseID;
...
Using a variable to identify a resource:
RadioButton[] rb = new RadioButton[size];
public static checkRB()
{
for(int i=0;i<size;i++)
{
int id = context.getResources().getIdentifier("vRB" + i, "id", context.getPackageName())
rb[i] = (RadioButton)findViewByID(id);
}
}
If you have an array of RadioButtons then you can get all the values at the same time, however initializing them will have to be manual.
RadioButton rb[];
boolean rbc[];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rbc=new boolean[200];
rb=new RadioButton[200]();
rb[0]=(RadioButton)findViewById(R.id.rb1);
rb[1]=(RadioButton)findViewById(R.id.rb2);
rb[2]=(RadioButton)findViewById(R.id.rb3);
rb[3]=(RadioButton)findViewById(R.id.rb4);
// many more.
}
public void checkRB()
{
for (int i=0;i<9;i++)
{
rbc[i]=rb.isChecked(); //I'd like to loop and initialize vars by adding a number to them (vRB1, vRB2, ...)
}
}
Then before starting your intent add all relevant data to it.
So I've managed to cramp up the radio buttons activity, so that it finally works. If anyone is interested - I've used tags in xml code to properly assign values (1, 2 and 3 for each group of buttons) and managed to get an output in my testToast. At least I didn't have to initialize all of the variables manually - I've been saving the values into an ArrayList and then appended to them via StringBuilder.
Thanks to everyone who tried to help - it turned out I've needed a bit more research, testing and teasing my half-awake brain.
btn = (Button) findViewById(R.id.v3_btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 1; i <= 36; i++)
{
tmpRGid = "radioGroup_v3q" + i;
tmp2RGid = getResources().getIdentifier(tmpRGid, "id", getPackageName());
RGid = (RadioGroup) findViewById(tmp2RGid);
selectedOption = RGid.getCheckedRadioButtonId();
RBid = (RadioButton) findViewById(selectedOption);
addToIDList.add((String)RBid.getTag());
}
String testToast = "";
StringBuilder builder = new StringBuilder();
builder.append("Vaša izbira (");
for (int z=0; z < addToIDList.size(); z++) {
testToast = addToIDList.get(z);
builder.append(testToast + ", ");
}
builder.setLength(builder.length() - 2);
builder.append(") je bila shranjena.");
Toast.makeText(Vprasalnik3.this, builder, Toast.LENGTH_LONG).show();
I'm dynamically adding rows to the table. I have few textViews[] that I add to the row[] and then I add row[] to the table. That's ok, but then I want to detect click on textView[] and delete that row, also I want the numbers of the textViews below deleted ones to decrease.
example:
1 a a a //(I delete this row)
2 b b b
3 c c c
after deleting, I want to have this:
1 b b b
2 c c c
I have tried adding onClickListener to one textView[], but sometimes it deletes row[] and decreases number, sometimes don't.
Can someone write me some example to try out?
EDIT: Here's my code (I think it's all that's needed for this)
This is the code on my button for adding rows:
public TextView textViewKoeficijent[] = new TextView[50];
public TextView textViewBr[] = new TextView[50];
public TextView textViewObrisi[] = new TextView[50];
public TextView textViewTip[] = new TextView[50];
public TextView textViewPar[] = new TextView[50];
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun * fkoeficijent;
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);
final TableLayout PopisParova = (TableLayout) findViewById(R.id.TableLayout);
final TableRow noviPar[] = new TableRow[50];
LayoutParams paramsBroj = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
paramsBroj.setMargins(4, 0, 2, 4);
LayoutParams paramsPar = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
paramsPar.setMargins(2, 0, 2, 4);
LayoutParams paramsKoef = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
paramsKoef.setMargins(2, 0, 4, 4);
//onclicklistener:
OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
brpara --;
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if(brpara==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
}
else{
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);}
PopisParova.removeViewAt(id);
//PopisParova.removeView(noviPar[id]);
for(i=1; i<=brpara; i++){
if(i>id){
String bri = String.valueOf(i-1);
textViewBr[i].setText(bri);
textViewObrisi[i].setDrawingCacheBackgroundColor(i-1);
}
}
}};
{
textViewBr[brpara] = new TextView(MainActivity.this);
textViewBr[brpara].setLayoutParams(paramsBroj);
textViewBr[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewBr[brpara].setBackgroundColor(0xFFFFFFFF);
brojPara = String.valueOf(brpara);
textViewBr[brpara].setText(brojPara);
textViewBr[brpara].setId(brpara);
}
{
textViewPar[brpara] = new TextView(MainActivity.this);
textViewPar[brpara].setLayoutParams(paramsPar);
textViewPar[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewPar[brpara].setBackgroundColor(0xFFFFFFFF);
textViewPar[brpara].setText(par);
textViewPar[brpara].setId(brpara);
}
{
textViewTip[brpara] = new TextView(MainActivity.this);
textViewTip[brpara].setLayoutParams(paramsPar);
textViewTip[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewTip[brpara].setBackgroundColor(0xFFFFFFFF);
textViewTip[brpara].setText(tip);
textViewTip[brpara].setId(brpara);
}
{
textViewKoeficijent[brpara] = new TextView(MainActivity.this);
textViewKoeficijent[brpara].setLayoutParams(paramsPar);
textViewKoeficijent[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewKoeficijent[brpara].setBackgroundColor(0xFFFFFFFF);
textViewKoeficijent[brpara].setText(koeficijent);
textViewKoeficijent[brpara].setId(brpara);
}
{
textViewObrisi[brpara] = new TextView(MainActivity.this);
textViewObrisi[brpara].setLayoutParams(paramsKoef);
textViewObrisi[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewObrisi[brpara].setBackgroundColor(0xFFFFFFFF);
textViewObrisi[brpara].setText("X");
textViewObrisi[brpara].setId(brpara);
textViewObrisi[brpara].setClickable(true);
textViewObrisi[brpara].setOnClickListener(onClickListener);
}
noviPar[brpara] = new TableRow(MainActivity.this);
noviPar[brpara].addView(textViewBr[brpara]);
noviPar[brpara].addView(textViewPar[brpara]);
noviPar[brpara].addView(textViewTip[brpara]);
noviPar[brpara].addView(textViewKoeficijent[brpara]);
noviPar[brpara].addView(textViewObrisi[brpara]);
PopisParova.addView(noviPar[brpara]);
brpara++;
editTextPar.setText("");
editTextTip.setText("");
editTextKoeficijent.setText("");
EDIT [2]:
brpara is my counter so I know which is number of row which is being added. I have a max of 20 rows.
Also, my loop is working perfectly for deleting one row, but when I delete multiple rows at once, it only changes my row numbers for the first time and after that only deletes rows.
What are you doing is this:
Let's say that brpara = 3.
You click the view with id = 2.
You execute brbara --; So brbara is now 2.
You check if id != brpara they are NOT so no renumbering. Logical Problem!
Furthermore you have this loop:
for(i=id+1; i<=brpara; i++){
i=id+1;
String bri = String.valueOf(id);
textViewBr[i].setText(bri);
textViewObrisi[i].setId(id);}
You say that i must be id+1 (in our example 3) to < brpara (in our example 2) so 3 <= 2 it never executes, if however id and brpara allows loop execution you resetting the i inside the loop
i=id+1;
If for example the loop starting with i = id+1 let's say id=2 so it is now 3 and go inside the loop the i will be always 3 because you are resetting it inside the loop.
It is not clear from your code how are you using brpara but the loop for renumbering is wrong. So let's assume that brpara is ALWAYS the last index in the array then I suggest the following code:
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if(brpara==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
}
else{
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);}
//FIRST RENUMBER ALL VIEWS FROM id+1 to brpara
if(id!=brpara){
for(i=id+1; i<=brpara; i++){
String bri = String.valueOf(i - 1); //<--HERE MAYBE MUST BE i-1 NOT id...
textViewBr[i].setText(bri);
textViewObrisi[i].setId(id);} //THIS DOESN'T FEEL RIGHT MAYBE i INSTEAD OF id? OR i - 1?
}
//AND FINALY REMOVE VIEW AND DECREASE brpara...
PopisParova.removeViewAt(id);
brpara --;
}
My suggestion assumes that brpara is always the last index and you are renumbering from the clicked view + 1 to the end. However there is a more elegant solution which is to create a function that renumbers the whole array from top to bottom regardless the view clicked and call it whenever a view gets added or removed. That way you will not have to manage brpara at all but this depends on what exactly you are trying to achieve.
Based on your Edits
Try this:
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if((brpara - 1)==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
} else {
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);
PopisParova.removeViewAt(id);
brpara --;
for(i=1; i<=brpara; i++){
String bri = String.valueOf(i);
textViewBr[i].setText(bri);
textViewObrisi[i].setDrawingCacheBackgroundColor(i);
}
}
}
Since brpara is a counter you should decrease it only when you are actually remove the view. You may have to change the
if((brpara - 1)==1){
with
if(brpara==1){
based on your needs. But now the loop seems to be OK.
Hope this helps...
The only possible solution is to replace deleted rows, and their data, with rows following, and their data and delete last row. So it looks like the deleted row is really deleted.
row[x] = row[x+1] // want to delete this
row[x+1] = row[x+2]
//... and like that until the last row
row[last] // delete this row
I am looking at creating a Dialog with a bundle of data. This all work correctly.
I have a number of buttons when clicked opens the dialog with the data and can then be updated.
Trouble I am having is it does not matter which button I press the bundle is only the data for the last row. Any ideas on getting the correct data passed to the Dialog for each button.
I was thinking down the line of setting an id for each view as I pass through the loop but unsure how to call that back again
Code:
for (int i = 0; i < nameInfo.size(); i++) {
// creating the views
View viewItem = (View) inflater.inflate(R.layout.view_item, null);
nameView = (TextView) viewItem.findViewById(R.id.title);
nameView.setId(i);
value1View = (TextView) viewItem.findViewById(R.id.value1);
value1View.setId(i);
value2View = (TextView) viewItem.findViewById(R.id.value2);
value2View.setId(i);
updateButton = (Button) sightmarkView.findViewById(R.id.updatebutton);
updateButton.setId(i);
// Getting the values
nameValue = nameInfo.get(i).toString();
value1 = db.getvalue1('1', nameInfo.get(i).toString());
value2 = db.getvalue2('2', nameInfo.get(i).toString());
// update fields
nameView.setText(nameValue);
value1View.setText(String.valueOf(value1));
value2View.setText(String.valueOf(value2));
updateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int updateButtonId = updateButton.getId();
bundle = new Bundle();
bundle.putString("name", nameValue);
bundle.putFloat("value1", value1);
bundle.putFloat("value2", value2);
showDialog(SIGHTMARK_DIALOG_ID, bundle);
}
});
pMainlayout.addView(viewItem);
}
Thanks for your time
In each loop (from the for), you are redefining the values of nameValue, value1 and value2.
To fix the problem, just change your code to match this:
// Getting the values
final String nameValue = nameInfo.get(i).toString();
final String value1 = db.getvalue1('1', nameInfo.get(i).toString());
final String value2 = db.getvalue2('2', nameInfo.get(i).toString());