2

I'm trying to type cast data but when I write TextView clickData=(TextView) view under onItemClick(...) logcat shows msg:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

I don't know why.

This belongs from main activity:

  public class ListViewForAllContact extends AppCompatActivity {
        ListView myListView;
        protected  void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listviewforallcontact);



            ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());

            Cursor mCursor= onbOfContactDatabase.phoneName();

            String[] fromFileName=new String[]{ContactDatabase.NAME};
            int[] toViewId=new int[]{R.id.textView5};
            SimpleCursorAdapter simpleCursorAdapter;

            simpleCursorAdapter=new SimpleCursorAdapter(this,R.layout.forreading,mCursor,fromFileName,toViewId,0);
             myListView=(ListView)findViewById(R.id.listView);
            myListView.setAdapter(simpleCursorAdapter);

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

                    TextView clickData=(TextView) view;//becouse of this line error will come.

                    Toast.makeText(getBaseContext(), clickData.getText(), Toast.LENGTH_LONG).show();
                }
            });

        }
    }

This is xml with ListView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

This is second XML file with TextView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView5"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"/>





</RelativeLayout>

2 Answers 2

1

That view you want to access its the RelativeLayout you show on second xml. If you want to access the TextView you have to find it on that RelativeLayout.

Instead of this

TextView clickData=(TextView) view;

try this

TextView clickData=(TextView) view.findViewById(R.id. textView5);

I think this resolves your problem

Updated...

My suggestion is to use an Adapter

public class YourAdapter extends BaseAdapter {

private ArrayList<Things> things;
private Context context;
private TextView yearTxtView;
private TextView weekTxtView;
private TextView extraTxtView;


public YourAdapter(Context context, ArrayList<Things> things) {
    this.things = things;
    this.context = context;
}

@Override
public int getCount() {
    return this.things.size();
}

@Override
public Object getItem(int position) {
    return this.things.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView =     LayoutInflater.from(this.context).inflate(R.layout.ITEM_LAYOUT, parent, false);

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do your code here
        }
    });

    return convertView;


}

}

and on that code you have, just do this

private YourAdapter yourAdapter;

this.yourAdapter = new YourAdapter(getActivity(),YourArray);

myListView.setAdapter(this.yourAdapter);

Sign up to request clarification or add additional context in comments.

9 Comments

why view is RelativeLayout ?
Its relative layout because that is the view that is surrounding your textview, its not getting the clicking on the textview its getting the click on that View/Cell not the item inside of it.
ok I update my Answer if its hard to understand say it but its the best way to control listviews and there items.
@Francisco Castro how can i get clicked item
on both situations you have the position (argument), so when you click on the item you know the position on the array
|
0

It seems like the Android compiler has become very specific. I encountered the same problem, and it is a pity because this is the kind of code that is on every sample program and Android developer forum or guide. The solution is to treat the compiler by feeding it exactly what it is asking for and in this case the textview (android.widget.TextView).

REMOVE the layout frame (LinearLayout, RelativeLayout, etc) around the TextView and trust that on inflating within the ListView parent widget, it will "find its way in the code world". So, set it free.

So you can define the properties of the TextView as you need, DECLARE the android schema (xmlns:android="http://schemas.android.com/apk/res/android") in the TextView itself. After this, the compile won't be confused and everything should compile.

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.