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


i'm trying type cast data when write textview clickdata=(textview) view under onitemclick(...) logcat shows msg:

java.lang.classcastexception: android.widget.relativelayout cannot cast android.widget.textview

i don't know why.

this belongs 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 line error come.                      toast.maketext(getbasecontext(), clickdata.gettext(), toast.length_long).show();                 }             });          }     } 

this xml 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 second xml file 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> 

that view want access relativelayout show on second xml. if want access textview have find on relativelayout.

instead of

textview clickdata=(textview) view;

try this

textview clickdata=(textview) view.findviewbyid(r.id. textview5);

i think resolves problem

updated...

my suggestion use 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 code here         }     });      return convertview;   } 

}

and on code have, this

private youradapter youradapter;

this.youradapter = new youradapter(getactivity(),yourarray);

mylistview.setadapter(this.youradapter);


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -