Error ArrayList when get null string from EditText - android

When edittext is null and press add button my code crash...I try to copy integers from edittext to an Array...How i can fix it this error? i have set edittext
android:inputType="numberDecimal"
Can i put try...catch?
public static ArrayList<Integer> pulseslist = new ArrayList<Integer>();
public int pulses1[]=null;
private OnClickListener btnAddListener = new OnClickListener() {
public void onClick(View v) {
String ag=editIRpulse1.getText().toString().trim();
if (ag!=null){
int intag= Integer.parseInt(ag);
if(ag.length() > 0){
pulseslist.add(intag);
editIRpulse1.setText(""); // adds text to arraylist and make edittext blank again
}
pulses1 = new int[pulseslist.size()];
for (int i = 0; i < pulseslist.size(); i++) {
pulses1[i] = pulseslist.get(i);
}
}
}
};

try this
public static ArrayList<Integer> pulseslist = new ArrayList<Integer>();
public int pulses1[]=null;
private OnClickListener btnAddListener = new OnClickListener() {
public void onClick(View v) {
String ag=editIRpulse1.getText().toString();
if (ag!=null){
ag.trim(); //Maybe crash for this method put here........................
int intag= Integer.parseInt(ag);
if(ag.length() > 0){
pulseslist.add(intag);
editIRpulse1.setText(""); // adds text to arraylist and make edittext blank again
}
pulses1 = new int[pulseslist.size()];
for (int i = 0; i < pulseslist.size(); i++) {
pulses1[i] = pulseslist.get(i);
}
}
}
};

change this
android:inputType="numberDecimal"
to
android:inputType="number"
Since you want only ints. You also should use a try/catch to catch a NumberFormatException to prevent empty Strings or numbers that are invalid. Just don't do nothing if you catch an exception. You should display a message to the user that they have enetered invalid characters

Related

Using Text Only Once Through Random Method

I am new to Android. I am showing Text in the TextView on Button click Randomly. On 1st Textview the Heading and on 2nd the explaination of that heading. I am able to show the Heading and Explaination Randomly and now I want if the Text is shown once should not be shown again means it will be removed. This is the point where I stuck. I am not able to remove the texts. Any help will be appreciated. I am posting my code here.
MainActivity.java
TextView text_heading,text_explain;
Button click;
Random random;
Integer [] array_heading ,array_explain ;
Integer int_text;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_heading = (TextView) findViewById(R.id.text_heading);
text_explain = (TextView) findViewById(R.id.text_explain);
click = (Button) findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text_heading.setText(array_heading.get(int_text)); //getting error
text_explain(array_explain.get(int_text)); //getting error
array_heading.remove(int_text); //getting error
array_explain.remove(int_text); //getting error
}
});
random = new Random();
array_heading = new Integer []{R.string.source_text1, R.string.source_text2, R.string.source_text3,
R.string.source_text6, R.string.source_text5, R.string.source_text4, R.string.source_text7,
R.string.source_text8, R.string.source_text9};
array_explain = new Integer []{R.string.source_text1_explain, R.string.source_text2_explain,
R.string.source_text3_explain,
R.string.source_text4_explain, R.string.source_text5_explain, R.string.source_text6_explain,
R.string.source_text7_explain,
R.string.source_text8_explain, R.string.source_text9_explain};
ArrayList<Integer> array_headingList = new ArrayList<Integer>(Arrays.asList(array_heading));
ArrayList<Integer>array_explainList = new ArrayList<Integer>(Arrays.asList(array_explain));
int_text = random.nextInt(array_headingList.size() - 1);
}
}
I am not able to remove the texts. Any help will be appreciated. I am
posting my code here.
To clean the content of the TextView you could pass null to setText. E.g
text_heading.setText(null);
If you want to change the content every time you click on the button, you have to move
int_text = random.nextInt(array_heading.length);
your onClick callback,
You should be aware of the fact that next int returns an int between [0, n). array_heading.length -1 is necessary only if you want to exclude R.string.source_text9_explain from the possible texts you want to show. Keep also in mind that if array_heading contains more items than array_explain you could get ArrayIndexOutBoundException
I would keep the strings together in an object:
public class Item {
private final int textId;
private final int textExplanationId;
public class Item(int textId, int textExplanationId){
this.textId = textId;
this.textExplanationId = textExplanationId;
}
public int getTextId(){return textId;}
public int getTextExplanationId(){return textExplanationId;}
}
Then I would store those in an ArrayList:
List<Item> items = new ArrayList<Item>(new Item[]{
new Item(R.string.source_text1, R.string.source_text1_explain),
new Item(R.string.source_text2, R.string.source_text2_explain),
//etc
});
Then I would shuffle that array once:
Collections.shuffle(items);
And read from it in order:
Item current = items.get(currentIndex++);
text_heading.setText(current.getTextId());
text_explain.setText(current.getTextExplanationId());
TextView text_heading,text_explain;
Button click;
Random random;
Integer [] array_heading ,array_explain ;
Integer int_text;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_heading = (TextView) findViewById(R.id.text_heading);
text_explain = (TextView) findViewById(R.id.text_explain);
click = (Button) findViewById(R.id.click);
random = new Random();
array_heading = new Integer []{R.string.source_text1, R.string.source_text2, R.string.source_text3,
R.string.source_text6, R.string.source_text5, R.string.source_text4, R.string.source_text7,
R.string.source_text8, R.string.source_text9};
array_explain = new Integer []{R.string.source_text1_explain, R.string.source_text2_explain,
R.string.source_text3_explain,
R.string.source_text4_explain, R.string.source_text5_explain, R.string.source_text6_explain,
R.string.source_text7_explain,
R.string.source_text8_explain, R.string.source_text9_explain};
ArrayList<Integer> array_headingList = new ArrayList<Integer>(Arrays.asList(array_heading));
ArrayList<Integer> array_explainList = new ArrayList<Integer>(Arrays.asList(array_explain));
int_text = random.nextInt(array_headingList.size() - 1);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text_heading.setText(array_headingList.get(int_text)); //getting error
text_explain.setText(array_explainList.get(int_text)); //getting error
array_headingList.remove(int_text); //getting error
array_explainList.remove(int_text); //getting error
}
});
}
}
You must use ArrayList for it.
ArrayList<Integer> heading = Arrays.asList(array_heading);
ArrayList<Integer> explain = Arrays.asList(array_explain);
Now set text from this arraylists. And when its set once remove it from the arraylist so it cannot be shown again.
Use like this
random = new Random();
array_heading = new Integer []{R.string.source_text1, R.string.source_text2, R.string.source_text3,
R.string.source_text6, R.string.source_text5, R.string.source_text4, R.string.source_text7,
R.string.source_text8, R.string.source_text9};
array_explain = new Integer []{R.string.source_text1_explain, R.string.source_text2_explain,
R.string.source_text3_explain,
R.string.source_text4_explain, R.string.source_text5_explain, R.string.source_text6_explain,
R.string.source_text7_explain,
R.string.source_text8_explain, R.string.source_text9_explain};
ArrayList<Integer> array_headingList = new ArrayList<Integer>(Arrays.asList(array_heading));
ArrayList<Integer> array_explainList = new ArrayList<Integer>(Arrays.asList(array_explain));
int_text = random.nextInt(array_headingList.size());
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text_heading.setText(array_headingList.get(int_text));
text_explain.setText(array_explainList.get(int_text));
array_headingList.remove(int_text);
array_explainList.remove(int_text);
if(array_headingList.size() == 0){
click.setEnabled(false);
Toast.makeText(getApplicationContext(),"All text finished",Toast.LENGTH_SHORT).show();
} else if(array_headingList.size() == 1){
int_text = 0;
} else {
int_text = random.nextInt(array_headingList.size());
}
}
});

