'how can add integer from edittext to array In android?
I am using java to code a small program. this program will take data from the keyboard and check the parity of that sequence and this is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edso = findViewById(R.id.so);
btnchan = findViewById(R.id.btnchan);
btnle = findViewById(R.id.btnle);
tvkq=findViewById(R.id.tvkq);
String n=edso.getText().toString();
List<Integer> arr = new ArrayList<Integer>();
arr.add(Integer.parseInt(n));
Integer[] array = arr.toArray(new Integer[5]);
btnchan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i=0;i< array.length;i++){
if (array[i]%2==0){
tvkq.setText("chăn"+array[i]);
}
}
}
});
btnle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i=0;i< array.length;i++){
if (array[i]%2!=0){
tvkq.setText("lẽ"+array[i]);
}
}
}
});
}
help me
i want to use data from edittext to check and display it in textview
Solution 1:[1]
int num = Integer.parseInt(mEditText.getText().toString());
arr.add(num);
or
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int num = Integer.parseInt(s.toString());
arr.add(num);
}
@Override
public void afterTextChanged(Editable s) {
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AhmetAcikalin |
