removing item from linikedList in android - java

I'm basically a beginner android dev and am struggling to remove items from the linkedList i am using. The function i can't get to work is "deleteLast()", any help wold be appreciated. I am also probably quite a bit of fixing that could be done but the main issue is the as mentioned problem.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText problemView;
private String problem;
private TextView answerView;
int cursorPosition = 0;
List<String> operationsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialising views
problemView = (EditText) findViewById(R.id.problemView);
answerView = (TextView) findViewById(R.id.answerView);
//prevents an underline appearing in the editText
problemView.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS|InputType.TYPE_NULL);
//initialise the the linkedList for the data structure
operationsList = new LinkedList<>();
problem = "";
//create numerical buttons
Button button0 = (Button) findViewById(R.id.button0);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
//create symbolic buttons
Button buttonDivide = (Button) findViewById(R.id.buttonDivide);
Button buttonMultiply = (Button) findViewById(R.id.buttonMultiply);
Button buttonAddition = (Button) findViewById(R.id.buttonAddition);
Button buttonSubtract = (Button) findViewById(R.id.buttonSubtract);
Button buttonPoint = (Button) findViewById(R.id.buttonPoint);
Button buttonScientificNotation = (Button) findViewById(R.id.buttonScientificNotation);
Button buttonAnswer = (Button) findViewById(R.id.buttonAnswer);
//create non-displaying buttons
Button buttonClearAll = (Button) findViewById(R.id.buttonClearAll);
Button buttonDelete = (Button) findViewById(R.id.buttonDelete);
Button buttonEquals = (Button) findViewById(R.id.buttonEquals);
//onClickListener for showable buttons, numbers, signs etc.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Button b = (Button) view;
String value = b.getText().toString();
operationsList.add(problemView.getSelectionStart(), value);
// problem += b.getText().toString();
cursorPosition = problemView.getSelectionStart() + 1;
displayArray();
}
};
// //onClickListener for scientific notation button since need to advance cursor more
// View.OnClickListener listenerScientific = new View.OnClickListener() {
// #Override
// public void onClick(View view) {
// Button b = (Button) view;
// String value = b.getText().toString();
//
// operationsList.add(problemView.getSelectionStart(), value);
//// problem += b.getText().toString();
// cursorPosition = problemView.getSelectionStart() + 1;
// displayArray();
// }
// };
button0.setOnClickListener(listener);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
button5.setOnClickListener(listener);
button6.setOnClickListener(listener);
button7.setOnClickListener(listener);
button8.setOnClickListener(listener);
button9.setOnClickListener(listener);
buttonDivide.setOnClickListener(listener);
buttonMultiply.setOnClickListener(listener);
buttonAddition.setOnClickListener(listener);
buttonSubtract.setOnClickListener(listener);
buttonPoint.setOnClickListener(listener);
buttonScientificNotation.setOnClickListener(listener);
buttonAnswer.setOnClickListener(listener);
View.OnClickListener nonListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getId() == R.id.buttonClearAll){
clearAll();
displayArray();
}
if (view.getId() == R.id.buttonDelete){
deleteLast();
}
if (view.getId() == R.id.buttonEquals){
}
}
};
buttonClearAll.setOnClickListener(nonListener);
buttonDelete.setOnClickListener(nonListener);
buttonEquals.setOnClickListener(nonListener);
}
public void displayArray(){
Iterator<String> iterator = operationsList.listIterator();
String temp = "";
while (iterator.hasNext()){
temp += iterator.next();
}
problemView.setText(temp);
problemView.setSelection(cursorPosition);
}
// public boolean delete(){
// stringProblemArray[spaceUsed-1] = null;
// spaceUsed -= 1;
// problemView.setSelection(problemView.getSelectionStart() - 1);
// return true;
// }
//
public void clearAll(){
operationsList.clear();
cursorPosition = 0;
}
public void deleteLast(){
Iterator<String> iterator = operationsList.listIterator();
int index = 0;
while (iterator.hasNext()){
if (index == cursorPosition-1){
iterator.remove();
}else {
iterator.next();
}
}
displayArray();
// if (iterator.hasNext()){
// for (int i =0; i == cursorPosition; i++){
// iterator.next();
// }
// iterator.remove();
// }
// for(Iterator<String> iter = operationsList.iterator(); iter.hasNext();) {
// String data = iter.next();
// if ( == rec1) {
// iter.remove();
// }
// }
}
}