Getting the array position of a array button during onClick

I have an array of button of size probably more than 20-30. My simple question is how to get the array index of the button that have been click? For example, i clicked btnDisplay[8] and then the apps will toast "8". As simple as that. but i don't know how to retrieve the index of the arrayed button.
switch (clickedButton.getId())
{
case R.id.Button01:
// do something
break;
case R.id.Button01:
// do something
break;
}
If i use this code, then i have to wrote like 20-30 cases. would there be a better solution?
How i generate button array
public class MainActivity extends Activity {
Button[] btnUpdate;
public void onCreate(Bundle savedInstanceState) {
//SOME CODE HERE
jsonParser = new JSONParser();
jObj = jsonParser.getJSONFromUrl(URL);
btnUpdate = new Button[jObj.length()];
for(int i=0;i<jObj.length();i++)
{
btnUpdate[i] = new Button(getApplicationContext());
btnUpdate[i].setText("Edit");
btnUpdate[i].setHeight(50);
}
Try this way
for (int i = 0; i < jObj.length(); i++) {
btnUpdate[i] = new Button(getApplicationContext());
btnUpdate[i].setText("Edit");
btnUpdate[i].setHeight(50);
btnUpdate[i].setTag(i); //ADD THIS LINE.
}
void onClick(View v) {
int index = (Integer) v.getTag();
Toast.makeText(getApplicationContext(), "BtnClicked"+index, Toast.LENGTH_SHORT).show();
}
Somehow try to use btnDisplay.indexof(); it works in C# I am not sure about Java
Try something like this
void onClick(View v)
{
int index = 0;
for (int i = 0; i < buttonArray.length; i++)
{
if (buttonArray[i].getId() == v.getId())
{
index = i;
Toast.makeText(getApplicationContext(), "BtnClicked"+index, Toast.LENGTH_SHORT).show();
break;
}
}
}

why method inside oncreate event doesn't work?

I have a private method like this
private void acaknomor() {
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i <= 4; i++) {
while (true) {
Integer next = ran.nextInt(4 + 1) + 0;
if (!generated.contains(next)) {
generated.add(next);
break;
}
}
}
int[] arrayAcak = new int[generated.size()];
for (int i = 0; i < generated.size(); i++) {
arrayAcak[i] = generated.get(i);
}
}
i call that method in oncreate and test by button click to show list value with a toast like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acaknomor();
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), String.valueOf(generated), Toast.LENGTH_SHORT).show();
}
});
The Question is why the toast show null when I put it inside a method , instead showing "0,1,2,3,4" ?
I tested my code without a method (put it raw inside oncreate event and it worked...)
Your are declaring List<Integer> generated = new ArrayList<Integer>(); in side the method acaknomor(). So it is not visible for the Toast.
I suggest you to declare it at class level.
private List generated;
Now inside the the acaknomor()
private void acaknomor() {
generated = new ArrayList<Integer>(); // Modify this line
for (int i = 0; i <= 4; i++) {
...
Instead of this:
List<Integer> generated = new ArrayList<Integer>();
Use
generated = new ArrayList<Integer>();
And declare List<Integer> generated for your activity
please have a look at acaknomor method
private void acaknomor() {
List<Integer> generated = new ArrayList<Integer>();....
use generated = new ArrayList<Integer>();
and make List<Integer> generated =null; Global which will help you on this issue

Browse an array of strings in a TextView

I wonder how I could navigate between the strings within an array, using the previous and next buttons, these strings will be displayed in a TextView. Thank you!
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView (R.layout.activity_f3);
setTitleFromActivityLabel (R.id.title_text);
TextView cumulos = (TextView) findViewById(R.id.cumulos);
TextView respostas = (TextView)findViewById(R.id.respostas);
Random randPhrase = new Random();
String[] cum = getResources().getStringArray(R.array.cumulos);
String[] resp = getResources().getStringArray(R.array.resp_cumulos);
String textout = "";
String textresp = "";
for (int i = 0; i < cum.length; i++) {
for (int j = 0; j < resp.length; j++) {
textresp = resp[j];
}
textout = cum[i];
}
cumulos.setText(textout);
respostas.setText(textresp);
}
Declare one int for index starting with 0 then in NextButton do
if(!index > resp.length-1 ) //not greater than array length
{
setText(resp[index++]);
}
else { nextButton.setEnabled(false); nextButton.setClicable(false); } //not clickable anymore
in PreviousButton do
if(!index < 0)
{
setText(resp[index--]);
}
else{
prevButton.setEnabled(false);
prevButton.setClicable(false);
}
Something like this? Mind this code is not tested, might throw exceptions.
It just to give you an idea.
You will need to create a next button and set an onClickListener for your button to navigate through the array. Lets say you also have a previous and next button. Try this:
Button btnNext = (Button) findViewById(R.id.yourNextbutton);
Button btnPrevious = (Button) findViewById(R.id.yourPreviousbutton);
int i = 0;
btnNext.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
respostas.setText(resp[i]);
}
}
});
btnPrevious.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if(i>0){
i-=1;
cumulos.setText(cum[i]);
respostas.setText(resp[i]);
}
}
});

