我对Android开发很新,而且我的Java有点生疏,所以请耐心等待。我想完成以下任务:
问题:此时我没有成功确认已插入数据并将数据拉回并在屏幕上显示。我想我已经确定了一个问题,即course_list没有填充课程,但由于我没有收到任何错误,我正在努力找出真正的问题。任何见解或指导都会非常感激!
MainActivity.Java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private ListView courseListView;
private ArrayList<Course> course_list = new ArrayList<Course>();
private ArrayList<String> course_names = new ArrayList<String>();
private ArrayAdapter<String> ap;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
courseListView = (ListView) findViewById(R.id.course_list);
// Init database
db = new DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
initAdapter();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Called when Add Course button is pressed
public void onClick( View v ) {
Intent intent = new Intent(v.getContext(), AddCourseActivity.class);
v.getContext().startActivity(intent);
}
//
private void initAdapter() {
if( course_list.size() != 0) {
course_list.clear();
course_list = db.getCourses();
for (int i = 0; i < course_list.size(); ++i) {
course_names.add(course_list.get(i).getCourseName());
}
ap = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, course_names);
courseListView.setAdapter(ap);
}
}
}
DatabaseHelper.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "Courses";
public static final String COURSE_TABLE = "MyCourses";
public static final String KEY_COURSE_NUM = "CourseNum";
public static final String KEY_COURSE_NAME = "CourseName";
public static final String KEY_COURSE_DESC = "CourseDesc";
public static final String KEY_DATE = "Date";
public static final String KEY_TIME = "Time";
public static final String KEY_INSTRUCTOR = "Instructor";
private ArrayList<Course> courseList = new ArrayList<Course>();
// Constructor, Create DB
public DatabaseHelper( Context context ) {
super(context, DATABASE_NAME, null, 1) ;
}
// Create the MyCourses table
public void onCreate( SQLiteDatabase db ) {
db.execSQL("CREATE TABLE " + COURSE_TABLE + " (_id INTEGER PRIMARY KEY, "
+ KEY_COURSE_NUM + " TEXT, "
+ KEY_COURSE_NAME + " TEXT, "
+ KEY_COURSE_DESC + " TEXT, "
+ KEY_DATE + " TEXT, "
+ KEY_TIME + " TEXT, "
+ KEY_INSTRUCTOR + " TEXT)");
}
// Upgrade the MyCourses table
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
// Drop older table if it exists
db.execSQL("DROP TABLE IF EXISTS " + COURSE_TABLE);
// Create table again
onCreate(db);
}
// Add a new course
public void addCourse( Course course ) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_COURSE_NUM, course.getCourseNum());
contentValues.put(KEY_COURSE_NAME, course.getCourseName());
contentValues.put(KEY_COURSE_DESC, course.getCourseDesc());
contentValues.put(KEY_DATE, course.getCourseDate());
contentValues.put(KEY_TIME, course.getCourseTime());
contentValues.put(KEY_INSTRUCTOR, course.getCourseInstructor());
db.insert(COURSE_TABLE, null, contentValues);
db.close();
}
// Get all courses, assign to array of course objects an return array
public ArrayList<Course> getCourses() {
courseList.clear();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + COURSE_TABLE, null);
// Loop through cursor, extract data and assign to item Course object
if( cursor.getCount()!=0) {
if( cursor.moveToFirst() ) {
do {
Course item = new Course();
item.setCourseNum(cursor.getString(cursor.getColumnIndex(KEY_COURSE_NUM)));
item.setCourseName(cursor.getString(cursor.getColumnIndex(KEY_COURSE_NAME)));
item.setCourseDesc(cursor.getString(cursor.getColumnIndex(KEY_COURSE_DESC)));
item.setCourseDate(cursor.getString(cursor.getColumnIndex(KEY_DATE)));
item.setCourseTime(cursor.getString(cursor.getColumnIndex(KEY_TIME)));
item.setCourseInstructor(cursor.getString(cursor.getColumnIndex(KEY_INSTRUCTOR)));
// Add the object to the courseList array of Courses
courseList.add(item);
} while( cursor.moveToNext());
}
}
cursor.close();
db.close();
return courseList;
}
}
Course.java
public class Course {
private String courseNum;
private String courseName;
private String courseDesc;
private String date;
private String time;
private String instructor;
// Empty constructor
public Course() {
}
// Constructor for new course
public Course(Course course) {
this.courseNum = course.courseNum;
this.courseName = course.courseName;
this.courseDesc = course.courseDesc;
this.date = course.date;
this.time = course.time;
this.instructor = course.instructor;
}
// Get courseNum
public String getCourseNum() {
return courseNum;
}
// Set courseNum
public void setCourseNum( String courseNum ) {
this.courseNum = courseNum;
}
// Get courseName
public String getCourseName() {
return courseName;
}
// Set courseName
public void setCourseName( String courseName ) {
this.courseName = courseName;
}
// Get courseDesc
public String getCourseDesc() {
return courseDesc;
}
// Set courseDesc
public void setCourseDesc( String courseDesc ) {
this.courseDesc = courseDesc;
}
// Get date
public String getCourseDate() {
return date;
}
// Set date
public void setCourseDate( String date ) {
this.date = date;
}
// Get time
public String getCourseTime() {
return time;
}
// Set time
public void setCourseTime( String time ) {
this.time = time;
}
// Get instructor
public String getCourseInstructor() {
return instructor;
}
// Set instructor
public void setCourseInstructor( String instructor ) {
this.instructor = instructor;
}
}
AddCourseActivity.java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class AddCourseActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_add_course, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// Called when Save button is pressed
public void saveCourse(View v) {
Course c = new Course();
DatabaseHelper db = new DatabaseHelper(this);
EditText num = (EditText) findViewById(R.id.courseNum);
c.setCourseNum(num.getText().toString());
EditText name = (EditText) findViewById(R.id.courseName);
c.setCourseName(name.getText().toString());
EditText desc = (EditText) findViewById(R.id.courseDesc);
c.setCourseDesc(desc.getText().toString());
EditText date = (EditText) findViewById(R.id.courseDate);
c.setCourseDate(date.getText().toString());
EditText time = (EditText) findViewById(R.id.courseTime);
c.setCourseTime(time.getText().toString());
EditText inst = (EditText) findViewById(R.id.courseInstructor);
c.setCourseInstructor(inst.getText().toString());
db.addCourse(c);
// Return to Main Activity
Intent intent = new Intent(v.getContext(), MainActivity.class);
v.getContext().startActivity(intent);
}
// Called when Cancel button is pressed
public void cancel(View v) {
// Return to Main Activity
Intent intent = new Intent(v.getContext(), MainActivity.class);
v.getContext().startActivity(intent);
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="My Courses"
android:textColor="#FFFFFF"
android:textSize="30dp"
android:background="#000000"/>
<Button
android:id="@+id/addCourse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Course"
android:onClick="onClick"/>
<ListView
android:id="@+id/course_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
Activity_Add_Course.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Add New Course"
android:textColor="#FFFFFF"
android:textSize="30dp"
android:background="#000000"/>
<EditText
android:id="@+id/courseNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Course #"/>
<EditText
android:id="@+id/courseName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Course Name"/>
<EditText
android:id="@+id/courseDesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="6"
android:hint="Course Description"/>
<EditText
android:id="@+id/courseDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Course Date"/>
<EditText
android:id="@+id/courseTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Course Time"/>
<EditText
android:id="@+id/courseInstructor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Course Instructor"/>
<Button
android:id="@+id/saveCourse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
android:onClick="saveCourse"/>
<Button
android:id="@+id/cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:onClick="cancel"/>
</LinearLayout>