i am able to display a listview from an array.But when i click on any of the row aplication crash开发者_StackOverflow社区es saying ClassCaste Exception android.widget.linearlayout. my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:id="@+id/icon" android:layout_height="wrap_content"
android:src="@drawable/icon" android:layout_width="22px"
android:layout_marginTop="4px" android:layout_marginRight="4px"
android:layout_marginLeft="4px">
</ImageView>
<TextView android:text="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/label"
android:textSize="30px"></TextView>
specific code where error could exist:
ArrayAdapter<String> countryAdapter=new ArrayAdapter<String>(this,R.layout.rowlayout,R.id.label,countries);
setListAdapter(countryAdapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(this);
and onclick method ,where i am trying to show toast on the clickd item from the list view:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView)linearlayout.findViewById(R.id.label)).getText(), Toast.LENGTH_SHORT).show();
}
application is crashing when i am clicking on any of the list item.
any suggestions? Thanks
Replace
((TextView)linearlayout.findViewById(R.id.label)).getText()
by
((TextView)view.findViewById(R.id.label)).getText()
Hope it works :)
精彩评论