我已经建了3个班级,1) tableStudents 2) DB 3) MainActivity
到目前为止,代码已成功创建数据库,并在给定的集合编码测试数据的情况下插入数据库。但是我无法弄清楚如何打印这些信息只是为了表明它确实已经成功输入了它。
我在大学的电脑上,因此我无法访问数据库文件
在没有浏览文件的情况下,我尝试使用我编写的方法输出列表来获取数据,但我不确定如何将其作为文本输出。
Fullcode如下:
tableStudents.java
public class tableStudents {
public String name, gender, password, course, modules;
public int age;
//Constructor
public tableStudents()
{
}
//constructor
public tableStudents(String name, String gender, int age, String password, String course, String modules)
{
this.name = name;
this.password = password;
this.gender = gender;
this.age = age;
this.course = course;
this.modules = modules;
}
public static abstract class tableColumns implements BaseColumns
{
public static final String Student_ID= "Student_ID";
public static final String Student_Name= "Student_Name";
public static final String Student_Password = "Student_Password";
public static final String Student_Gender = "gender";
public static final String Student_Age = "age";
public static final String Student_Course = "course";
public static final String Modules = "modules";
public static final String Database = "databasename";
public static final String Table = "tablename";
}
}
DB.java
public class DB extends SQLiteOpenHelper {
public static final int db_version = 1;
public static final String Table = "Students";
public static final String Student_ID = "Student_ID";
public static final String Student_Name = "Student_Name";
public static final String Student_Password = "Student_Password";
public static final String Student_Gender = "gender";
public static final String Student_Age = "age";
public static final String Student_Course = "course";
public static final String Modules = "modules";
public DB(Context context) {
super(context, tableColumns.Database, null, db_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//Create Table
db.execSQL("CREATE TABLE " + Table + "(" +
Student_ID + " INTEGER PRIMARY KEY, " +
Student_Name + " TEXT, " +
Student_Password + " TEXT, " +
Student_Gender + " TEXT, " +
Student_Age + " INTEGER, " +
Student_Course + " TEXT, " +
Modules + " TEXT)");
Log.d("DB", "DB Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Table);
onCreate(db);
}
public List<tableStudents> getData() {
List<tableStudents> studentList = new ArrayList<tableStudents>();
// Select All Query
String selectQuery = "SELECT * FROM " + Table;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
tableStudents student = new tableStudents();
student.name = cursor.getString(0);
student.password = cursor.getString(1);
student.gender = cursor.getString(2);
student.age = Integer.parseInt(cursor.getString(3));
student.course = cursor.getString(4);
student.modules = cursor.getString(5);
studentList.add(student);
} while (cursor.moveToNext());
}
// return contact list
return studentList;
}
public boolean insertStudent(String name, String gender, int age, String password, String course, String modules) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Student_Name, name);
contentValues.put(Student_Password, password);
contentValues.put(Student_Gender, gender);
contentValues.put(Student_Age, age);
contentValues.put(Student_Course, course);
contentValues.put(Modules, modules);
Log.d("DB", "Inserted Successfully");
return true;
}
}
MainActivity.java
public class MainActivity extends Activity {
Intent appIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DB db = new DB(this);
db.insertStudent("T", "T", 5, "T", "T", "T");
List<tableStudents> outputList = db.getData();
}
public void goHomepage(View v)
{
Intent intent = new Intent(MainActivity.this, Homepage.class);
startActivity(intent);
}
public void goAccount(View v)
{
Intent intent = new Intent(MainActivity.this, MyAccount.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
嗯,您有一个List
,因此您可以使用ListView
显示相应的数据。如果您只是想查看从getData返回的数据,您可以遍历数组并Log
将它传递给您的logcat输出。
for (tableStudents student: outputList) {
// Log your results.
Log.d("result_list", student.toString())
}
如果您想在应用中显示此信息,请使用ListView
。
Android为任意数据和listview提供了一个基本界面,如果您认为需要,可以扩展和自定义,称为BaseAdapter
。它们还提供了BaseAdapter
的具体实现,供您使用而无需太多工作。
一个这样的实现是ArrayAdapter
类。 (See here)此类描述如下:
由任意对象数组支持的具体BaseAdapter。默认情况下,此类期望提供的资源ID引用单个TextView。如果要使用更复杂的布局,请使用也带有字段ID的构造函数。该字段id应引用较大布局资源中的TextView。
但是引用了TextView,它将填充数组中每个对象的toString()。您可以添加自定义对象的列表或数组。覆盖对象的toString()方法,以确定将为列表中的项显示哪些文本。
因此,您可以在活动的布局文件中创建ListView
,为其指定一个ID,然后创建一个适配器以支持ListView
。例如:
首先在活动xml文件中创建列表视图。
<强> activity_main.xml中强>
<ListView
android:id="@+id/my_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
一旦定义了,您应该覆盖tableStudents.toString()
方法并实现一个用户友好的版本,将相关字段显示为字符串。
tableStudents.java (通常,您应该避免这样做,而是扩展ArrayAdapter
,然后覆盖getView()
并在那里显示对象的属性)
@Override
public String toString() {
StringBuilder out = new StringBuilder();
out.append("Student: ")
out.append(this.name);
// etc.
return out.toString();
}
现在,在您致电getData()
后的活动中,请参考ListView
并创建ArrayAdapter
:
<强> MainActivity.java 强>
List<tableStudents> outputList = db.getData();
// Reference the list view.
ListView listView = (ListView) findViewById(R.id.my_list_view);
// Create the adapter.
ArrayAdapter listViewAdapter = new ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
outputList
);
// Then set the adapter for the list view.
listView.setAdapter(listViewAdapter);
您的数据库结果应显示在应用中的一个不错的可滚动列表中。
希望这有帮助,如果您有任何问题,请与我联系。