这是我的活动代码。我刚刚开始学习android。而且我无法通过我的 ListView 上的 SimpleCursorAdaptor 获取任何数据当我使用 ArrayAdaptor 时,我能够在 ListView 上获取数据,但不能使用 Simple Cursor Adapter
public class LaunchActivity extends Activity {
ListView taskList;
SQLiteDatabase db ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
// Log.d("Anuranjit", "IN activity");
taskList = (ListView) findViewById(R.id.TaskList);
taskList.setDividerHeight(7);
TaskDatabaseManager taskManager = new TaskDatabaseManager(
getApplicationContext());
db = taskManager.getWritableDatabase();
addData();
Cursor c=db.rawQuery("Select * from "+TaskDatabaseManager.DATABASE_TABLE, null);
c.moveToFirst();
String[] fromCol={TaskDatabaseManager.KEY_ROWID,TaskDatabaseManager.TASK_DESCRIPTION,TaskDatabaseManager.TASK_STATUS,TaskDatabaseManager.TASK_TIMESTAMP};
int[] v={R.id.TaskList,R.id.TaskList,R.id.TaskList,R.id.TaskList};
SimpleCursorAdapter sa=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1 ,c,fromCol,v,0);
taskList.setAdapter(sa);
}
public void addData(){
Date c=new Date();
db.execSQL("insert into "+TaskDatabaseManager.DATABASE_TABLE+"( "+TaskDatabaseManager.TASK_DESCRIPTION+","+TaskDatabaseManager.TASK_TIMESTAMP +","+TaskDatabaseManager.TASK_STATUS+") values ('Hello World',' "+(new Timestamp(c.getTime()))+" ' ,'0');");
Cursor ac=db.rawQuery("Select * from "+TaskDatabaseManager.DATABASE_TABLE, null);
ac.moveToFirst();
Toast.makeText(getApplicationContext(), String.valueOf(ac.getCount()), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), ac.getString(1), Toast.LENGTH_LONG).show();
}
}
启动器活动布局。我在 XML 中只有一个列表视图,这是问题吗?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LaunchActivity" >
<ListView
android:id="@+id/TaskList"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@android:color/holo_green_light" >
</ListView>