件名とマークをデータベースに保存できるアプリをプログラムしました。私が解決しなければならない最後の問題は、MarksクラスからListViewをフィルタリングして、対応するサブジェクトのみを表示する方法です。
アプリは3つのクラスで構成されています。
主要:
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);
}
}
現在のように、マークアクティビティには、タイトルに別の内容が記載されている場合でも、あらゆる主題のすべてのマークが表示されます。データベースの「subject」列を使用してListViewをフィルタリングし、タイトルと同じ件名のデータベースのアイテムのみが表示されるようにします。私はインターネット上で見つけたさまざまな解決策に圧倒され始めており、それらのいずれも私の問題に適応させることができません...
前もって感謝します