SimpleCursorAdapter: could someone help with this code
I want to create a ListView (not sure if i'm doing this part right either) and populate it with rows from the game table in the database rugby.db, all of which are done in this app's onCreate() method.
When I fire the app's button, I get a:
java.lang.illegalArgumentException: column '_id' does not exist
Here is the code:
package com.gottaget.listviewsfromDB;
import android.app.ListActivity;
import android.view.View;
import android.widget.ListView;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.content.ContentValues;
import android.content.Context;
import android.widget.SimpleCursorAdapter;
import android.database.Cursor;
import android.widget.TextView;
import android.database.sqlite.*;
import android.database.SQLException;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.LayoutInflater;
public class SecondListView extends ListActivity {
Button retrieve=null;
TextView tv1;
TextView tv2;
ListView lv;
SimpleCursorAdapter adapter;
String []fromColumns=null;
int []toLayoutIDs=null;
Context myContext=null;
private static String DBNAME= "rugby.db";
SQLiteDatabase rugby=null;
private static String createGameTable="create table Game (Game_id integer Primary Key, " + "date_of_game text, year_of_game text, home_team text, visiting_team text, final_score text);";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// setContentView(R.layout.listview);
myContext=this.getApplicationContext();
//open a connection to the rugby.db
rugby=openOrCreateDatabase(DBNAME, SQLiteDatabase.OPEN_READWRITE, null);
rugby.setLockingEnabled(true);
retrieve=(Button) findViewById(R.id.button1);
tv1=(TextView) findViewById(R.id.textView1);
tv2=(TextView) findViewById(R.id.textView2);
fromColumns=new String[]{"Game.Game_id"};
toLayoutIDs=new int[]{R.id.textView1};
// lv=(ListView)findViewById(R.id.list);
if(checkForGAMETable()==false){
rugby.execSQL(createGameTable);
}else{
Toast.makeText(myContext,"game table exists",Toast.LENGTH_SHORT).show();
}//ends checkForGAMETable condition
//insert into Game(date_of_game, year_of_game, home_team, visiting_team, final_score) values ("May 22", "2002", "SJHS", "FHS", "42H 16V");
//populate the first row in the Game table
ContentValues newGameValues = new ContentValues();
newGameValues.put("date_of_game", "May 22");
newGameValues.put("year_of_game", "2002");
newGameValues.put("home_team", "SJHS");
newGameValues.put("visiting_team", "HTHS");
newGameValues.put("final_score", "28H 10V");
rugby.insert("Game", null, newGameValues);
//populate the second row in the Game table
ContentValues newGameValues2 = new ContentValues();
newGameValues2.put("date_of_game", "May 29");
newGameValues2.put("year_of_game", "2002");
newGameValues2.put("home_team", "SJHS");
newGameValues2.put("visiting_team", "FHS");
newGameValues2.put("final_score", "42H 10V");
rugby.insert("Game", null, newGameValues2);
retrieve.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Cursor c=rugby.query("Game", null, null, null, null, null, null);
c.moveToFirst();//returns the first row in the query
tv2.setText(c.getString(0).toString());
tv2.append(" "+c.getString(1).toString());
tv2.append(" "+c.getString(2).toString());
tv2.append(" "+c.getString(3).toString());
tv2.append(" "+c.getString(4).toString());
tv2.append(" "+c.getString(5).toString());
SimpleCursorAdapter adapter=new SimpleCursorAdapter(myContext, R.layout.listviewrow, c, fromColumns, toLayoutIDs);
}//ends onClick()
});//ends retrieve.setOnClickListener()
}//ends onCreate()
public boolean checkForGAMETable(){
Cursor c=rugby.query("sqlite_master", null, "type=? and name=?", new String[]{"table", "Game"}, null, null, null);
if(c.getCount()>0){
return true;
}else{
return false;
}//ends if()...else
}//checkForGAMETable()
}//ends SecondListView class
THIS IS THE layout file main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 2" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retrieve Game from Database" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
THIS IS THE listview.xml FILE
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent" android:dividerHeight="1px"
android:layout_height="fill_parent"
android:id="@android:id/list" >
</ListView>
</LinearLayout>
THIS IS THE listviewrow.xml FILE
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_alignLeft="@+id/name" android:layout_below="@+id/name"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="12dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:layout_marginRight="4dp" android:text="My Daughter"
android:textStyle="bold" android:textSize="16dip" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:text="Sophie"
/>
</LinearLayout>
|