LinkedList already has removeLast() method, no need to write yours, just write
operationsList.removeLast()
that`s all.
Also if you dont want to cast your List to LinkedList (even if you are actually using LinkedList) you can delete last element using index
operationsList.remove(operationsList.size()-1);

Related

How to get text from button to edit text?

this code work very well but now when question is CAR button1 = A button2 = R button3 = C when I click button3 button1 = INVISIBLE edit text = A and when I click button2 edit text = A I want when click on button edit text get text from button and check text in edit text
requires: edit text get char form button, add this code to function , remove last index from array
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
LinearLayout linearlayout;
Button button;
EditText txt;
String Array[]= {"car","blue","red","fox"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout = findViewById(R.id.linearlayout);
txt = findViewById(R.id.txt);
int random = (int) (Math.random() * Array.clone().length);
StringBuilder word = new StringBuilder(Array[random]);
for (int i = 0; i < Array[random].length(); i++) {
char id = 'b';
button = new Button(this);
button.setId(id + i);
linearlayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setVisibility(View.INVISIBLE);
txt.setText(txt.getText().toString() + button.getText());
}
});
}
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
LinearLayout linearlayout;
//Button button; //TODO: is local var!
EditText txt; //Global var
String Array[]= {"car","blue","red","fox"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout = findViewById(R.id.linearlayout);
txt = findViewById(R.id.txt);
int random = (int) (Math.random() * Array.clone().length);
StringBuilder word = new StringBuilder(Array[random]);
for (int i = 0; i < Array[random].length(); i++) {
char id = 'b';
final Button button = new Button(this); //local var
button.setId(id + i);
linearlayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//the problem was here (var button)
button.setVisibility(View.INVISIBLE);
txt.setText(txt.getText().toString() + button.getText());
}
});
}
}
}
Not sure of the questions, but the one thing I did find to be an issue with the code.
txt.setText(txt.getText().toString() + button.getText());
The bolded part doesn't returns a char sequence. Should be the same thing as the edit text.
button.getText().toString();

onClick view is already defined

I am trying to work with two buttons on one screen and I want to have two different actions occur when each button is clicked. This is my code and I am getting the error message onClick view is already defined on the method titles of both onClick methods. Any help is greatly appreciated.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private Button button3;
private Button button2;
private EditText editText;
private EditText editText9;
private EditText editText10;
private EditText editText11;
private EditText editText12;
private EditText editText13;
private EditText editText14;
private TextView textView;
private TextView textView22;
private View view;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
Intent intent = getIntent();
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
editText = (EditText) findViewById(R.id.editText);
editText9 = (EditText) findViewById(R.id.editText9);
editText10 = (EditText) findViewById(R.id.editText10);
editText11 = (EditText) findViewById(R.id.editText11);
editText12 = (EditText) findViewById(R.id.editText12);
editText13 = (EditText) findViewById(R.id.editText13);
editText14 = (EditText) findViewById(R.id.editText14);
textView = (TextView) findViewById(R.id.textView);
textView22 = (TextView) findViewById(R.id.textView22);
}
public void onClick (View view) {
this.view = view;
if (view.getId() == R.id.button3) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
MainActivity2.this.startActivity(intent);
}
}
public void onClick (View view){
if (view.getId() == R.id.button2) {
String value8 = editText.getText().toString();
String value9 = editText9.getText().toString();
String value10 = editText10.getText().toString();
String value11 = editText11.getText().toString();
String value12 = editText12.getText().toString();
String value13 = editText13.getText().toString();
String value14 = editText14.getText().toString();
}
}
}
You have two onClick methods with the same parameters so it doesn't know which to use. Combine the two and it should work.

mapping text to buttons from within a method

I'm in the very early stages of developing this android app, and I'm trying to setText to buttons from within a method. Doing it not in a method is fine, but I want to call the method over and over. However when I try to create the method to do this, I get a "Non-static method findViewById(int) cannot be referenced from a static context" error.
eg: Button button = (Button) findViewById(R.id.button);
button.setText(options[0]);
The findViewById will be red highlighted
package com.example.lp1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private Button button;
private Button button2;
private Button button3;
private Button button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int score = 0;
int count = 3;
static int getQuestion(){
ArrayList set = addingThree();
final int a = (int) set.get(0);
final int b = (int) set.get(1);
final int c = (int) set.get(2);
final int d = (int) set.get(3);
final int ans = (int) set.get(4);
String qText = (String) set.get(5);
String[] options = new String[] {Integer.toString(a),
Integer.toString(b),Integer.toString(c),Integer.toString(d)};
Button button = (Button) findViewById(R.id.button);
button.setText(options[0]);
Button button2 = (Button) findViewById(R.id.button2);
button2.setText(options[1]);
Button button3 = (Button) findViewById(R.id.button3);
button3.setText(options[2]);
Button button4 = (Button) findViewById(R.id.button4);
button4.setText(options[3]);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(qText);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
static ArrayList addingThree(){
Random rand = new Random();
int numa = rand.nextInt(99);
int numb = rand.nextInt(99);
int numc = rand.nextInt(99);
int ans = numa + numb + numc;
int fake1 = rand.nextInt(200) + 50;
int fake2 = rand.nextInt(200) +50;
int fake3 = rand.nextInt(200) +50;
int[ ] options = {fake1, fake2, fake3, ans};
String stringQuestion = ("What is the sum of " + numa + ", " + numb + ",
and " + numc + "? ");
Arrays.sort(options);
ArrayList parcel = new ArrayList();
parcel.add(options[0]);
parcel.add(options[1]);
parcel.add(options[2]);
parcel.add(options[3]);
parcel.add(ans);
parcel.add(stringQuestion);
return parcel;
}
}
Remove the
static
from your functions.
Also remove the functions from the onCreate callback, they don't belong there.
When you're invoking the functions in onCreate, the functions/methods cannot be static
If you want to use findViewById() in static method then make your rootview Global variable

How can I convert a string to an int?

There probably is a really simple answer to this. My goal is to divide the the inputed information x and y and put that into z.
Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public int x = Integer.parseInt("");
public int y = Integer.parseInt("");
public int z = (y/x);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button
Button btn = (Button) findViewById(R.id.button);
//EditText
EditText nop = (EditText) findViewById(R.id.editText);
EditText cob = (EditText) findViewById(R.id.editText2);
x = Integer.parseInt(nop);
y = Integer.parseInt(cob);
final TextView tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(z);
}
});
}
}
Declare z inside your onclick function. Or assign it a value inside your function.
Also nop and cob are editText's get the data in it, use getText().
Because Integer.parseInt() accepts only a String but not an editText, so you must use getText() to get the String value in it.
x = Integer.parseInt(nop.getText().toString());
y = Integer.parseInt(cob.getText().toString());
final TextView tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int z = x / y;
tv.setText(z);
}
});
Also z is declared globally and assigned nothing. But it needs to be assigned a value when ever you click the button, so that your editText changes every time you click the button.
Convert first as a string from Edittext then make it as Integer.
String Val1 = nop.getText().toString();
String Val2 =cob.getText().toString();
x = Integer.parseInt(val1);
y = Integer.parseInt(val2);

OnClick listener implementation not working

I'm using onClick() method by implementing OnClickListener class. Using below code but it does not work! There are no errors but it doesn't work. When I click on the button nothing happens. What is wrong?
package com.behnam.PhoneCh.main;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
EditText mainEt;
TextView mainTv;
Button mainBtn;
Button mainBtn2;
String vaje;
Javab javab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
vaje = new String();
javab = new Javab();
}
#Override
public void onClick(View aaaaa) {
switch (aaaaa.getId()) {
case R.id. Log.i("mylog", "start button1");
vaje = mainEt.getText().toString();
mainTv.setText(javab.startJ(vaje));
break;
case R.id.button2:
int c = 3;
break;
}
}
}
You never set your listener on your Buttons
mainBtn.setOnClickListener(this);
You will need to add this for all of the Buttons that you want to use the onClick().
See this answer for different ways of handling onClick()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
mainBtn.setOnClickListener(this);
mainBtn2.setOnClickListener(this);
vaje = new String();
javab = new Javab();
}
#Override
public void onClick(View aaaaa) {
switch (aaaaa.getId()) {
case R.id.button1;
Log.i("mylog", "start button1");
vaje = mainEt.getText().toString();
mainTv.setText(javab.startJ(vaje));
break;
case R.id.button2:
int c = 3;
break;
}
}
Change your onCreate and onClick with the above code.
You need to change your code to :
public class MainActivity extends Activity implements OnClickListener {
EditText mainEt;
TextView mainTv;
Button mainBtn;
Button mainBtn2;
String vaje;
Javab javab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
mainBtn.setOnClickListener(this);
mainBtn2 .setOnClickListener(this);
vaje = new String();
javab = new Javab();
}
// And then you can use
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// Dome some stuff on click of button 1
break;
case R.id.button2:
// Dome some stuff on click of button 2
break;
}
}
}
Try this instead:
Button btMain = (Button)findViewById(R.id.button1);
btMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doStuff();
}
});
This is the best way to start understanding how the listeners work.

Categories