I\'m trying display random data from database into two textviews (TextView1 display NAME, TextView2 display PHONENUMBER) when I click on next button, but I don\'t know how c
Try this :
Button buttonnext = (Button) findViewById(R.id.next);
buttonnext.setOnClickListener(onclickListener);
private OnClickListener onclickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
int randInt = new Random().nextInt(contacts.size()) + 1;
Contact cn=contacts.get(randInt);
name.setText("Id: "+cn.getID()+" ,Name: " + cn.getName() + "
,Phone: " + cn.getPhoneNumber());
}
};
you will need to set setOnClickListener for button to do some action on Button click and use and Counter variable to show next value on button click as:
List<Contact> contacts = db.getAllContacts();
TextView name = (TextView) findViewById(R.id.textView1);
Button buttonnext = (Button) findViewById(R.id.next);
buttonnext.setOnClickListener(onclickListener);
int listcounter=0;
private OnClickListener onclickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
if(listcounter<contacts.size()){
Contact cn=contacts.get(listcounter);
name.setText("");
name.append("Id: "+cn.getID());
name.append(" ,Name: "+cn.getName());
name.append(",Phone: "+cn.getPhoneNumber());
listcounter++;
}
else{
//Reset the Counter here
listcounter = 0;
}
}
};
You need an OnClickListener
in your button. You can declare it in XML using the android:onClick
attribute.
This is explained in a detailed manner in http://developer.android.com/guide/topics/ui/controls/button.html#HandlingEvents.