onItemClick View object to TextView Cast error

Put your problem here if it does not fit any of the other categories.

onItemClick View object to TextView Cast error

Postby Raskolnikov » Thu Jun 03, 2010 3:32 pm

I am trying to understand the onItemClick method a bit more. I created this simple test to try to get the jist but am having trouble.

I recieve an error: java.lang.ClassCastException

it happens at the line 8 when i try to cast the "view" object to a TextView. Is there other way to derive the text that is being displayed by the list of a particular item that is clicked?


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mContactList.setOnItemClickListener(new OnItemClickListener() {
  2.                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {      
  3.                         // When clicked, show a toast with the TextView text      
  4.                         //Toast.makeText(AddContactTest20.this, ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
  5.                         TextView resultText = (TextView)findViewById(R.id.tvResult);
  6.                
  7.                         try {
  8.                                         resultText.setText(((TextView) view).getText().toString());
  9.                                 } catch (Exception e) {
  10.                                         Log.e("Line 57", e.getMessage()+" - "+e.getLocalizedMessage()+" - "+e.toString());
  11.                                 }
  12.                 }  
  13.         });
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I can provide more code if needed.

Ras
Raskolnikov
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue May 18, 2010 4:54 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Alexa [Bot], Exabot [Bot] and 21 guests