This is my code For activity.I have just begun to learn android. And i am not able to get any data through the SimpleCursorAdaptor on my ListView When I m using the ArrayAdaptor then i am able to get data on the ListView but not With Simple Cursor Adaptor
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();
}
}
Launcher activity layout.I only have a list view in the XML is this the problem ?
<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>
This line is your problem.
int[] v={R.id.TaskList,R.id.TaskList,R.id.TaskList,R.id.TaskList};
You have to map the values from your cursor to Textfields in your list item layout. Not in your layout that countains the list view.
Atm you are using a generic layout for your list items (android.R.layout.simple_list_item_1) Try making your own. (simple xml layout containing 1 TextView for every value you want to display) Then:
SimpleCursorAdapter sa=new SimpleCursorAdapter(this,R.layout.my_list_item ,c,fromCol,v,0);
and
int[] v={R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4};