我在sqlite数据库中创建了3个表,即
表1)学院(栏目: - college_id,college_name,college_address,city,state,pincode,phone_number,rating,principal,email,website)
表2)课程(栏目: - course_id,course_name,course_description,course_stream,duration,rating)&
表3)CollegeCourse(列: - collegecourse_id,college_id,course_id,effsective_start_date,effective_end_date,effective_flag)。
我无法从SQLite数据库中检索数据,在列表视图中显示数据,过滤列表视图,以及在单击项目时显示详细信息。
示例: - http://imageshack.com/a/img633/7490/a58U0S.png
布局:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Type college name to filter..." >
<requestFocus />
</EditText>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
MainActivity.java:
package com.android.collegefinder;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.searchcollegebtn);
b.setOnClickListener(this);
CollegeTable db = new CollegeTable(this);
db.addCollege(new College(1, "Smt. Chandibai Himathmal Mansukhani College", "Opp. Railway Station, Smt.Chandibai Himathmal Mansukhani road", "Ulhasnagar", "Maharashtra", "421003", "02512734940", "B++", "Dr. Charanjit Kaur Singh", "principalchmc@gmail.com", "www.chm.edu"));
db.getAllColleges();
db.addCourse(new Course(1, "Economics", "N/A", "Arts", "3", 0));
db.getAllCourses();
db.addCollegeCourse(new CollegeCourse(1, 1, 1, "2015-01-01 00:00:00.000", "9999-01-01 00:00:00.000", 1));
db.getAllCollegeCourse();
}
@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);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.searchcollegebtn) {
startActivity(new Intent(MainActivity.this, SearchCollege.class));
}
}
}
DBHandler: -
package com.android.collegefinder;
import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class CollegeTable extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "CollegeFinder";
public CollegeTable(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_COLLEGE_TABLE = "CREATE TABLE College ( " +
"college_id INTEGER PRIMARY KEY, " + "college_name TEXT, " + "college_address TEXT, " + "city TEXT, " + "state TEXT, " +
"pincode TEXT, " + "phone_number TEXT, " + "college_rating TEXT, " + "principal TEXT, " + "email TEXT, " + "website TEXT )";
String CREATE_COURSE_TABLE = "CREATE TABLE Course ( " +
"course_id INTEGER PRIMARY KEY, " + "course_name TEXT, " + "course_description TEXT, " + "course_stream TEXT, " +
"duration TEXT, " + "course_rating REAL )";
String CREATE_COLLEGECOURSE_TABLE = "CREATE TABLE CollegeCourse ( " +
"collegecourse_id INTEGER PRIMARY KEY, " + "college_id INTEGER, " + "course_id INTEGER, " + "effective_start_date TEXT, " + "effective_end_date TEXT," + "effective_flag INTEGER )";
db.execSQL(CREATE_COLLEGE_TABLE);
db.execSQL(CREATE_COURSE_TABLE);
db.execSQL(CREATE_COLLEGECOURSE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS College");
this.onCreate(db);
}
private static final String TABLE_COLLEGE = "College";
private static final String KEY_COLLEGE_ID = "college_id";
private static final String KEY_COLLEGE_NAME = "college_name";
private static final String KEY_COLLEGE_ADDRESS = "college_address";
private static final String KEY_CITY = "city";
private static final String KEY_STATE = "state";
private static final String KEY_PINCODE = "pincode";
private static final String KEY_PHONE_NUMBER = "phone_number";
private static final String KEY_COLLEGE_RATING = "college_rating";
private static final String KEY_PRINCIPAL = "principal";
private static final String KEY_EMAIL = "email";
private static final String KEY_WEBSITE = "website";
private static final String[] COLUMNS = {KEY_COLLEGE_ID,KEY_COLLEGE_NAME,KEY_COLLEGE_ADDRESS,KEY_CITY,KEY_STATE,KEY_PINCODE,KEY_PHONE_NUMBER,KEY_COLLEGE_RATING,KEY_PRINCIPAL,KEY_EMAIL,KEY_WEBSITE};
public void addCollege(College college){
Log.d("addCollege", college.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_COLLEGE_ID, college.getCollegeId());
values.put(KEY_COLLEGE_NAME, college.getCollegeName());
values.put(KEY_COLLEGE_ADDRESS, college.getCollegeAddress());
values.put(KEY_CITY, college.getCity());
values.put(KEY_STATE, college.getState());
values.put(KEY_PINCODE, college.getPincode());
values.put(KEY_PHONE_NUMBER, college.getPhoneNumber());
values.put(KEY_COLLEGE_RATING, college.getCollegeRating());
values.put(KEY_PRINCIPAL, college.getPrincipal());
values.put(KEY_EMAIL, college.getEmail());
values.put(KEY_WEBSITE, college.getWebsite());
db.insert(TABLE_COLLEGE, null, values);
db.close();
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//COURSE TABLE
private static final String TABLE_COURSE = "Course";
private static final String KEY_COURSE_ID = "course_id";
private static final String KEY_COURSE_NAME = "course_name";
private static final String KEY_COURSE_DESCRIPTION = "course_description";
private static final String KEY_COURSE_STREAM = "course_stream";
private static final String KEY_DURATION = "duration";
private static final String KEY_COURSE_RATING = "course_rating";
private static final String[] COLUMNS_COURSE = {KEY_COURSE_ID, KEY_COURSE_NAME, KEY_COURSE_DESCRIPTION, KEY_COURSE_STREAM, KEY_DURATION, KEY_COURSE_RATING};
public void addCourse(Course course){
Log.d("addCourse", course.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_COURSE_ID, course.getCourseId());
values.put(KEY_COURSE_NAME, course.getCourseName());
values.put(KEY_COURSE_DESCRIPTION, course.getCourseDescription());
values.put(KEY_COURSE_STREAM, course.getCourseStream());
values.put(KEY_DURATION, course.getDuration());
values.put(KEY_COURSE_RATING, course.getCourseRating());
db.insert(TABLE_COURSE, null, values);
db.close();
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// college course table
private static final String TABLE_COLLEGECOURSE = "CollegeCourse";
private static final String KEY_COLLEGECOURSE_ID = "collegecourse_id";
private static final String KEY_COLLEGE_ID1 = "college_id";
private static final String KEY_COURSE_ID1 = "course_id";
private static final String KEY_EFFECTIVE_START_DATE = "effective_start_date";
private static final String KEY_EFFECTIVE_END_DATE = "effective_end_date";
private static final String KEY_EFFECTIVE_FLAG = "effective_flag";
private static final String[] COLUMNS_COLLEGECOURSE = {KEY_COLLEGECOURSE_ID,KEY_COLLEGE_ID1,KEY_COURSE_ID1,KEY_EFFECTIVE_START_DATE,KEY_EFFECTIVE_END_DATE,KEY_EFFECTIVE_FLAG};
public void addCollegeCourse(CollegeCourse collegecourse){
Log.d("addCollegeCourse", collegecourse.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_COLLEGECOURSE_ID, collegecourse.getCollegeCourseId());
values.put(KEY_COLLEGE_ID1, collegecourse.getCollegeId());
values.put(KEY_COURSE_ID1, collegecourse.getCourseId());
values.put(KEY_EFFECTIVE_START_DATE, collegecourse.getEffectiveStartDate());
values.put(KEY_EFFECTIVE_END_DATE, collegecourse.getEffectiveEndDate());
values.put(KEY_EFFECTIVE_FLAG, collegecourse.getEffectiveFlag());
db.insert(TABLE_COLLEGECOURSE, null, values);
db.close();
}
SearchCollege.java: -
package com.android.collegefinder;
import android.app.Activity;
import android.os.Bundle;
public class SearchCollege extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.searchcollege);
}
}