Hey i am using the code below to use as a press a button to get new text in to a textview,
But its laggy and doesn’t work half of the times i press it, but the other half of the time it works fine, anyone have any idé?
final Button button = (Button) findViewById(R.id.slumpaovrigt);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Random myRandom5 = new Random();
TextView textovrigt = (TextView) findViewById(R.id.textovrigt);
switch(myRandom5.nextInt() %151) {
case 0:
textovrigt.setText("text 1");
break;
case 1:
textovrigt.setText("text 2");
break;
case 2:
textovrigt.setText("text 3");
break;
case 3:
textovrigt.setText("text 4");
break;
}
}
});
}
}
Also, i use 4 different xml views whit the same code but for different buttons and textviews and also whit different texts, each of the contains around 150 different texts (cases).
Changed the code abit and it got better, however still a bit slow
this is the complete code in the java file
import java.util.Random;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Ovrigtskamt extends Activity {
Button slumpaovrigt;
TextView textovrigt;
Random slumpaovrigt2 = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ovrigttext);
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
slumpaovrigt = (Button) findViewById(R.id.slumpaovrigt);
textovrigt = (TextView)findViewById(R.id.textovrigt);
loadDoc();
}
private void loadDoc(){
String s = "";
for(int x=0;x<=100;x++){
s += "Line: "+String.valueOf(x)+"\n";
}
textovrigt.setMovementMethod(new ScrollingMovementMethod());
textovrigt.setText("Klicka på Slumpa");
slumpaovrigt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch(slumpaovrigt2.nextInt() %4) {
case 0:
case 0:
textovrigt.setText("text 1");
break;
case 1:
textovrigt.setText("text 2");
break;
case 2:
textovrigt.setText("text 3");
break;
case 3:
textovrigt.setText("text 4");
break;
}
}
});
}
}
but as said whit 151 different texts
Update: well you are doing % 151 do you have that many elements in your code? Not in this code example. Use % 4 instead.
Update2: Or
random.nextInt(4).Two optimizations we can see directly, but sounds strange if that would cause that much lag really.