データベースからいくつかのテキストビューにデータを取得し、それらのテキストビューをリストビューに入れたいときに、いくつかの問題に直面しました.データベースに日付とヘッドのような2つの列があることを説明しましょう.そして、それらをそれぞれのテキストビューに取得したいこれは他の人にとってはばかげた質問かもしれませんが、私はこの分野では初めてです.SimpleCursorAdapterについては知っていますが、使用方法と使用場所がわかりません.だから私を導いてください。
My Classes::database.MyArrayAdapter クラスの DBAdapter クラスを作成しました。そしてメインクラスは Head.java として呼び出します。
DBAdapter.java
public class DBAdapter {
//EditText mHead;
public static final String KEY_ROWID="_id";
public static final String KEY_DATE="date";
public static final String KEY_HEAD="head";
private static final String TAG="DBAdapter";
private static final String DATABASE_NAME="accounting";
private static final String DATABASE_TABLE="accounts";
private static final int DATABASE_VERSION=1;
private static final String DATABASE_CREATE=
"create table accounts (_id integer primary key autoincrement," +
"date datetime not null, head text not null);";
private final Context context;
private DataBaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx){
this.context=ctx;
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) {
// TODO Auto-generated method stub
try {
db.execSQL(DATABASE_CREATE);
}catch(SQLException e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Updrading database from version "+oldVersion+" to "+newVersion+"," +
"which will destroy all old data");
db.execSQL("drop table if exists accounts");
onCreate(db);
}
}
public DBAdapter open() throws SQLException{
db=DBHelper.getWritableDatabase();
return this;
}
public void close(){
DBHelper.close();
}
**//--insert contact in the database--**
public long insertContact(String date,String head)
{
ContentValues initialvalue=new ContentValues();
initialvalue.put(KEY_DATE, date);
initialvalue.put(KEY_HEAD, head);
return db.insert(DATABASE_TABLE, null, initialvalue);
}
//--delete a particular contact--
public boolean deleteContact(long rowId){
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null)>0;
}
//--Retrieving all the contacts--
public Cursor getAllContacts(){
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DATE,KEY_HEAD}, null, null, null, null,null);
}
//--Retrieve a particular contact--
public Cursor getContact(long rowId)throws SQLException{
Cursor mCursor=
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_DATE,KEY_HEAD},
KEY_ROWID + "="+ rowId, null, null, null,
null, null);
if(mCursor != null){
mCursor.moveToFirst();
}
return mCursor;
}
//--update a contact--
public Boolean updateContact(long rowId,String date,String head){
ContentValues args=new ContentValues();
args.put(KEY_DATE, date);
args.put(KEY_HEAD, head);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" +rowId,null)>0;
}
}
MyArrayAdapter.java
public class MyArrayAdapter extends ArrayAdapter<Object>{
private final Context context;
private final String[] values;
//@SuppressWarnings("unchecked")
public MyArrayAdapter(Context context,String[] values){
super(context,R.layout.list_things,values);
this.context=context;
this.values=values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflate=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowview=inflate.inflate(R.layout.list_things, parent, false);
TextView mDate=(TextView)rowview.findViewById(R.id.tvdate);
TextView mHead=(TextView)rowview.findViewById(R.id.tvhead);
mDate.setText(values[position]);
mHead.setText(values[position]);
return rowview;
}
}
やっと全部やりたいヘッドクラス
Head.java
public class Head extends ListActivity {
String date = "";
String head;
TextView display;
EditText headname;
ListView list;
LayoutInflater inflate;
Layout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.head);
display=(TextView)findViewById(R.id.tvCurrentDate);
display.setText(date);
// Intent intent=new Intent();
DBAdapter db=new DBAdapter(this);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(Head.this, R.layout.list_things, null, null, null);
//Cursor cursor=getContentResolver().query("/data/data/com.crypto.ranjit/DBAdapter",
//new String []{db., selection, selectionArgs, sortOrder)
Bundle b = getIntent().getExtras();
if (b != null) {
date = b.getString("date");
}
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
headname=(EditText)findViewById(R.id.etHeadname);
date=display.getText().toString();
head=headname.getText().toString();
DBAdapter dba = new DBAdapter(Head.this);
dba.open();
long id = dba.insertContact(date, head);
// id=dba.insertContact(head, date);
dba.close();
}
});
//DBAdapter db=new DBAdapter(Head.this);
//db.open();
}
}