Android 如何使用where子句获取值listview项并使用它在数据库中插入数据

Android 如何使用where子句获取值listview项并使用它在数据库中插入数据,android,listview,Android,Listview,我有一个listview,它显示从SQLite数据库检索到的studentdata。当你点击一个项目时,你进入一个活动,在那里你可以给一个学生评分。现在我想知道当你点击一个保存按钮时,你如何插入那个等级。必须将分数插入正确的学生(该学生先前在上一活动的列表视图中选择)。这是我的DBAdapter: package com.ipmedt4.challengeweek_v2; import android.content.ContentValues; import android.content.

我有一个listview,它显示从SQLite数据库检索到的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);
}
}
package com.ipmedt4.challengeweek_v2;
导入android.annotation.TargetApi;
导入android.app.Activity;
导入android.app.ListActivity;
导入android.content.Context;
导入android.content.Intent;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteException;
导入android.database.sqlite.SQLiteOpenHelper;
导入android.os.Build;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.FilterQueryProvider;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.SimpleCursorAdapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.ArrayList;
公共类OverzichtStudenten扩展了ActionBarActivity{
私人学生助理;
私有SimpleCursorAdapter数据适配器;
列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u overzicht\u studenten);
dbHelper=新学生dbadapter(this);
dbHelper.open();
//maak alle数据学校
dbHelper.deleteAllStudenten();
//toevoegen-van数据
dbHelper.insertStudenten();
//genereren ListView van Sqlite数据库
displayListView();
}
@TargetApi(构建版本代码蜂窝)
私有void displayListView(){
Cursor Cursor=dbHelper.showAlleStudenten();
//选择人gewenste kolommen
字符串[]列=新字符串[]{
学生数据库,
StudentDBAdapter.KEY\u studentnumer,
学生数据适配器
};
//在XML gedefiniërde视图中
int[]至=新int[]{
R.id.naam,
R.id.studentnummer,
克拉斯
};
dataAdapter=新的SimpleCorsorAdapter(
这个,R.layout.student\u信息,
光标,
柱,
到
1);
ListView ListView=(ListView)findViewById(R.id.listView1);
setAdapter(dataAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView列表视图、视图视图、整型位置、长id){
游标游标=(游标)listView.getItemAtPosition(位置);
Intent myintent2=新的Intent(view.getContext(),beoorderingscherm.class);
起始触觉(myintent2);
}
});
EditText myFilter=(EditText)findViewById(R.id.myFilter);
myFilter.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
{dataAdapter.getFilter().filter(s.toString());
}}
@凌驾
公共无效后文本已更改(可编辑){
}
});
setFilterQueryProvider(新的FilterQueryProvider(){
@凌驾
公共游标运行查询(CharSequence约束){
返回dbHelper.fetchStudentenbyName(constraint.toString());
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u overzicht\u studenten,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
因此,有一个带有listview的活动,它是对学生的概述(包括studentname、class和studentnumber)。当你点击一个学生时,你进入一个活动,在那里你可以给一个学生评分,并将该评分保存在数据库中。 我知道我必须在DBAdapter类中创建一个方法,我可以在活动中实现该方法,您可以在活动中为学生评分。我想到了Contentvalues.get。
我的问题是:如何检索listview中项目的值,以便使用这些值在数据库中的正确学生处插入分数(在下一个活动中)?这样我就可以使用这些值进行SQL查询了?我还有一个“学生”班,里面有能手和能手。谢谢

使用
Intent.putExtra(“key\u name”,value)
修改onItemClick,将所选行的位置或id传递给下一个活动。然后,您可以在后续活动中使用
getIntent.getIntExtra(“key\u name”)

检索它。您可以从
onItemClick
获取
位置。使用它来获取学生的记录。因此,当我在年级类中单击“保存”按钮时,我可以使用“onItemClick”来获取项目在(上一个)Listview活动中的“位置”?