我有一个listview,显示从sqlitedatabase检索的studentdata。当您点击某个项目时,您将进入可以为学生评分的活动。现在,我想知道在单击“保存”按钮时如何插入该等级。必须将成绩插入到正确的学生中(此学生之前已在上一个活动的列表视图中选择)。这是我的DBAdapter:
package com.ipmedt4.challengeweek_v2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Charlie on 28-12-2014.
*/
public class StudentDBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAAM = "_naam";
public static final String KEY_STUDENTNUMMER = "_studentnummer";
public static final String KEY_KLAS = "_klas";
public static final String KEY_CIJFER = "_cijfer";
public static final String KEY_OPMERKINGEN = "_opmerkingen";
private static final String TAG = "StudentDBAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDB;
private static final String DATABASE_NAME = "Challengeweek";
private static final String SQLITE_TABLE = "Studenten";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static final String DATABASE_CREATE = " CREATE TABLE IF NOT EXISTS " + SQLITE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAAM + "," + KEY_STUDENTNUMMER + ","
+ KEY_KLAS + "," + KEY_CIJFER + "," + KEY_OPMERKINGEN + ")";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
Log.w(TAG, "Upgrading database from version" + oldVersion + "to" + newVersion +
"which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS" + SQLITE_TABLE);
onCreate(db);
}
}
public StudentDBAdapter (Context ctx){
this.mCtx = ctx;
}
public StudentDBAdapter open () throws SQLiteException{
mDbHelper = new DatabaseHelper(mCtx);
mDB = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createStudent (String naam, String studentnummer, String klas){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAAM, naam);
initialValues.put(KEY_STUDENTNUMMER, studentnummer);
initialValues.put(KEY_KLAS, klas);
return mDB.insert(SQLITE_TABLE, null, initialValues);
}
public boolean deleteAllStudenten(){
int doneDelete = 0;
doneDelete = mDB.delete(SQLITE_TABLE, null, null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
public Cursor fetchStudentenbyName(String inputText) throws SQLiteException{
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length() == 0){
mCursor = mDB.query(SQLITE_TABLE, new String[] {
KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS
}, null, null, null, null, null);
}
else {
mCursor = mDB.query(true, SQLITE_TABLE, new String [] {KEY_ROWID,
KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS}, KEY_NAAM + "like '%" + inputText +
"%'", null, null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor showAlleStudenten(){
Cursor mCursor = mDB.query(SQLITE_TABLE, new String[] {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER,
KEY_KLAS}, null, null, null, null, null);
if (mCursor != null){
mCursor.moveToFirst();
}
return mCursor;}
public Cursor SelecteerStudenten1A(){
String Table = "Studenten";
String [] fields = {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS};
String where = "_klas = 'INF1A'";
Cursor mCursor = mDB.query(Table, fields, where, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor SelecteerStudenten1B(){
String Table = "Studenten";
String [] fields = {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS};
String where = "_klas = 'INF1B'";
Cursor mCursor = mDB.query(Table, fields, where, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor SelecteerStudenten1C(){
String Table = "Studenten";
String [] fields = {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS};
String where = "_klas = 'INF1C'";
Cursor mCursor = mDB.query(Table, fields, where, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor SelecteerStudenten1D(){
String Table = "Studenten";
String [] fields = {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS};
String where = "_klas = 'INF1D'";
Cursor mCursor = mDB.query(Table, fields, where, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor SelecteerStudenten1E(){
String Table = "Studenten";
String [] fields = {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS};
String where = "_klas = 'INF1E'";
Cursor mCursor = mDB.query(Table, fields, where, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor SelecteerStudenten1F(){
String Table = "Studenten";
String [] fields = {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS};
String where = "_klas = 'INF1F'";
Cursor mCursor = mDB.query(Table, fields, where, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor SelecteerStudenten1G(){
String Table = "Studenten";
String [] fields = {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS};
String where = "_klas = 'INF1G'";
Cursor mCursor = mDB.query(Table, fields, where, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor SelecteerStudenten1H(){
String Table = "Studenten";
String [] fields = {KEY_ROWID, KEY_NAAM, KEY_STUDENTNUMMER, KEY_KLAS};
String where = "_klas = 'INF1H'";
Cursor mCursor = mDB.query(Table, fields, where, null, null, null, null);
if(mCursor !=null){
mCursor.moveToFirst();
}
return mCursor;
}
public void insertStudenten(){
createStudent("Adel, Pieter", "s1078455", "INF1G");
createStudent("Bieber, Justin", "s1084567", "INF1D");
createStudent("Dadel, Maria", "s1087499", "INF1F");
createStudent("Dekker, Vera", "s1084235", "INF1B");
createStudent("Elegast, Karel", "s1074211", "INF1E");
createStudent("Emmer, Lisa", "s1074326", "INF1C");
createStudent("Hendriksen, Dorien", "s1074788", "INF1A");
createStudent("Krokus, Joke", "s1089413", "INF1H");
createStudent("Mandarijn, Max", "s1084211", "INF1H");
createStudent("Nelissen, Henk", "s1087465", "INF1F");
createStudent("Rokers, Lianne", "s1071244", "INF1G");
createStudent("Tekkel, Hester ", "s1071586", "INF1C");
createStudent("Uitjes, Berend ", "s1087451", "INF1A");
createStudent("Venkeltje, Sophia ", "s1075612", "INF1B");
createStudent("Vlieger, Olaf ", "s1074127", "INF1E");
createStudent("Vogel, Bas ", "s1084522", "INF1A");
createStudent("Water, Michiel ", "s1084531", "INF1H");
createStudent("Weken, Dirk ", "s1074537", "INF1D");
createStudent("Wieken, Stefanie ", "s1084511", "INF1C");
createStudent("Zeker, Donald ", "s1077369", "INF1E");
}
}
这是我的ListView课程,概述了所有学生:
package com.ipmedt4.challengeweek_v2;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class OverzichtStudenten extends ActionBarActivity {
private StudentDBAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overzicht_studenten);
dbHelper = new StudentDBAdapter(this);
dbHelper.open();
//maak alle data schoon
dbHelper.deleteAllStudenten();
//toevoegen van data
dbHelper.insertStudenten();
//genereren ListView van SQLiteDatabase
displayListView();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void displayListView(){
Cursor cursor = dbHelper.showAlleStudenten();
//selecteer gewenste kolommen
String[] columns = new String[]{
StudentDBAdapter.KEY_NAAM,
StudentDBAdapter.KEY_STUDENTNUMMER,
StudentDBAdapter.KEY_KLAS
};
//In XML gedefiniërde Views
int[] to = new int[]{
R.id.naam,
R.id.studentnummer,
R.id.klas
};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.student_info,
cursor,
columns,
to,
1);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
Intent myintent2 = new Intent(view.getContext(), Beoordelingscherm.class);
startActivity(myintent2);
}
});
EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
{ dataAdapter.getFilter().filter(s.toString());
}}
@Override
public void afterTextChanged(Editable s) {
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchStudentenbyName(constraint.toString());
}
});
}
@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_overzicht_studenten, 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);
}
}
所以,有一个listview活动,它是学生的概述(有学生姓名,班级和学生编号)。当您点击学生时,您将进入一项活动,您可以在该活动中为学生评分并将该成绩保存在数据库中。 我知道我必须在DBAdapter类中创建一个方法,我可以在您可以为学生评分的活动中实现该方法。我想到了Contentvalues.get。 我的问题是:如何检索listview中项目的值,以便我可以使用这些值在数据库中的正确学生中插入成绩(在下一个活动中)?所以我可以使用这些值来进行SQL查询?我还有一个课程“学生”,其中包含吸气剂和制定者。谢谢!
答案 0 :(得分:0)
修改onItemClick,使用Intent.putExtra("key_name",value)
将所选行的位置或ID传递给下一个活动。然后,您可以使用getIntent.getIntExtra("key_name")