2013-06-13 5 views
0

데이터베이스의 클래스 배열이리스트 뷰에 표시됩니다. 목록 뷰에서 행을 클릭하면 해당 클래스 인스턴스의 모든 데이터가 페이지의 다른 위치에있는 textviews에 표시됩니다. 내가 사용하는 클래스는 playerid도 포함하는 Player 클래스입니다.리스트 뷰를 클릭 한 후 클래스의 인스턴스를 검색하는 방법

나는 이런 식으로 시도했지만 작동하지 않는다면 어떤 도움을 주셔서 감사합니다!

 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) 
+1

정확히 "작동하지 않는"것은 무엇입니까? 그것은 추락합니까? 디버거를 사용하여 무엇을 수행했는지 확인하십시오. – Karakuri

+0

죄송합니다, 죄송합니다. 위의 로그를 고양이에 게시하십시오. –

+0

'classCastException'처럼 보이고,'playerlistview.getAdapter(). getItem (position);'Player' 객체를 반환 하시겠습니까? –

답변

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].....); 
관련 문제