Total value of multiple textviews into a single textiew? - android

I used the answer to this question to get the basic value of the TextView clicks into a single TextView .This code gives a per click value, I want to know how to add the existing value of TextView.
Note: m1aa and m1ab are number variables in a previous activity and received through intent on this one.
UPDATE This question should have been "how do you convert a string to a decimal."
//Original non working code.
private int productTotal1;
private int productTotal2;
private int overallTotalproduct;
private TextView m1a,
private TextView m1aa,
//and then way down the page
productTotal1 = 0;
productTotal2 = 0;
overallTotalproduct = 0;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
textView1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal1++;
overallTotalproduct = productTotal1 + productTotal2;
textView1.setText(String.valueOf(productTotal1));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
textView2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal2++;
overallTotalproduct = productTotal1 + productTotal2;
textView2.setText(String.valueOf(productTotal2));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
***UPDATE***WORKS FINE :)
AFTER GETTING HELP AND "THINKING" ABOUT WHAT I WAS WRITING I END UP WITH...
//Working version
private double overallTotalproduct;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final String stringm1aa = textView1.getText().toString();
final double intm1aa = Double.parseDouble(stringm1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
final String stringm1ab = textView2.getText().toString();
final double intm1ab = Double.parseDouble(stringm1ab);

You can use following code to get a value of TextView and convert it to integer
String s = textView.getText().toString();
int n = Integer.parseInt(s);
Remember NumberFormatException will be thrown if the string does not contain a parsable integer.

Related

Android getting value empty from custom list view Edittext

Am getting value here for Checkbox, and textview properly. But, I could not get the EditText Values. am getting empty only.
my sample code:
View view = null ;
for(int i = 0; i < list.getCount(); i++ ) {
view = list.getAdapter().getView(i, null, null) ;
TextView textViewNickName = (TextView) view.findViewById(R.id.textview);
String textviewValue = textViewNickName.getText().toString().trim() ;
Log.v("ppi", "textViewNickName::"+textviewValue);
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
CheckBox checkBoxOne = (CheckBox) view.findViewById(R.id.checkBox1) ;
Log.v("ppi", "checkBoxOne::"+checkBoxOne.isChecked());
}
I referred this link also :
Iterate through ListView and get EditText-Field values
Thanks Advance
You have mistake to get value from EditText, any event time you can get value initialization time you can't get text from EditText
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
see above code that in your mistake if you have no enter any string in EditText then how can you retrieve initialization time.
you have to make change in you code as like blow.
EditText edittext = (EditText) view.findViewById(R.id.edittext_amount) ;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String edittextValue = edittext.getText().toString().trim() ;
Log.v("ppi", "Amount edittextValue::"+edittextValue);
}
});

How to solve : number Format Exception Invalid int:""

