3

I'm trying to print the value that has been clicked on in the listView, but then i'm getting the following exception:

07-04 10:40:56.482: E/AndroidRuntime(1356): FATAL EXCEPTION: main
07-04 10:40:56.482: E/AndroidRuntime(1356): java.lang.ClassCastException:      android.widget.LinearLayout cannot be cast to android.widget.TextView
07-04 10:40:56.482: E/AndroidRuntime(1356):     at com.passwordkeeper.ui.ActivityHomeScreen$1.onItemClick(ActivityHomeScreen.java:88)

Here is a code snippet:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTextView = (TextView) findViewById(R.id.labelList);
    db = new DatabaseHandler(this);
    createList();
    displayList();
}   

The createList function is working properly. Here is my displayList method:

public void displayList(){
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_home_screen, R.id.listTextView, mAccountNames));
    mListView = getListView();
    mListView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            String product = ((TextView) view).getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });
}

My xml code for the file activity_home_screen.xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
    android:id="@+id/listTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="26dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />  

Any help would be useful!

Thank you.

3
  • post your xml code here.. Commented Jul 4, 2013 at 5:21
  • mTextView should be TextView Type Commented Jul 4, 2013 at 5:21
  • view which returns text as string product, in onItemClick method is giving you classcast excpetion, check for it. Commented Jul 4, 2013 at 5:24

4 Answers 4

4

You can try this

mListView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            TextView txtview = (TextView)view.findViewById(R.id.listTextView);
            String product = txtview.getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Use this:

String product = ((TextView) view.findViewById(R.id.listTextView)).getText().toString();

Comments

1

The View you are getting inside OnItemClickListener will return the parent layout ,That is LinearLayout in your case.

you can get your textview by doing following :

mListView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            TextView txtview = (TextView)view.findViewById(R.id.listTextView);
            String product = txtview.getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });

Comments

0
mListView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            TextView textview= (TextView)view.findViewById(R.id.TEXTVIEW_ID);
            String product = textView.getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });

Replace "TEXTVIEW_ID" with the id of textview you have given in list view item xml(If not given, give one).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.