xyz rety xyz rety - 5 months ago 20
MySQL Question

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

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 plz help me.

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>

Answer

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);