mardi 27 janvier 2015

Android Random string from file after button click

I have some problem with Random. I need choose one line from txt file and pass it into textview after button click (each time a different line). My code do this only one during the first click, but i want to for each button click next line is show. (sorry for my bad English) -_-



public class MainActivity extends Activity {

long seed = System.nanoTime();
String newString1;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


valueA();
Button buttonGenerate = (Button)findViewById(R.id.button1);
final TextView textGenerateNumber = (TextView)findViewById(R.id.textView1);

buttonGenerate.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
valueA();
textGenerateNumber.setText(newString1);
}});
}



public void valueA (){

Scanner s = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> list = new ArrayList<String>();

try {
while (s.hasNextLine() ) {
list.add(s.nextLine());
}
} finally {
s.close();
}

Random r = new Random(seed);

String a = list.get(r.nextInt(list.size()));

newString1 = a.substring(2);

}

}




Aucun commentaire:

Enregistrer un commentaire