0

我使用 SQLite 数据库制作了一个应用程序。我想在列表视图中显示数据,但我不想显示数据库中的所有数据,而只是显示一些数据。所以,我想添加一个条件。我只想显示字符串“dag”为“maandag”的数据。谁能帮我?

这是我的 DatabaseHelper 中创建数据库的代码:

package com.persoonlijk.rooster.test2;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import java.util.ArrayList;
import java.util.List;

//A helper class to manage database creation and version management. 
public class DataManipulator
{
//Database attributes
private static final String DATABASE_NAME = "mydatabase.db";
static final String TABLE_NAME = null;
private static final int DATABASE_VERSION = 12;

//Table attributes
public static final String KEY_ROWID = "id";
public static final String KEY_DAG = "dag";
public static final String KEY_UUR = "uur";
public static final String KEY_VAK = "vak";
public static final String KEY_LOKAAL = "lokaal";

private static Context context;
static SQLiteDatabase db;

private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + " (dag,uur,vak,lokaal) values (?,?,?,?)";
public DataManipulator(Context context) {
DataManipulator.context = context;
OpenHelper openHelper = new OpenHelper(DataManipulator.context);
DataManipulator.db = openHelper.getWritableDatabase();
this.insertStmt = DataManipulator.db.compileStatement(INSERT);
}

public long insert(String dag,String uur,String vak,String lokaal) {
this.insertStmt.bindString(1, dag);
this.insertStmt.bindString(2, uur);
this.insertStmt.bindString(3, vak);
this.insertStmt.bindString(4, lokaal);
return this.insertStmt.executeInsert();
}

public void deleteAll() {
db.delete(TABLE_NAME, null, null);
}

public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME, new String[] { "id","dag","uur","vak","lokaal" }, null, null, null, null, "dag asc");
int x=0;
if (cursor.moveToFirst()) {
   do {
    String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),
cursor.getString(3),cursor.getString(4)};
    list.add(b1);
    x=x+1;
   } while (cursor.moveToNext());
 }
if (cursor != null && !cursor.isClosed()) {
   cursor.close();
}
cursor.close();
return list;
}

public boolean delete(long id) {
    return db.delete(TABLE_NAME, KEY_ROWID + "=" + id, null) > 0;
        }

 private static class OpenHelper extends SQLiteOpenHelper {
 OpenHelper(Context context) {
     super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
     db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, dag TEXT, uur TEXT, vak TEXT, lokaal TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
     onCreate(db);
}
}

}

这是我要显示特定数据的 Activity 的代码。现在,显示了数据库中的所有数据。

package com.persoonlijk.rooster.test2;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class RoosterMaandag extends ListActivity{   

  TextView selection;
  public int idToModify;
  DataManipulator dm;

  List<String[]> list = new ArrayList<String[]>();
  List<String[]> names2 =null ;
  String[] stg1;

  /** Called when the activity is first created. */
  //zorgt voor het overzicht van de gegevens uit de database
  @Override
  public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.rooster);

    dm = new DataManipulator(this);
    names2 = dm.selectAll();

    stg1=new String[names2.size()];
    int x=0;
    String stg;

    for (String[] dag : names2) {
    stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
    stg1[x]=stg;
    x++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stg1);
    this.setListAdapter(adapter);
    selection=(TextView)findViewById(R.id.selection);

}  

 //menuknoppen
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
   menu.add(Menu.NONE, 0, 0, "Voeg gegevens toe");
   menu.add(Menu.NONE, 1, 1, "Verwijder gegevens");
   return super.onCreateOptionsMenu(menu);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
       case 0:
           startActivity(new Intent(this, SaveData.class));
       return true;
       case 1:
           startActivity(new Intent(this, VerwijderData.class));
       return true;

   }
   return false;
  }

 }

当我的问题不够清楚时,请告诉我。我真的希望有人能帮助我!

我在我的 DataManipulator.java 中添加了这段代码:

    public List<String[]> selectSome(String arg) {
    String[] columns = new String[] { "id", "dag", "uur", "vak", "lokaal" };
    String[] selectionArgs = {arg};
    Cursor cursor = db.query(TABLE_NAME, columns, "dag = ?", selectionArgs, null, null, "dag asc");

    List<String[]> list = new ArrayList<String[]>(cursor.getCount());
    while (cursor.moveToNext()) {
        String[] b1 = new String[] { cursor.getString(0),
                cursor.getString(1), cursor.getString(2),
                cursor.getString(3), cursor.getString(4) };
        list.add(b1);
    }
    cursor.close();
    return list;
}

我的 RoosterMaandag.java 中的这段代码:

 public void onCreate(Bundle savedInstanceState){
 ....
 dm.selectSome("maandag");
 ....
 }
4

2 回答 2

0

你的DataManipulator班级需要一个新方法:

public List<String[]> selectSome(String arg) {
    String[] columns = new String[] { "id", "dag", "uur", "vak", "lokaal" };
    String[] selectionArgs = {arg};
    Cursor cursor = db.query(TABLE_NAME, columns, "dag = ?", selectionArgs, null, null, "dag asc");

    List<String[]> list = new ArrayList<String[]>(cursor.getCount());
    while (cursor.moveToNext()) {
        String[] b1 = new String[] { cursor.getString(0),
                cursor.getString(1), cursor.getString(2),
                cursor.getString(3), cursor.getString(4) };
        list.add(b1);
    }
    cursor.close();
    return list;
}

我再说一遍。考虑将 aCursorAdapter用于这些任务。

于 2013-01-03T15:43:50.343 回答
0

我不知道 android 但对于 sql

尝试这个

select data_to_print from table_name where REGEXP_LIKE(column_name_where_to_match,'dag$');

更多信息在这里http://docs.oracle.com/cd/B12037_01/server.101/b10759/ap_posix001.htm#i690819

例子:

SQL> select job_id from jobs where regexp_like(job_id,'N$');

JOB_ID
----------
MK_MAN
PU_MAN
SA_MAN
ST_MAN
于 2013-01-03T15:20:28.543 回答