0

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 ?

Om Sao
  • 7,064
  • 2
  • 47
  • 61

3 Answers3

1

you have to use the listener OnItemClickListener to get the item which was clicked : OnItemClickListener on Android developer

****Update****

In your code, you are using a ListFragment that already contains a Listview.

SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.rss_item_list_row, from, to);
setListAdapter(simpleAdapter);

With this lines, you create an Adapter and give this adapter to the Fragment's ListView. Then you recover the RSSFeedsItemLL ListView from your layout and you set the OnItemClickListener on it :

ListView RSSFeedsItemLL = (ListView) RootView.findViewById(R.id.rssFeedsFragment);
RSSFeedsItemLL.setOnItemClickListener(new AdapterView.OnItemClickListener(){...}

But this ListView is never linked to an adapter.

You have two solutions :

  • Replace the ListFragment with a Fragment in your class definition and use the ListView in your layout file

  • Use the RSSFeedsItemLL ListView in your fragment

For the second case, you just have to override the method onListItemClick in your RSSFeedsTab class :

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    System.out.println("********Reached onClick*********");
    Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
    super.onListItemClick(l, v, position, id);
}

You also have to remove the line android:clickable="true" in your LinearLayout definition in 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"
>
<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="6dp"
    >
    ...
  • If you will see Fragment code in Question, I am actually trying to do the same, but no success. Please note my Java class extends to `ListFragment` and not `ListActivity` and I can't change it due to other functionalities dependencies. – Om Sao Oct 09 '18 at 00:04
  • 1
    @OmSao I updated the answer with a more detailed explanation – Stephane Rothen Oct 09 '18 at 06:29
  • Thanks for the suggestion and detailed explainations! @Stephane Rothen: – Om Sao Oct 09 '18 at 08:51
0

Use onItemClickListener on ListView view: RSSFeedsItemLL

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

References: OnItemClickListener

Om Sao
  • 7,064
  • 2
  • 47
  • 61
Lorenzo Vincenzi
  • 1,153
  • 1
  • 9
  • 26
0

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;
    }
};
Om Sao
  • 7,064
  • 2
  • 47
  • 61