0

I have an array of classes from a database being displayed in a listview. When a row is clicked on the listview I want all the data from that instance of the class to be displayed in textviews elsewhere on the page. The class I am using is a Player class which also includes a playerid.

I have tried something like this but it does not work, any help would be great thanks!

     playerlistview.setOnItemClickListener(new OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> listView, View view,
         int position, long id) {

     Player selplayer =  (Player) playerlistview.getAdapter().getItem(position);

     String playername = selplayer.name;
     String playerposition = selplayer.position;
     String playerdob = selplayer.dob;
     String playergoals = selplayer.goals;
     String playerpoints = selplayer.points;

     txtplayername.setText(playername);
     txtplayerposition.setText(playerposition);
     txtplayerdob.setText(playerdob);
     txtplayergoals.setText(playergoals);
     txtplayerpoints.setText(playerpoints);

}
      });



AndroidRuntime(793): java.lang.ClassCastException: com.example.corkgaaapp.Squad$Player cannot be cast to android.database.Cursor
AndroidRuntime(793): at com.example.corkgaaapp.Squad$1.onItemClick(Squad.java:97)
4

1 回答 1

0

您可能可以从字符串名称创建您的类的实例:

    String selplayer =  ((TextView) playerlistview.getItemAtPosition(position)).getText().toString();
    Player myPlayer = (Player)Class.forName(selPlayer);

我不知道您拥有什么类型的数据库,但您可以检索数据并创建一个新实例

    int position = 42; //position is the row user clicked in the list view
    String[] playerinfo = new String[5];

    /* database retrieval here for position into array playerinfo*/

    txtplayername.setText(playerinfo[0]);
    txtplayerposition.setText(playerinfo[1]);
    txtplayerdob.setText(playerinfo[2]);
    txtplayergoals.setText(playerinfo[]3);
    txtplayerpoints.setText(playerinfo[4]);

    myPlayer = new Player(playerinfo[0], playerinfo[1].....);
于 2013-06-13T14:50:15.640 回答