早上好,我有问题。
所以我想从下面的方法返回一个多值是我的代码:
public static String getReferee(String referee_id, SQLiteDatabase sqLiteDatabase)
{
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM tbl_referee", null);
String refereeInfo = null;
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
String nameInRecord = cursor.getString(1);
if (nameInRecord.equals(referee_id))
{
String referee_name = cursor.getString(0); // 0 means table column of REFEREE NAME
String referee_game1 = cursor.getString(2); // 2 means table column of REFEREE GAME 1
String referee_game2 = cursor.getString(3); // 3 means table column of REFEREE GAME 2
refereeInfo = referee_name + referee_game1 + referee_game2;
break;
}
}
while (cursor.moveToNext());
}
}
return refereeInfo1,refereeInfo2,refereeInfo3,refereeInfo4;
}
如您所见,返回码仅返回字符串RefereeInfo,其中包含来自我的查询的多个数据。如何返回多个String并将其传递给我的Main Activity。
下面的代码是我的主要活动:
//GET REFEREE PROFILE
private void getRefereeProfile()
{
String memberInfo = SQLiteFunctionUtility.getReferee(referee_id, mDbHelper.getSqliteObjectWithReadable());
if (memberInfo != null)
{
txtview1.setText(returnstatement1);
txtview2.setText(returnstatement2);
txtview3.setText(returnstatement3);
txtview4.setText(returnstatement4);
}
else
{
Toast.makeText(MainActivity.this, "No Referee Found", Toast.LENGTH_LONG).show();
}
我从网上搜索所有内容,因此急需答案。预先谢谢您!
答案 0 :(得分:3)
为了返回多个值,您需要将它们放入一个对象并返回该对象。您可以使用List
或String[]
,但为此目的使用自定义类会更加明确:
public static class Referee
{
private String name;
private String game1;
private String game2;
public Referee(String name, String game1, String game2)
{
this.name = name;
this.game1 = game1;
this.game2 = game2;
}
// Getters...
}
public static Referee getReferee(String referee_id, SQLiteDatabase sqLiteDatabase)
{
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM tbl_referee", null);
String refereeInfo = null;
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
String nameInRecord = cursor.getString(1);
if (nameInRecord.equals(referee_id))
{
String referee_name = cursor.getString(0); // 0 means table column of REFEREE NAME
String referee_game1 = cursor.getString(2); // 2 means table column of REFEREE GAME 1
String referee_game2 = cursor.getString(3); // 3 means table column of REFEREE GAME 2
return new Referee(referee_name, referee_game1, referee_game2);
}
} while (cursor.moveToNext());
}
}
return null;
}
如果您有一个名为getReferee
的方法,则可以很好地表明您还应具有该方法返回的类型Referee
。这样,您的代码就变得不言自明了。
您可以在主要活动中使用这种新类型,如下所示:
private void getRefereeProfile()
{
Referee memberInfo = SQLiteFunctionUtility.getReferee(referee_id, mDbHelper.getSqliteObjectWithReadable());
if (memberInfo != null)
{
txtview1.setText(memberInfo.getName());
txtview2.setText(memberInfo.getGame1());
txtview3.setText(memberInfo.getGame2());
// what is txtView4 for?
}
else
{
Toast.makeText(MainActivity.this, "No Referee Found", Toast.LENGTH_LONG).show();
}
答案 1 :(得分:0)
您没有提供Java语法。您可以做的是创建一个包装器对象,该对象将保存要返回的所有值,并将其作为一个对象返回。
或者,您可以使用ArrayList
或java数组对象。
答案 2 :(得分:0)
像这样创建一个定义所有值的pojo类,并为数据库创建方法。
public User getUser(String email, String password) {
User user = new User();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + Reg_TABLE_NAME + " WHERE email=" + email + " AND password=" + password;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
user.email = (c.getInt(c.getColumnIndex("email")));
user.password = (c.getString(c.getColumnIndex("password")));
c.moveToNext();
}
}
c.close();
db.close();
return user;
}
获取用户数据后,您可以显示电子邮件和密码。
制作一个可以执行所有操作的数据库类。
public class DB extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "myDb";
public static final String Reg_TABLE_NAME = "Register";
public Context mContext;
public DB(Context context) {
super(context, DATABASE_NAME, null, 1);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + Reg_TABLE_NAME + " (id integer primary key, email text, password text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Reg_TABLE_NAME);
onCreate(db);
}
public User insertUser(User user) {
java.util.Date date = new java.util.Date();
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("email", user.getEmail());
contentValues.put("password", user.getPassword());
long insertId = db.insert(Reg_TABLE_NAME, null, contentValues);
db.close();
}
public void deleteComment(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Reg_TABLE_NAME, "id =" + id, null);
db.close();
}
public User getUser(String email, String password) {
User user = new User();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + Reg_TABLE_NAME + " WHERE email=" + email + " AND password=" + password;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
user.email = (c.getInt(c.getColumnIndex("email")));
user.password = (c.getString(c.getColumnIndex("password")));
c.moveToNext();
}
}
c.close();
db.close();
return user;
}
}
然后执行插入,更新,读取,删除等操作,将其插入活动和片段中。
DB db=new DB(this); // if fragment then pass getActivity()
User user=new User();
user.setEmail("sdf");
user.setPassword("sdf");
db.insertUser(user);
答案 3 :(得分:0)
您不能从Java中的方法返回多个值。没有语法可以像您一样返回多个字符串。您可以使用String Builder,Buffer或Array列表来完成此工作。
如果使用数组列表,请执行以下操作:-
ArrayList<String> arr= new ArrayList<String>();
arr.add(refereeInfo1);
arr.add(refereeInfo2);
arr.add(refereeInfo3);
arr.add(refereeInfo4);
然后您可以将该列表返回为:-
return arr;
还将方法的返回类型从String更改为List。
答案 4 :(得分:0)
您可以使用元组-匿名/不相关元素的集合。阅读本文http://www.baeldung.com/java-tuples。 AFAIK Android SDK包含对元组。 Kotlin语言具有成对和三重。如果愿意,可以编写带有N个参数的元组,其中0