ANdroid Studio>> getCount将()

时间:2014-10-26 00:39:49

标签: java android

我有一个简单的问题,这将有助于我理解很多(我希望!)

好吧,我有这段代码:

// 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();
}

我该如何显示这个号码?!

我提前感谢你......

2 个答案:

答案 0 :(得分:3)

此方法将return转到int。因此,你必须在你的课堂上宣布它。假设您想在同一个班级中显示此号码,您可以按照以下步骤操作:

   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
}
}

答案 1 :(得分:0)

要在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());
    }
}

但是,您可能需要查看Android fundamentals以确保全面了解。