我编写了一个能够在数据库中保存主题和标记的应用程序。我必须解决的最后一个问题是,如何从Marks类中过滤ListView,以便它只显示相应的主题。
该应用程序由三个类组成。
主:
package maturaarbeit.nicola_pfister.marks;
import maturaarbeit.nicola_pfister.marks.database.DBAdapter;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
public class Main extends ListActivity {
DBAdapter db = new DBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subjects);
}
@Override
protected void onPause() {
super.onPause();
db.close();
}
@Override
protected void onResume() {
super.onResume();
db.open();
getData();
}
@SuppressWarnings("deprecation")
private void getData() {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
db.getAllSubjects(),
new String[] { "subject" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
Cursor cursor = db.getSubject(id);
String subject = null;
try {
subject = cursor.getString(cursor.getColumnIndex("subject"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(Main.this, Marks.class);
intent.putExtra("selected", subject);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.subjects, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
Builder builder = new Builder(this);
final EditText input = new EditText(this);
builder
.setTitle(R.string.dialog_addsubject)
.setView(input)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString();
db.insertSubject(value);
getData();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
break;
case R.id.menu_delete:
// getData();
break;
}
return super.onOptionsItemSelected(item);
}
}
商标:
package maturaarbeit.nicola_pfister.marks;
import java.util.Calendar;
import maturaarbeit.nicola_pfister.marks.database.DBAdapter;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;
public class Marks extends ListActivity {
DBAdapter db = new DBAdapter(this);
String selection = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marks);
final Bundle extras = getIntent().getExtras();
selection = extras.getString("selected");
if (extras!= null) {
setTitle(selection);
}
}
@Override
protected void onPause() {
super.onPause();
db.close();
}
@Override
protected void onResume() {
super.onResume();
db.open();
getData();
}
@SuppressWarnings("deprecation")
private void getData() {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
db.getAllMarks(),
new String[] { "value" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.marks, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
Builder builder = new Builder(this);
final EditText input = new EditText(this);
builder
.setTitle(R.string.dialog_addmark)
.setView(input)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString();
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH) + 1;
final String date = day + "." + month + ".";
db.insertMark(date, selection, value);
getData();
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
break;
case R.id.menu_delete:
break;
}
return super.onOptionsItemSelected(item);
}
}
和DBAdapter:
package maturaarbeit.nicola_pfister.marks.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
int id = 0;
public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_SUBJECT = "subject";
public static final String KEY_VALUE = "value";
public static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "database";
private static final String DATABASE_TABLE_MARKS = "marks";
private static final String DATABASE_TABLE_SUBJECTS = "subjects";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE_MARKS =
"create table " + DATABASE_TABLE_MARKS + " (" + KEY_ROWID + " integer primary key autoincrement, " +
KEY_DATE +" text not null, " +
KEY_SUBJECT +" text not null, " +
KEY_VALUE +" text not null" + ");";
private static final String DATABASE_CREATE_SUBJECTS =
"create table " + DATABASE_TABLE_SUBJECTS + " (" + KEY_ROWID + " integer primary key autoincrement, " +
KEY_SUBJECT +" text not null " + ");";
@SuppressWarnings("unused")
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context context) {
this.context = context;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE_MARKS);
db.execSQL(DATABASE_CREATE_SUBJECTS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_MARKS);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_SUBJECTS);
onCreate(db);
}
}
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public long insertMark (String date, String subject, String value) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_SUBJECT, subject);
initialValues.put(KEY_VALUE, value);
return db.insert(DATABASE_TABLE_MARKS, null, initialValues);
}
public boolean deleteMark(long rowId) {
return db.delete(DATABASE_TABLE_MARKS, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllMarks() {
return db.query(DATABASE_TABLE_MARKS, new String[] {
KEY_ROWID,
KEY_DATE,
KEY_SUBJECT,
KEY_VALUE
}, null, null, null, null, null);
}
public Cursor getMark(long rowId) throws SQLException {
Cursor mCursor =
db.query(true, DATABASE_TABLE_MARKS, new String[] {
KEY_ROWID,
KEY_DATE,
KEY_SUBJECT,
KEY_VALUE
},
KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateMark(long rowId, String date, String subject, String value) {
ContentValues args = new ContentValues();
args.put(KEY_DATE, date);
args.put(KEY_SUBJECT, subject);
args.put(KEY_VALUE, value);
return db.update(DATABASE_TABLE_MARKS, args, KEY_ROWID + "=" + rowId, null) >0;
}
public void deleteMark() {
db.delete(DATABASE_TABLE_MARKS, null, null);
}
public long insertSubject (String subject) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_SUBJECT, subject);
return db.insert(DATABASE_TABLE_SUBJECTS, null, initialValues);
}
public boolean updateSubject(long rowId, String subject) {
ContentValues args = new ContentValues();
args.put(KEY_SUBJECT, subject);
return db.update(DATABASE_TABLE_MARKS, args, KEY_ROWID + "=" + rowId, null) >0;
}
public Cursor getSubject(long rowId) throws SQLException {
Cursor mCursor =
db.query(true, DATABASE_TABLE_SUBJECTS, new String[] {
KEY_ROWID,
KEY_SUBJECT
},
KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getAllSubjects() {
return db.query(DATABASE_TABLE_SUBJECTS, new String[] {
KEY_ROWID,
KEY_SUBJECT
}, null, null, null, null, null);
}
}
就像现在一样,标记活动显示任何主题的所有标记,即使标题说明了其他内容。我想使用我的数据库的“主题”列过滤ListView,以便只显示数据库的项目,这些项目与标题所说的主题相同。我开始被互联网上找到的所有不同解决方案所震撼,我无法将其中的任何一个适应我的问题......
提前谢谢
答案 0 :(得分:1)
也许在DBAdapter中创建这样的方法,并在SimpleCursorAdapter中使用它而不是db.getAllMarks(),并传入选择。
public Cursor getMarks(String subject) {
return db.query(DATABASE_TABLE_MARKS, new String[] {
KEY_ROWID,
KEY_DATE,
KEY_SUBJECT,
KEY_VALUE
}, KEY_SUBJECT + " = ?", subject, null, null, null);
}