How to update whole output row on change of given Input? - android

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.

Related

Want to loop through questions and count correct answers

I'd like to show a series of 10 questions and have the user answer, and have the app show whether the answer is correct, and then show the next question.
Right now I can show the first question, check whether it's correct, and then display the second question. I don't know how to get this looping, however, to show all questions. Here is the relevant code:
public class MathTestActivity extends AppCompatActivity {
int i = 0;
int n = 20; /*How many rows this test*/
String[] mathTest = new String[40];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mathtest);
final TextView mathProblem = (TextView) findViewById(R.id.mathProblem);
final EditText mathAnswer = (EditText) findViewById(R.id.mathAnswer);
//Styling for the question text
mathProblem.setTextSize(40);
mathProblem.setTextColor(Color.rgb(0, 0, 0));
//Try to read the problem and answers text file
try {
InputStream is = this.getResources().openRawResource(R.raw.mediummath);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
int n = 20; /*How many rows this test has*/
/*read in file to array*/
for (i = 0; i < n; i=i+2) {
if ((line = reader.readLine()) != null)
mathTest[i] = line;
if ((line = reader.readLine()) != null)
mathTest[i+1] = line;
}
} catch (IOException e) {
e.printStackTrace();
}
mathProblem.setText(mathTest[0]);
Button enterButton = (Button) findViewById(R.id.enterButton);
enterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int correctcount = 0;
i = 0;
String answer = mathAnswer.toString() ;
String correctAnswer = mathTest[i+1];
if (answer.equals(correctAnswer)){
Toast.makeText(MathTestActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT).show();
correctcount++;
i = i + 2;
mathProblem.setText(mathTest[i]);
correctAnswer = mathTest[i+1];
}
else{
Toast.makeText(MathTestActivity.this,
correctAnswer,
Toast.LENGTH_SHORT).show();
i = i + 2;
mathProblem.setText(mathTest[i]);
correctAnswer = mathTest[i+1];
}
}
});
}
}
You can't count the correct answers with a local variable that gets set to 0 each time you click the button. :)
You should move int correctcount = 0; outside of the listener and next to your i and n variables.
You will also want to remove the i = 0; line from inside the button click because that will reset you back to the first question every time you click the button.
Also, since these three lines are duplicated between the if and the else, you can just place them directly after the else.
i = i + 2;
mathProblem.setText(mathTest[i]);
correctAnswer = mathTest[i+1]; // this isn't needed, though
You can try putting your questions in ViewPager if you want to make the questions shown in sliding presentation (http://developer.android.com/training/animation/screen-slide.html).
Or you can put your question inside a ListView if you want to view the question from top to bottom. (http://developer.android.com/guide/topics/ui/layout/listview.html)
EDIT:
Actually you are close to achieve what you want to do.
You should delete i = 0; inside your button onClick() function as it prevents you from going to next questions (3rd, 4th, etc). You might want to consider clearing the mathAnswer EditText when the user submits a question.
Also, it is not necessary to set correctAnswer = mathTest[i+1]; inside your if-else function.
Use arraylist for questions array and answer array.use view pager with two layouts questions and answer layout.if the answer array of position equal to the user answer its correct answer like u can dynamically

How to make invisible images visible?

I am developing a card game. I divide 13 cards to each client using Server,
when I divide 13 cards to 1st player, 9 cards are invisible and remaining 4 are visible.
Now I want when I click this one Image, the remaining 9 cards gets visible?
How to do this?
- the code is this:
String str=" "c,a", "c,k", "c,q", "c,j", "c,10", "c,9", "c,8", "c,7", "c,6", "c,5", "c,4", "c,3", "c,2"";
drawCards(str);
private void drawCards(String drawString) {
String[] separated = msgLog.split("\\,");
for (int i = 2; i < separated.length - 1; i += 2) {
String symbol = separated[i];
String num = separated[i + 1];
String resourceName = symbol + num;
//symbol and number is used for get image from xml file
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
im = (ImageView) findViewById(resID);
Context context = im.getContext();
cardID = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
//9 card invisible
if ( i > 10) {
im.setVisibility(View.GONE);
}
/* elseif(x.getVisibility() == VISIBLE)
{
x.setVisibility(INVISIBLE);
}*/
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter * 5, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(cardID);
im.setOnClickListener(this);
counter = counter + 8;
}
}
public void onClick(View v) {
final String IdAsString = v.getResources().getResourceName(v.getId());
pieceToast = Toast.makeText(getApplicationContext(), idServer, Toast.LENGTH_SHORT);
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
pieceToast.show();
}
});
}
When I click this card how to hide card make visible
You can take the help from below given link:
https://stackoverflow.com/questions/41285814/how-to-make-visible-and-invisible-an-image-by-clicking-a-button-in-android-studi
If you still have question, let me know
You could play around with the visibility and use the methode setVisibility(View.VISIBLE) and setVisibility(View.GONE)

How to dynamically add rows and detect and delete clicked?

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

Set imageview background on button click?

lets say I have a timer activity. Theres an image view that displays a number of 0 - 9(these are viewed as images, NOT text).
eg. the images names are clock_0, clock_1, clock_2...clock_9.
There is a plus and minus button that should change a counter(to pick the image). If current image is clock_0, pressing the plus button will change the view to clock_1 etc. How do I do this efficiently?
EDIT
Heres what I tried to do;
set an int[],
int[] clockResource = new int[10];
clockResource[0] = R.drawable.clock_0;
clockResource[1] = R.drawable.clock_1;
clockResource[2] = R.drawable.clock_2;
clockResource[3] = R.drawable.clock_3;
clockResource[4] = R.drawable.clock_4;
clockResource[5] = R.drawable.clock_5;
clockResource[6] = R.drawable.clock_6;
clockResource[7] = R.drawable.clock_7;
clockResource[8] = R.drawable.clock_8;
clockResource[9] = R.drawable.clock_9;
assign an onclicklistener for the addbutton
case R.id.addMin:
if (addMin.isEnabled()) {
if (mintwo < 9) {
mintwo++;
} else if (mintwo == 9) {
minone++;
mintwo = 0;
}
min2.setBackgroundResource(clockResource[mintwo]);
min1.setBackgroundResource(clockResource[minone]);
}
Since they're ImageViews, use setImageDrawable or setImageResource instead.
Related SO-question.
case R.id.addMin:
if (addMin.isEnabled()) {
if (mintwo < 9) {
mintwo++;
} else if (mintwo == 9) {
minone++;
mintwo = 0;
}
min2.setImageResource(clockResource[mintwo]); //updated
min1.setImageResource(clockResource[minone]); //updated
}

Dynamic Button Onclick Listener

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...

Categories

Resources