[android-developers] Re: ListView.setOnItemClickListener() fails to catch selection events for custom list items
Thanks for your suggestion, Filip. I added the following to the
ListView:
public void onListItemClick (ListView l, View v, int position,
long id)
{
item_index = position;
Toast.makeText(this, "item clicked = " + position,
Toast.LENGTH_LONG).show();
}
But it didn't get executed when I selected an item in the list (the
Toast never got displayed).
On Nov 4, 1:03 am, Filip Havlicek <havlicek.fi...@gmail.com> wrote:
> Hi Bob,
>
> since you are using ListActivity, you can override onListItemClick instead
> of creating new OnItemClickListener. More information here
>
> http://developer.android.com/reference/android/app/ListActivity.html#...,
> android.view.View, int, long)
>
> That should fix your problem.
>
> Best regards,
> Filip Havlicek
>
> 2010/11/4 FractalBob <ruom...@gmail.com>
>
>
>
>
>
>
>
> > I thought I knew how to catch list selection events in Android and
> > then I regained consciousness. In my situation, I have a ListView that
> > I populate with a custom view, all of the same class (let's call it
> > the NewsChannel() class). NewsChannel consists of a LinearLayout of
> > ImageViews and TextViews. When the list is displayed, the user may
> > select a NewsChannel item in the list; I want to find out which list
> > item was selected.
>
> > Also, I registered an onClickListener to one of the TextViews and that
> > works fine: when the text is clicked on, an activity is launched which
> > is looking for the position of the NewsChannel item in the list.
>
> > So, here's what my code looks like:
>
> > 1. Registering the onClickListener in the ListActivity:
>
> > public class NewsListActivity extends ListActivity
> > {
> > .
> > .
> > .
> > public void onCreate(Bundle icicle)
> > {
> > super.onCreate(icicle);
> > setContentView(R.layout.news_list_format);
>
> > ListView lv = getListView();
>
> > lv.setOnItemClickListener(new OnItemClickListener() {
> > public void onItemClick(AdapterView<?> parent, View view,
> > int position, long id) {
> > // When clicked, show a toast with the TextView text
> > item_index = position;
> > Toast.makeText(getApplicationContext(), "item clicked =
> > " + position, Toast.LENGTH_SHORT).show();
> > }
> > });
>
> > .
> > .
> > .
> > 2. The layout for the ListView associated with NewsListActivity:
>
> > <?xml version="1.0" encoding="UTF-8"?>
> > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
> > android"
> > android:orientation="vertical"
> > android:layout_width="fill_parent"
> > android:layout_height="fill_parent"
> > android:background="@drawable/wpaper_tile"
>
> > <ListView android:id="@id/android:list"
> > android:layout_width="fill_parent"
> > android:layout_height="fill_parent"
> > android:layout_alignParentTop="true"
> > android:dividerHeight="15dp"
> > android:divider="@android:color/transparent"
> > android:cacheColorHint="@android:color/transparent"
> > android:scrollbars="none"
> > />
> > <FrameLayout
> > android:layout_width="fill_parent"
> > android:layout_height="55dp"
> > android:layout_centerHorizontal="true"
> > android:layout_alignParentBottom="true"
> > android:background="#88000000"
> > />
> > <ImageView
> > android:layout_width="fill_parent"
> > android:layout_height="wrap_content"
> > android:layout_centerHorizontal="true"
> > android:layout_alignParentBottom="true"
> > android:src="@drawable/phonetop_bg"
> > />
> > </RelativeLayout>
>
> > 3. The NewsChannel layout:
>
> > <?xml version="1.0" encoding="UTF-8"?>
> > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
> > android"
> > android:layout_width="fill_parent"
> > android:layout_height="wrap_content">
>
> > <include layout="@layout/news_heading_format"
> > android:id="@+id/news_heading_header"/>
>
> > <ImageView android:id="@+id/news_channel_image"
> > android:layout_width="88dp"
> > android:layout_height="66dp"
> > android:layout_alignParentLeft="true"
> > android:layout_below="@id/news_heading_header"
> > android:padding="10dp" />
>
> > <TextView android:id="@+id/news_channel_text"
> > android:lines="4"
> > android:inputType="textMultiLine"
> > android:layout_toRightOf="@id/news_channel_image"
> > android:layout_below="@id/news_heading_header"
> > android:layout_width="fill_parent"
> > android:layout_height="wrap_content"/>
> > </RelativeLayout>
>
> > When I run the app, the ItemClickListener is never called. However, if
> > NewsChannel were of a simpler type, say, a single ImageView or
> > TextView embedded in the layout, the callback does get called.
>
> > Any idea what I'm doing wrong? My whole goal is simply to let the
> > activity launched by NewsChannel (not shown) to find out which
> > instance of NewsChannel it was launched from. Thanks, in advance!
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscribe@googlegroups.com<android-developers%2Bunsubs cribe@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home