Search code examples
androidlistviewonitemclick

getting the position onItemClick implementation in LinearLayout


I am trying to show a ListView and my Java class extendsListFragment. While setting setOnItemClickListener on ListItem, the click Listener doesn't seem to work. Here is my code:

ListView_rssfeeds_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/relativeLayout"
    tools:context=".interestingReads.RSSFeedsTab">
    <ListView
        android:id="@+id/rssFeedsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/diffBackgroundWhite"
        />

</RelativeLayout>

rss_item_list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rssFeeds_LL_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="12dp"
    android:layout_marginBottom="12dp"
    android:orientation="vertical"
    android:clickable="true"
    >
<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="6dp"
    >

    <ImageView
        android:id="@+id/rssFeeds_Image_list_view"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="@string/app_name"
        android:padding="0dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="7dp">

        <TextView
            android:id="@+id/rssFeeds_Title_list_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/rssFeeds_Description_list_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:textSize="12sp" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>

RSSFeedsTab.java (Please NOTE: RSSFeedsTab is Fragment and not Activity)

public class RSSFeedsTab extends ListFragment implements  OnItemClickListener {
.
.
.
@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
.
.
.
}

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
.
.
.
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
        setListAdapter(simpleAdapter);

        View RootView =  getActivity().getLayoutInflater().inflate(R.layout.fragment_rssfeeds_tab, container, false);
        ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);




        RSSFeedsItemLL.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
                System.out.println("********Reached onClick*********");
                Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
            }
        });
        return super.onCreateView(inflater, container, savedInstanceState);


    }

With this much implementation, I am able to get the ListView rssFeeds_LL_list_view. I am not sure, how I can use click event on those ListView such that I can get position in ListView ?


Solution

  • Finally I have resolved the issue by overriding method getView()

    So, I changed only the line: SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);

    to

    SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to){
        @Override
        public View getView (int position, View convertView, ViewGroup parent)
        {
            View v = super.getView(position, convertView, parent);
            final int finalposition = position;
            LinearLayout RSSFeed_singleItem=(LinearLayout) v.findViewById(R.id.rssFeeds_LL_list_view);
            RSSFeed_singleItem.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getActivity(),"Link:==>"+localLinkArray[finalposition],Toast.LENGTH_SHORT).show();
                }
            });
            return v;
        }
    };