Regarding android Development

I am doing an application in which I have to display the numbers on TextView randomly and automatically with the help of Timer. I am able to get the random Numbers in the log without repeating, but I am not able to print the same on device please help me...
Regards,
Akki
Source:
//RandomNumber.java
public class RandomNumber extends Activity{
static Random randGen = new Random();
int tambolanum,count=0;
private Button previousbutton;
private Button startbutton;
private Button nextbutton;
int bingonum[]=new int[90];
boolean fill;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.numbers);
LinearLayout number=(LinearLayout)findViewById(R.id.numbersview);
final TextView randomnum=(TextView)findViewById(R.id.numberstext);
previousbutton=(Button)findViewById(R.id.previous);
nextbutton=(Button)findViewById(R.id.next);
startbutton=(Button)findViewById(R.id.start);
startbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on click
//--- Initialize the array to the ints 0-90
do{
fill = true;
//Get new random number
tambolanum = randGen.nextInt(90) + 1;
//If the number exists in the array already, don't add it again
for(int i = 0; i < bingonum.length; i++)
{
if(bingonum == tambolanum)
{
fill = false;
}
}
//If the number didn't already exist, put it in the array and move
//To the next position
if(fill == true)
{
bingonum[count] = tambolanum;
count++;
}
} while(count < 90);
for(i=0;i
{
randomnum.setText(Integer.toString(bingonum[i]);
}
}
setText(CharSequence text)
The problem you're having is that you're overwriting your text in every itteration of this loop:
for(i=0;i
{
randomnum.setText(Integer.toString(bingonum[i]);
}
You need to build your string first then set it. Something like:
StringBuilder sb = new StringBuilder();
for(i=0;i /* where's the rest of this for-statement? */
{
sb.append(Integer.toString(bingonum[i]);
}
randomnum.setText(sb.toString());

Categories

Resources