I am not sure I am doing this right, with my limited knowledge of this programming language… or any for that matter. Let’s see if I can make this make sense so I can get some help.
I am running a query against my database to pull out all entries in the database
Part of DBAdapter.java:
//***RETRIEVES ALL THE FINALSCORES***//
public Cursor getAllFinalscores()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_DATE,
KEY_FINALSCORE,
KEY_HOLESPLAYED},
null, null, null, null, null);
}
I have then created a separate class which extends ListActivity (because I want all of the contents to be displayed in a listview.
PastGames.java
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class PastGames extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pastgames);
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAllFinalscores();
if (c.moveToFirst())
{
do {
String[] listData = new String [] {c.getString(1),
c.getString(2), c.getString(3)};
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.pastgames, listData));
}
while (c.moveToNext());
}
db.close();
}
}
and then I am calling that from my Main activity (which just to note, does not extend ListActivity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.past_games:
Intent intentListGames = new Intent().setClass( this, PastGames.class );
this.startActivityForResult( intentListGames, 0 );
return true;
}
return true;
}
So, if I understand this correctly, I am calling PastGames.java from my main activity (which then launches PastGames.java) and within PastGames.java – onCreate I am opening the db, running my query, closing and hopefully putting into listview
I get the following in LogCat when I run that option in emulator:
06-13 22:10:36.581: ERROR/AndroidRuntime(204): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deckert/com.deckert.PastGames}: java.lang.ClassCastException: android.widget.TextView
06-13 22:10:36.581: ERROR/AndroidRuntime(204): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-13 22:10:36.581: ERROR/AndroidRuntime(204): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-13 22:10:36.581: ERROR/AndroidRuntime(204): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
Could someone please help me understand why this isnt working?
Thank you in advance and I apologize for the huge post!
You are using the same layout in both your
ArrayAdapterand your PastGamesActivityroot view. I imagine that you do not want to use theListView‘s layout for your Activities layout. I think this is what is causing theClassCastException, because yourListView‘s layout is probably defined to use just a simpleTextView, and the Activity is blowing up saying that it cannot cast aListViewto aTextView.