bat .t
bat .t

Reputation: 301

ANdroid Studio >> GetCount()

I have a simple question, which will help me to understand a lot ( I hope!)

Well, I have this code:

// Getting contacts Count
    public int getContactsCount() {

    helper = new DBHelper(CheckTable.this);
    SQLiteDatabase db = helper.getReadableDatabase();

    String countQuery = "SELECT  * FROM " + Market.TABLE;

    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

How may I display this number?!

I thank you in advance...

Upvotes: 0

Views: 1824

Answers (2)

Andre Perkins
Andre Perkins

Reputation: 7800

To display the value in a textview:

public class MyActvitiy Extends Activity{
    public void onCreate(Bundle b){
    super.onCreate(b);
    //sets the view to the xml file that contains the textview
    setContentView(R.layout.myActvityLayout);
    //inflates textview from xml
    TextView tv_contactCount = findViewById(R.id.tv_contactCount);
    //creates a new example object
    Example e = new Example();
    //sets the textview to equal the count
    tv_contactCount.setText(e.getContactsCount());
    }
}

However, you may want to go over Android fundamentals to ensure you get a comprehensive understanding.

Upvotes: 0

Payam
Payam

Reputation: 487

This method is going to return you an int. Hence you have to declare it in your class. Assuming that you want to display this number in the same class you can simply follow this:

   public class Example{

  // Getting contacts Count
    public int getContactsCount() {

    helper = new DBHelper(CheckTable.this);
    SQLiteDatabase db = helper.getReadableDatabase();

    String countQuery = "SELECT  * FROM " + Market.TABLE;

    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}


 public static void main(String[] args) { //main method
    int a= getContactsCount(); //calling the method
    System.out.println(a);  //displaying the integer
}
}

Upvotes: 3

Related Questions