Here's my codes
//declare a boolean variable for score
private int optionTxtView = 0 ;
//Search word
//display score
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);
String s1= search.getText().toString();
String s2= dbHelper.getData(s1);
if(optionTxtView == 0){
//display the score on textview1
tv2.setText(s2);
optionTxtView = 1;
}
else{
//display the score on textview2
tv3.setText(s2);
optionTxtView = 0;
}
calculate();
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(null);
}
private void calculate(){
x = Integer.parseInt(tv2.getText().toString());
y = Integer.parseInt(tv3.getText().toString());
z = x + y;
score.setText(Integer.toString(z));
}
So basically i decided to have 3 textviews, score, tv2 and tv3. So the first score for the first word should be in the tv2 and the second score should be in tv3. and the sum of this two textviews should be in score.
After putting text on the textview, it will search for the word from the textview in the database by hitting imagebutton. If the word is in the database, it will automatically put the word in the listview and at the same time it SHOULD display the score on two textviews.
I really need help on this. I tried everything i can but it never adds EXCEPT when the value/number in the textview tv2 and tv3 is set from the properties. But whenever the numbers/value is from the database (because that's what it should be) it NEVER ADDS !
Any help will be highly appreciated. Please help me with my codes.
onCreate Method
//TAKE NOTE: 16 IMAGEBUTTONS
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper =new DBAdapter(this);
search = (EditText) findViewById(R.id.textHere);
words = (EditText) findViewById(R.id.edWord);
scores = (EditText) findViewById(R.id.edScore);
show_addWord = (ImageView) findViewById(R.id.show_addWord);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
wordList.setAdapter(adapter);
score = (TextView)findViewById(R.id.yourScore);
tv2 = (TextView)findViewById(R.id.tv2);
tv3 = (TextView)findViewById(R.id.tv3);
wordList = (ListView) findViewById(R.id.wordList);
text = (AutoCompleteTextView) findViewById(R.id.textHere);
wordList = (ListView) findViewById(R.id.wordList);
blizzard = (ImageView) findViewById(R.id.blizzard);
bomb = (ImageView) findViewById(R.id.bomb);
searchWord = (ImageView) findViewById(R.id.searchWord);
wordList = (ListView) findViewById(R.id.wordList); //this is til 16
image1 = (ImageButton) findViewById(R.id.Button1);
image16 = (ImageButton) findViewById(R.id.Button16);
timer = (TextView) this.findViewById(R.id.timer);
timer.setText("00:00:30");
countDownTimer = new MyCountDownTimer(30000,1000);
final AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.textHere);
final ImageButton image1 = (ImageButton) findViewById(R.id.Button1); //this is til 16
final ImageButton image16 = (ImageButton) findViewById(R.id.Button16);
final int[] myPics = { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.e }; //this is til z
//this is til 16
int rando = (int)(Math.random()* 5);
image1.setImageResource(myPics[rando]);
image1.setId(myPics[rando]);
//this is from a -z
OnClickListener myCommoClickListner = new OnClickListener(){
#Override
public void onClick(View arg0) {
Log.i(TAG,"arg0.getId() = " + arg0.getId());
if(arg0.getId()==R.drawable.a){
Log.i(TAG,"arg0.getId()="+arg0.getId());
generatedString=generatedString+"a"; //[PLACEE RESPACTIVE CHARACTEER HERE]
text.setText(generatedString);
((ImageButton) arg0).setImageResource(R.drawable.changea);
if (!timeHasStarted) {
countDownTimer.start();
timeHasStarted = true;
}
else{
if(arg0.getId()==R.drawable.z){
Log.i(TAG,"arg0.getId() = " + arg0.getId());
generatedString = generatedString+"z";
text.setText(generatedString);
((ImageButton) arg0).setImageResource(R.drawable.changez);
if (!timeHasStarted) {
countDownTimer.start();
timeHasStarted = true;
}
}
}
};
image1.setOnClickListener(myCommoClickListner);
image2.setOnClickListener(myCommoClickListner);
image3.setOnClickListener(myCommoClickListner);
image4.setOnClickListener(myCommoClickListner);
image5.setOnClickListener(myCommoClickListner);
image6.setOnClickListener(myCommoClickListner);
image7.setOnClickListener(myCommoClickListner);
image8.setOnClickListener(myCommoClickListner);
image9.setOnClickListener(myCommoClickListner);
image10.setOnClickListener(myCommoClickListner);
image11.setOnClickListener(myCommoClickListner);
image12.setOnClickListener(myCommoClickListner);
image13.setOnClickListener(myCommoClickListner);
image14.setOnClickListener(myCommoClickListner);
image15.setOnClickListener(myCommoClickListner);
image16.setOnClickListener(myCommoClickListner);
}
LOGCAT
it never adds EXCEPT when the value/number in the textview tv2 and tv3
is set from the properties
You will need to put your code in the onClick button or anything that listen to the user's action. After that, you can get your textview's value and do something with it.
From the look of your code, i suggest you to move all of the initializations in the onCreate:
//move this code to onCreate
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);
//also move this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
wordList.setAdapter(adapter);
After that, call your viewWord() in the onClick (or anything you like), example :
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
viewWord(v); //v is your button
}
});
UPDATE
After looking the exception in your logcat, it seems the error occurred because the integer has some whitespaces : "2 " <- a space after 2
Try :
x = Integer.parseInt(tv2.getText().toString().replaceAll("\\s",""));
y = Integer.parseInt(tv3.getText().toString().replaceAll("\\s",""));
The idea is to remove all of the whitespaces before parse it to Integer.
Feel free to comment if you have some questions or if i miss understood you :)
Related
Problem: My app crashed whenever the comment part is removed. But after I had tried to debug the problem seemed to be at line priceTextViews[i].setText(price[i]). Can anyone help to point out the problem? Thank you
TextView[] quantityTextViews;
TextView[] priceTextViews;
int[] unitPrice = {5, 15, 40, 20, 25, 15, 20, 25, 10, 20};
int[] price = new int[10];
int[] quantity = new int[10];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.balance_page);
textView();
Intent intent = getIntent();
String[] quantityOfItemString = intent.getStringArrayExtra (ShoppingPageActivity.EXTRA_MESSAGE);
for (int i = 0; i < quantityTextViews.length; i++) {
quantityTextViews[i].setText(quantityOfItemString[i]);
quantity[i] = Integer.parseInt(quantityOfItemString[i]);
price[i]= unitPrice[i]* quantity[i];
}
/*for (int i = 0; i < priceTextViews.length; i++) {
price[i]= unitPrice[i]* quantity[i];
priceTextViews[i].setText(price[i]);
}*/
}
public void textView() {
quantityTextViews = new TextView[]{
(TextView) findViewById(R.id.textView49),
(TextView) findViewById(R.id.textView48),
(TextView) findViewById(R.id.textView47),
(TextView) findViewById(R.id.textView46),
(TextView) findViewById(R.id.textView45),
(TextView) findViewById(R.id.textView44),
(TextView) findViewById(R.id.textView43),
(TextView) findViewById(R.id.textView42),
(TextView) findViewById(R.id.textView41),
(TextView) findViewById(R.id.textView40),
};
priceTextViews = new TextView[]{
(TextView) findViewById(R.id.textView51),
(TextView) findViewById(R.id.textView52),
(TextView) findViewById(R.id.textView53),
(TextView) findViewById(R.id.textView54),
(TextView) findViewById(R.id.textView55),
(TextView) findViewById(R.id.textView56),
(TextView) findViewById(R.id.textView57),
(TextView) findViewById(R.id.textView58),
(TextView) findViewById(R.id.textView59),
(TextView) findViewById(R.id.textView50),
};
}
setText requires a string. The setText method that does accept an int needs to correspond to a text resource within your application.
priceTextViews[i].setText(Integer.toString(price[i]));
When you're using setText with int argument it automatically use setText(#StringRes int resid) instead of void setText(CharSequence text), so cast your argument to String or similar CharSequence type.
Easiest solution is
priceTextViews[i].setText("" + price[i]);
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;
}
}
});
I have an Array List that i am getting Values from the EditText widget and placing them inside an Array List
I would like to do a Calculation and output the total to a Text View
The Program is Running fine it just crashes when i hit Calculate..
ArrayList<String> score = new ArrayList<String>();
public class MyActivity extends Activity {
EditText T1, T2, T3, T4, T5, T6, T7, T8, T9;
TextView total;
int sum = 0;
Button ButtonClear, ButtonScore;
ArrayList<String> score = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
T1 = (EditText) findViewById(R.id.editText2);
T2 = (EditText) findViewById(R.id.editText3);
T3 = (EditText) findViewById(R.id.editText4);
T4 = (EditText) findViewById(R.id.editText5);
T5 = (EditText) findViewById(R.id.editText6);
T6 = (EditText) findViewById(R.id.editText7);
T7 = (EditText) findViewById(R.id.editText8);
T8 = (EditText) findViewById(R.id.editText9);
T9 = (EditText) findViewById(R.id.editText10);
total = (TextView) findViewById(R.id.textView28);
ButtonScore = (Button) findViewById(R.id.buttonScore);
ButtonClear = (Button) findViewById(R.id.buttonClear);
score.add(T1.getText().toString());
score.add(T2.getText().toString());
score.add(T3.getText().toString());
score.add(T4.getText().toString());
score.add(T5.getText().toString());
score.add(T6.getText().toString());
score.add(T7.getText().toString());
score.add(T8.getText().toString());
score.add(T9.getText().toString());
public void Calculate(View v)
{
for (int i = 0; i < score.size(); i++)
{
sum = Integer.valueOf(score.get(i));
}
total.setText(sum);
}
You are setting integer value to textview therefore it crash,So change
total.setText(sum);
to
total.setText(String.valueOf(sum));
And to calculate sum do like
sum += Integer.valueOf(score.get(i));
I successfully created a dynamic TextView and a Button now when ever the button was clicked the value of a TextView changes.
But the problem is I have a final "submit button" outside a loop which should get the INDIVIDUAL values of each TextView and I cant think of a way how to do it can someone pls give me an approach for this thanks!. pls be nice..
code
Cursor citem= sdb.rawQuery("SELECT * FROM ITEM INNER JOIN CATEGORY ON item.categoryid = category.id where category.categoryname='"+fcat+"'", null);
ScrollView scrollView= new ScrollView(this);
LinearLayout mainLayout= new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
Button border = new Button(this);
border.setId(Integer.parseInt(cuser.getString(cuser.getColumnIndex("id"))));;
border.setText("ORDER");
while (citem.moveToNext())
{
byte[] blob =citem.getBlob(citem.getColumnIndex("itemimage"));
int id = Integer.parseInt(citem.getString(citem.getColumnIndex("id")));
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setTag(id);
int i;
ImageView iv = new ImageView(this);
iv.setId(id);
iv.setImageBitmap(dh.getPhoto(blob));
final TextView txtquantity = new TextView(this);
txtquantity.setId(id);
txtquantity.setText("0");
txtquantity.setTextSize(20);
final TextView txtprice = new TextView(this);
txtprice.setId(id);
txtprice.setText(citem.getString(citem.getColumnIndex("itemprice")));
txtprice.setTextSize(30);
ImageButton btn1 = new ImageButton(this);
btn1.setId(id);
final int id_ = btn1.getId();
btn1.setImageResource(R.drawable.ic_launcher);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int i = 0;
i=Integer.parseInt((String) txtquantity.getText())+1;
txtquantity.setText(String.valueOf(i));
totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())+(Integer.parseInt(txtprice.getText().toString())*1)));
}
});
ImageButton btn2 = new ImageButton(this);
btn2.setId(id);
final int id2_ = btn2.getId();
btn2.setImageResource(R.drawable.ic_launcher);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(Integer.parseInt((String)txtquantity.getText())<=0)
{
}
else
{
int i=0;
i= Integer.parseInt((String) txtquantity.getText())-1;
txtquantity.setText(String.valueOf(i));
totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())-(Integer.parseInt(txtprice.getText().toString())*1)));
}
}
});
linearLayout.addView(iv);
linearLayout.addView(txtprice);
linearLayout.addView(btn1);
linearLayout.addView(txtquantity);
linearLayout.addView(btn2);
mainLayout.addView(linearLayout);
}
mainLayout.addView(totalprice);
mainLayout.addView(border);
scrollView.addView(mainLayout);
setContentView(scrollView);
Not quite sure what problem you're having? If it's just reading all the TextViews you created in the loop, then you should just keep a list, and send it off for processing when you submit...
List<TextView> tv_list = new ArrayList<TextView>();
while(...){
//In loop..add your tv's
TextView some_tv = new TextView()
tv_list.add(some_tv);
...
}
//In the submit, send them off for processing...
private void process_tvs(List<TextView> tv_list){
for(TextView tv:tv_list){
//Assuming your tv's have numbers...
int val = Integer.valueOf(tv.getText().toString());
//do something....
}
}
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);