why method inside oncreate event doesn't work? - android

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

Related

How to pass the class name reference to edit text which is inside static method in android

I have a static method and inside that method I am creating a edit text dynamically.
Here is the code,
public static void done() {
EditText[] editText = new EditText[dynamiclen];
for (int n = 0; n < dynamiclen; n++) {
editText[n] = new EditText(clasname.this);
......
}
When I use this code, I am getting error in classname.this saying it can't get a reference. I even tried changing to clasname.class.getName().this but still I am not able to resolve it. Any help would be great
new EditText() requires Context not class name
so you can modify your method like below
public static void done(Context context) {
EditText[] editText = new EditText[dynamiclen];
for (int n = 0; n < dynamiclen; n++) {
editText[n] = new EditText(context);
......
}
Just do this:
Calling:
done(this);
method definition:
public static void done(Context c) {
//...
}

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;
}
}
}

Error ArrayList when get null string from EditText

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

onClickListener not triggering

I'm doing a rather round about way of making a solitaire app. What's happening right now is that I'm trying to click on a button that is supposed to deal cards from the deck to the state that you can play cards from. However, the listener is never triggering.
I put a system.out.println statement in to test to see if it ever enters the code block with the listener, and it does not.
public class SolitaireGame extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
buttonSound = MediaPlayer.create(SolitaireGame.this, R.raw.button_click);
//instance variables
theDeck = new Deck();
botCardStacks = new ArrayList<BotCardStack>(7);
aceStacks = new ArrayList<AceCardStack>(4);
playableCards = new CardStack(52);
deckButton = (Button) this.findViewById(R.id.deckButton);
deckButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
dealCardsToPlayableStack();
}
});
As requested, here is my add cards to playable stack method
public void dealCardsToPlayableStack() {
Stack<Card> tempStack = new Stack<Card>();
int i = 0;
Card temp, temp1, temp2, temp3;
if(theDeck.getValueOfNext() == 0) {
while(!playableCards.isEmpty()) {
temp = playableCards.popCard();
temp.setVisible(false);
tempStack.push(temp);
}
while(!tempStack.isEmpty()) {
theDeck.addCard(tempStack.pop());
}
}
else if(theDeck.getValueOfNext() >= 3) {
temp1 = theDeck.popCard();
temp1.setVisible(true);
temp2 = theDeck.popCard();
temp2.setVisible(true);
temp3 = theDeck.popCard();
temp3.setVisible(true);
playableCards.addCard(temp3);
playableCards.addCard(temp2);
playableCards.addCard(temp1);
}
else if(theDeck.getValueOfNext() == 2) {
temp1 = theDeck.popCard();
temp1.setVisible(true);
temp2 = theDeck.popCard();
temp2.setVisible(true);
playableCards.addCard(temp2);
playableCards.addCard(temp1);
}
else if(theDeck.getValueOfNext() == 1) {
temp1 = theDeck.popCard();
temp1.setVisible(true);
playableCards.addCard(temp1);
}
}
First you should use buttonSound.prepare() before buttonSound.start() (I prefer using SoudPool Class instead of Mediaplayer for sound effects as it is less costly in memory). Secondly, can you share the dealCardsToPlayableStack() method. You can put it outside the onClick() method (in onCreate()) and if you have the same behaviour, then the problem is in this method, not the OnClick() method.

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