I am beginner in android app development. My code is as follows- How to solve Number Format Exception in this code-
public class InterestActivity extends AppCompatActivity {
private EditText editAmount;
private EditText editInterest;
private EditText editDuration;
private TextView resultView;
private Button Calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interest);
editAmount = (EditText) findViewById(R.id.editAmount);
editInterest = (EditText) findViewById(R.id.editInterest);
editDuration = (EditText) findViewById(R.id.editDuration);
resultView = (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
}
public void interestCalculate (View v) {
resultView= (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
int a = Integer.parseInt(editAmount.getText().toString());
int i = Integer.parseInt(editInterest.getText().toString());
int d = Integer.parseInt(editDuration.getText().toString());
int ir= i/100;
float res = a*ir*d;
int intres= (int) res;
resultView.setText(Integer.toString(intres));
}}
public void interestCalculate (View v) {
resultView= (TextView) findViewById(R.id.resultView);
Calculate = (Button) findViewById(R.id.button4);
int a = 0;
int i = 0;
int d = 0;
try{
a= Integer.parseInt(editAmount.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editAmount"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
try{
i = Integer.parseInt(editInterest.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editInterest"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
try{
d = Integer.parseInt(editDuration.getText().toString());
}catch(Throwable e){
Toast.makeText(getApplicationContext(),"editDuration"+ "'s value is not a Integer",Toast.LENGTH_LONG);
}
int ir= i/100;
float res = a*ir*d;
int intres= (int) res;
resultView.setText(Integer.toString(intres));
}}
You are assigning the Views to temporary local variables instead of the attributes of the class.
Short solution:
Remove EditText before editAmount = ...
Likewise with all the other assignments inside onCreate.
If you want to continue Android programming you should understand why this will work. This might help you: What is the difference between field, variable, attribute, and property in Java POJOs?

How to make decision in onClick

This is for searching the word on the TextView
public void viewWord(View view)
{
score = (TextView)findViewById(R.id.yourScore);
tv2 = (TextView)findViewById(R.id.tv2);
tv3 = (TextView)findViewById(R.id.tv3);
searchWord = (ImageView) findViewById(R.id.searchWord);
wordList = (ListView) findViewById(R.id.wordList);
text = (AutoCompleteTextView) findViewById(R.id.textHere);
wordList = (ListView) findViewById(R.id.wordList);
//SearchWord in AutoCompleteTextView text
//Display equivalent score of word in TextView score
String s1= search.getText().toString();
String s2= dbHelper.getData(s1);
score.setText(s2);
tv2.setText(s2);
tv3.setText(s2);
//Get data from textview
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
wordList.setAdapter(adapter);
adapter.add(text.getText().toString());
adapter.notifyDataSetChanged();
text.setText("");
add();
}
public void add (){
x = Integer.parseInt(tv2.getText().toString());
y = Integer.parseInt(tv3.getText().toString());
z = x + y;
score.setText(Double.toString(z));
}
In my app, I am using an ImageView to search the word and display the score. I am having a hard time in displaying the score using 1 TextView so I decided to make it 2. I want my ImageView to function like this:
if(onMyImageViewFirstClick){
display the score on textview1
}
else{
if(onMyImageViewSecondClick){
display the score on textview2
}
how can i do it this way? Any suggestions will be highly appreciated :)
Note: I am new in android programming. I would appreciate if you'll also help me with the codes
You can do it with a class variable changin his value between 0 - 1, for example:
private int optionTxtView = 0
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(optionTxtView == 0){
//display the score on textview1
optionTxtView = 1;
}else{
//display the score on textview2
optionTxtView = 0;
}
}
});
or you can use a boolean value (suggested by #mapo)
private boolean optionTxtView;
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(optionTxtView){
//display the score on textview1
optionTxtView = false;
}else{
//display the score on textview2
optionTxtView = true;
}
}
});
So, according to your code, the solution must be:
//declare a boolean variable
private int optionTxtView = 0
public void viewWord(View view) {
...
...
...
//SearchWord in AutoCompleteTextView text
//Display equivalent score of word in TextView score
String s1= search.getText().toString();
String s2= dbHelper.getData(s1);
score.setText(s2);
if(optionTxtView == 0){
//display the score on textview1
tv2.setText(s2);
optionTxtView = 1;
}else{
//display the score on textview2
tv3.setText(s2);
optionTxtView = 0;
}
...
...
...
}
Here's how you can implement a click counter.
Note how you reset the counter when a total of 2 clicks are registered. Once that happens the counter starts over and again will first affect tv1 and then tv2.
// Find TextViews before (so you don't have to repeat this expensive operation)
final TextView tv1 = findViewById(R.id.textView1);
final TextView tv2 = findViewById(R.id.textView2);
imageButton.setOnClickListener(new View.OnClickListener() {
int numberOfClicks = 0;
#Override
public void onClick(View view) {
++numberOfClicks;
if(numberOfClicks < 2){
tv1.setText("Some text");
} else if(numberOfClicks >= 2){
tv1.setText("Some other text");
// Reset the click counter
numberOfClicks = 0;
}
}
});

