当我想将数据从数据库检索到一些文本视图然后将这些文本视图放入列表视图时,我遇到了一些问题。让我解释一下我的数据库中有两列,如日期和标题。我想将它们检索到它们的每个文本视图中并将这些文本放入列表视图。我知道这对其他人来说可能是一个愚蠢的问题,但我是这个领域的新手。我对 SimpleCursorAdapter 有所了解,但不知道如何以及在哪里使用它。它可能会通过这个来解决。所以请指导我。
我的类::我为 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;
}
}
最后是我想做所有事情的头类
头.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();
}
}