开发者

Not able in accessing textView from linearlayout using findViewById in listview in onItemClick

开发者 https://www.devze.com 2023-04-12 13:41 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消