I am trying to generate a multiplication table,where the 1st EditText takes the actual number while 2nd EditText takes the actual range of multiplication table. When I run the project , the result is only number * range..
can anyone help me in the loop or code below mentioned Or,any alternatives to display the table in GridLayout or TableLayout rather than TextView.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiplication_table);
number = (EditText)findViewById(R.id.numberTable);
range = (EditText)findViewById(R.id.numberRange);
click = (Button)findViewById(R.id.click);
result = (TextView)findViewById(R.id.display);
final String x = range.getText().toString();
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int a = Integer.parseInt(number.getText().toString());
int b = Integer.parseInt(range.getText().toString());
for(int i = 1 ; i <= 10; i++)
{
for(int j = 1 ; j <= 10; j++)
{
int res = a * b;
result. setText(a +" * " + b + " = " + res);
}
}
return;
}
});
}
}
you are calling setText() for every row, but setText() will reset the text of the TextView, you might want to use append() instead
result.setText("");
int a = Integer.parseInt(number.getText().toString());
int b = Integer.parseInt(range.getText().toString());
for(int i = 1 ; i <= b; i++){
int res = a * i;
result.append(a +" * " + i + " = " + res + "\n");
}
or maybe use StringBuilder
int a = Integer.parseInt(number.getText().toString());
int b = Integer.parseInt(range.getText().toString());
StringBuilder builder = new StringBuilder();
for(int i = 1 ; i <= b; i++){
int res = a * i;
builder.append(a +" * " + i + " = " + res + "\n");
}
result.setText(builder.toString())
Related
Please have a look at this code and at the bottom is the question. Thanks for your help
int i,fact=1;
String value = edtLCM.getText().toString();
for (i = Integer.parseInt(value); i >= 1; i--) {
fact = fact * i;
if (i > 1) {
String one = i + " x ";
System.out.print(i + " x ");
} else {
System.out.print(i);
String two = String.valueOf(i);
}
}
System.out.println(" = " + fact);
LCMResult.setText("");
I want to set the textview of all the 3 "System.out.println()" in one line. The desired result would be like this(if a user input 4 in the edtLCM): 4x3x2x1 = 24
Use a StringBuilder to put the new strings together instead of using system out.
int i,fact=1;
String value = edtLCM.getText().toString();
StringBuilder sb = new StringBuilder(); // find a better name
for (i = Integer.parseInt(value); i >= 1; i--) {
fact = fact * i;
if (i > 1) {
String one = i + " x ";
// System.out.print(i + " x ");
sb.append(i).append(" x ");
} else {
//System.out.print(i);
sb.append(i);
String two = String.valueOf(i);
}
}
//System.out.println(" = " + fact);
sb.append(" = ").append(fact);
String result = sb.toString(); // will be 4 x 3 x 2 x 1 = 24
LCMResult.setText("");
I have the list of images in horizontal LinearLayout If I click particular image the same image will appear on the above of single image view.
How can I get the position of Images in Image View.
CODE
JSONArray multipleimage = alertObj.getJSONArray(PRODUCT_IMAGES);
/*JSONObject singleimage = multipleimage.getJSONObject(Integer.parseInt("original_res"));
String singleimg = productpath + alertObj.getString("seller_id") + String.valueOf(singleimage);
firstimages=(ImageView)
findViewById(R.id.singleimage);
YelloPage.imageLoader.displayImage(singleimg,firstimages,options);*/
horizontalimage=(LinearLayout) findViewById(R.id.linearimage);
if(multipleimage.length()>0)
{
for (int j = 0; j < multipleimage.length(); j++) {
JSONObject pimages = multipleimage.getJSONObject(j);
JSONObject oneimage = multipleimage.getJSONObject(0);
ii = new ImageView(singleshooppingcart.this);
multipleimages = (ImageView) findViewById(R.id.singleimage);
ii.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayout.LayoutParams image = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
image.width = 150;
image.height = 150;
image.setMargins(5, 0, 0, 0);
String multimgs = pimages.getString("original_res");
String oneimg = oneimage.getString("original_res");
String[] img2 = multimgs.split("\\.");
String imagone = productpath + alertObj.getString("seller_id") + '/' + img2[0] + '(' + '2' + '0' + '0' + ')' + '.' + img2[1];
String singleiamges = productpath + alertObj.getString("seller_id") + '/' + oneimg;
YelloPage.imageLoader.displayImage(imagone, ii, options);
YelloPage.imageLoader.displayImage(singleiamges, multipleimages, options);
ii.setLayoutParams(image);
horizontalimage.addView(ii);
ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
How to solve this problem Thanks in Advance.
You can use many approaches for this-
Simplest approach is using ViewGroup#indexOfChild(View child) method. This will simply return what you want.
Or you can use View#setTag(Object) and View#getTag(), specially if you want to get some thing more than mere index in onClick.
You can also use View#setTag(int, Object) and View#getTag(int), if you want to set more than one tag per view.
Hope below example code for first approach helps-
ii.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
// the parameter view here is your imageview, which is clicked.
// horizontalimage is your LinearLayout, which is a ViewGroup
int indexOfImage = horizontalimage.indexOfChild(view);
// do what you want to do with index
}
});
Not tested..try like this...
JSONArray multipleimage = alertObj.getJSONArray(PRODUCT_IMAGES);
/*JSONObject singleimage = multipleimage.getJSONObject(Integer.parseInt("original_res"));
String singleimg = productpath + alertObj.getString("seller_id") + String.valueOf(singleimage);
firstimages = (ImageView)findViewById(R.id.singleimage);
YelloPage.imageLoader.displayImage(singleimg,firstimages,options);*/
horizontalimage = (LinearLayout)findViewById(R.id.linearimage);
if(multipleimage.length()>0)
{
for (int j = 0; j < multipleimage.length(); j++) {
JSONObject pimages = multipleimage.getJSONObject(j);
JSONObject oneimage = multipleimage.getJSONObject(0);
ii = new ImageView(singleshooppingcart.this);
multipleimages = (ImageView) findViewById(R.id.singleimage);
ii.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayout.LayoutParams image = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
image.width = 150;
image.height = 150;
image.setMargins(5, 0, 0, 0);
String multimgs = pimages.getString("original_res");
String oneimg = oneimage.getString("original_res");
String[] img2 = multimgs.split("\\.");
String imagone = productpath + alertObj.getString("seller_id") + '/' + img2[0] + '(' + '2' + '0' + '0' + ')' + '.' + img2[1];
String singleiamges = productpath + alertObj.getString("seller_id") + '/' + oneimg;
YelloPage.imageLoader.displayImage(imagone, ii, options);
YelloPage.imageLoader.displayImage(singleiamges, multipleimages, options);
ii.setLayoutParams(image);
horizontalimage.addView(ii);
ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
multipleimages.setImageURI (singleiamges);
}
});
}
}
want to use the the value of variable b in spinner selection. See the code .
button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View view) {
String name = editText.getText().toString();
if (yes.isChecked()) {
int age = Integer.parseInt(ageText.getText().toString());
int l;
l = 80 ;
int y ;
y = l - age ;
c = y - 7;
final int z;
b = 2016 + c;
editText3.setText((name + " you might die in the year ")+ b + "");
} else {
if (no.isChecked()) {
int age = Integer.parseInt(ageText.getText().toString());
int c;
int l;
l = 80;
int y;
y = l - age;
c = y + 10;
b = 2016 + c;
editText3.setText(name + (" you might die in year ") + b + "");}}} // this is button code //
now comes the spinner code .
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
final List<String> list = new ArrayList<String>();
list.add("none,i am healthy");
list.add("Diabetes");
list.add("cancer");
list.add("HIV/AIDS");
list.add("Tuberculosis");
list.add("Coronary Artery");
list.add("respiratory disease");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item, list);
String disease = spinner1.getSelectedItem().toString();
spinner1.setAdapter(dataAdapter);
if (disease.equals("Diabetes")) {
int x = b-5;
int j = 2016+ x ;
String name = editText.getText().toString();
editText3.setText(name + (" you might die in year ") + j + ""); }
else if (disease.equals("none,i am healthy")) {
String name = editText.getText().toString();
int x = b + 7 ;
int j = 2016 + x ;
editText3.setText(name + ( "you might die in year ") + j + "");
}
};});
};};
this app is executing without an error yet it is not giving me the desired output . How do i use the value of b in spinner ( i have already defined it global)
i wrote a code for calculating some weights. they are integer weights.
and i need to save them in every time the button is clicked.
please help me. i cant see why the compiler gives me an error when i try to push the button for the second time. here is my complete code:
public class TrainingActivity extends Activity {
private EditText etIn1, etIn2, etDesired;
private TextView prevInput;
int W[][] = new int[2][];
int X[][] = new int[30][];
int w0=0, w1=0, w2=0, p=1, sum=0, clicks=0;
private Button nxtData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.training_activity);
View backgroundImage = findViewById(R.id.background);
Drawable background = backgroundImage.getBackground();
background.setAlpha(40);
etIn1= (EditText) findViewById(R.id.etInput1);
etIn2 = (EditText) findViewById(R.id.etInput2);
etDesired = (EditText) findViewById(R.id.etDesired);
prevInput = (TextView) findViewById(R.id.prevInput);
nxtData = (Button) findViewById(R.id.nextData);
nxtData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int sum = 0;
++clicks;
int intetIn1 = Integer.parseInt(etIn1.getText().toString());
int intetIn2 = Integer.parseInt(etIn2.getText().toString());
int intetDesired = Integer.parseInt(etDesired.getText().toString());
X[clicks-1] = new int[] {intetIn1, intetIn2, 1};
prevInput.setText("Last Inputs: (" + intetIn1 + ", " + intetIn2 +
", " + intetDesired + ")");
if(clicks == 1) {
if(intetDesired == 1) {
W[0] = new int[] {intetIn1, intetIn2, 1};
W[1] = W[0];
} else if(intetDesired == (-1)){
W[0] = new int[] {-intetIn1, -intetIn2, -1};
W[1] = W[0];
}
} else if(clicks > 1) {
for(int i=0; i<3; i++){
sum = sum + W[clicks-1][i] * X[clicks-1][i];
} if(sum>0 && intetDesired==1) {
W[clicks] = W[clicks-1];
} else if(sum<0 && intetDesired==(-1)) {
W[clicks] = W[clicks-1];
} else if(sum<=0 && intetDesired==1) {
for(int i=0; i<3; i++) {
W[clicks][i] = W[clicks-1][i] + X[clicks-1][i];
}
} else if(sum>=0 && intetDesired==(-1)) {
for(int i=0; i<3; i++) {
W[clicks][i] = W[clicks-1][i] - X[clicks-1][i];
}
}
}
etIn1.setText("");
etIn2.setText("");
etDesired.setText("");
}
});
}}
and here is the exception it throws:
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
UPDATEEEEEEEE
i fixed the problem with arrayindexoutofboundexception by changing W[2][] to W[20][]. but in some clicks it gives me this error:
java.lang.NullPointerException
and it's not clear in which clicks. sometimes it's in the second click. or some times it's in fourth click. please help.
W[clicks] = W[clicks - 1];
in above line, you have get error because you have only define size of the array
int W[][] = new int[2][];
so it assigned W[0][] and W[1][] only
When click on second time variable clicks value is 2 then compiler gives ArrayIndexOutOfBoundException
EDITED.............................................................
you have got null value because of your bad logic and not proper way to build two dimensional array. Pls use debug tool to find the actual problem to implement logic and use two dimensional array like below example in java or android:
List<List<Integer>> triangle = new ArrayList<List<Integer>>();
List<Integer> row1 = new ArrayList<Integer>(1);
row1.add(2);
triangle.add(row1);
List<Integer> row2 = new ArrayList<Integer>(2);
row2.add(3);row2.add(4);
triangle.add(row2);
triangle.add(Arrays.asList(6,5,7));
triangle.add(Arrays.asList(4,1,8,3));
System.out.println("Size = "+ triangle.size());
for (int i=0; i<triangle.size();i++)
System.out.println(triangle.get(i));
I hope you can help me.
I make a game like 4Pics1Word.
I want to load the Level Randomly, I want a loop which generate a Random number from 0 to 10, and then check if the generated number is the first time loaded.
If yes write it in an array and end the loop.
If the number is not the first time loaded, generate a new random number and check it again until it is not used.
For example this is my code (donĀ“t work right):
Boolean usedImageSet = false;
for (int t = 0; t <= usedImages.length; t++) {
if (usedImageSet == false) {
StringBuilder sb = new StringBuilder();
sb.append(currentQuestion);
String used = sb.toString();
if (usedImages[t] != null) {
System.out.println("usedImage" + t
+ " = not Null, it is" + usedImages[t]);
if (usedImages[t].equals(used)) {
System.out.println("String: "
+ used
+ " found it here: [" + t + "]");
currentQuestion = (int) (Math.random() * 10);
}else {
System.out.println("String: "
+ used + " not found");
}
}
if (usedImages[t] == null) {
usedImages[t] = used;
System.out.println("useddImage[" + t + "]: "
+ usedImages[t]);
System.out.println("usedImage" + t + " is Null, change to"
+ usedImages[t]);
usedImageSet = true;
}
}
}
PS:
Thank you all, I think the solution from Oren is the best
// naive implementation
ArrayList<Integer> list = new ArrayList();
for(int i=0; i<10; i++)
{
list.add(i);
}
Collections.shuffle(list);
// output the generated list
for(int i=0; i<10; i++)
{
System.out.print(list.get(i));
}
But how can I save the list if I close the game?
You would probably be much better off creating a List of the numbers you want and then calling Collections.shuffle() on that list.
// naive implementation
ArrayList<Integer> list = new ArrayList();
for(int i=0; i<10; i++)
{
list.add(i);
}
Collections.shuffle(list);
// output the generated list
for(int i=0; i<10; i++)
{
System.out.print(list.get(i));
}
int oldnumber = 5;
int newnumber = new Random().nextInt(10);
while (newnumber == oldnumber){
newnumber = new Random().nextInt(10);
}