Display data from database into textview

后端 未结 3 1084
误落风尘
误落风尘 2020-12-16 07:45

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

相关标签:
3条回答
  • 2020-12-16 08:09

    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());
        }
    };
    
    0 讨论(0)
  • 2020-12-16 08:14

    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;
    
           }
        }
    };
    
    0 讨论(0)
  • 2020-12-16 08:17

    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.

    0 讨论(0)
提交回复
热议问题