Having trouble finding a textview by the id

I use a menu inflater and in every row I give the textview and seekbar an id of i(I use a for loop to give them there id). When I get the id of the seekbar and I want to insert text into the textview. The problem I am having is how do I set the text in the textview that has the same value as the seekbar.
public static void setNewTipAmount(SeekBar barChanged, double amountForTip) {
int getID = barChanged.getId(); //get seekbar id
TextView textView = tipPerPerson.findViewById(getID);// get the textview with same id
textView.setText("Hello");
}
}
Here is the code to how I assign the Id's
for (int i = 0; i < InBetweenUIAndBusinessLogic.getGuests(); i++) {
View v = in.inflate(R.layout.row_per_person, table, false);
double tipPerIndividual = (Math.round(InBetweenUIAndBusinessLogic
.getTipPerPerson() * 100.0) / 100.0);
String tip = Double.toString(tipPerIndividual);
tipPerPerson = (TextView) v.findViewById(R.id.tipPerPerson);
tipPerPerson.setId(i);
tipPerPerson.setText(tip);
rows.add(tipPerPerson);
percentageTip = (SeekBar) v.findViewById(R.id.seekBar1);
percentageTip.setOnSeekBarChangeListener(new myListener());
double guests = InBetweenUIAndBusinessLogic.getGuests();
percentageTip.setProgress((int) (100 / guests));
percentageTip.setId(i);
table.addView(v);
bars.add(percentageTip);
}
I suggest creating a simple mapping between TextView id and Seekbar id. This way each id will be different and you can still get the corresponding item id. Here is my solution:
mGuestCount = InBetweenUIAndBusinessLogic.getGuests();
for (int i = 0; i < InBetweenUIAndBusinessLogic.getGuests(); i++) {
View v = in.inflate(R.layout.row_per_person, table, false);
double tipPerIndividual = (Math.round(InBetweenUIAndBusinessLogic
.getTipPerPerson() * 100.0) / 100.0);
String tip = Double.toString(tipPerIndividual);
tipPerPerson = (TextView) v.findViewById(R.id.tipPerPerson);
tipPerPerson.setId(i);
tipPerPerson.setText(tip);
rows.add(tipPerPerson);
percentageTip = (SeekBar) v.findViewById(R.id.seekBar1);
percentageTip.setOnSeekBarChangeListener(new myListener());
double guests = InBetweenUIAndBusinessLogic.getGuests();
percentageTip.setProgress((int) (100 / guests));
percentageTip.setId(i + mGuestCount);
table.addView(v);
bars.add(percentageTip);
}
Now to find the textview:
public static void setNewTipAmount(SeekBar barChanged, double amountForTip) {
int getID = barChanged.getId(); //get seekbar id
TextView textView = tipPerPerson.findViewById(getID - mGuestCount);// get the textview with corresponding id
textView.setText("Hello");
}

defining a view based upon a int

is there anyway to change which view you are editing based upon the value of a int?
Without using a if statement?
for example:
int counter = 1;
public void attach(){
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
TextView text1 = (TextView) findViewById(R.id.text1)
TextView text2 = (TextView) findViewById(R.id.text2)
TextView text3 = (TextView) findViewById(R.id.text3)
TextView text4 = (TextView) findViewById(R.id.text4)
if (counter < 5){
if (sp.getInt("test" + counter , 0) == counter){
text + counter .setText("test" + counter);
}else{counter = counter + 1; attach();}}
}
Simplest way is to define an array of possible view IDs.
int[] viewIds = new int[] {
R.id.text1,
R.id.text2,
R.id.text3,
R.id.text4
}
//some code
int i = /*whatever*/;
TextView tv = (TextView)findViewById(viewIds[i]);
tv.setText("test"+i);

Categories

Resources