Search code examples
androidandroid-listviewrsssharelistadapter

How to invoke button from inside ListItem?


I have create a LinearLayout for a list item with the following XML code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="normal|bold"
        android:textSize="16dip"
        android:padding="5dip"
        android:text="Title"
        android:textColor="#001a90" />

    <TextView
        android:id="@+id/txtDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="18dip"
        android:padding="5dip"
        android:text="Description"
        android:textColor="#000000"
        android:textIsSelectable="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:onClick="myClickHandler" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:id="@+id/separator1"
        android:visibility="visible"
        android:background="@android:color/darker_gray"/>

</LinearLayout>

The below Layout is the XML which contains the list:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/wallpaper2a"
    android:id="@+id/rlMain">

    <Button
        android:layout_width="30dp"
        android:layout_height="50dp"
        android:text="A-"
        android:id="@+id/btn20"
        android:textSize="20dp"
        android:background="@android:color/transparent"
        android:textColor="#000000"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:textStyle="bold"
        android:nestedScrollingEnabled="false" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="A+"
        android:id="@+id/btn30"
        android:textSize="30dp"
        android:onClick="setTextSizeUp"
        android:background="@android:color/transparent"
        android:textColor="#000000"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/btn20"
        android:textStyle="bold" />

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_alignEnd="@+id/btn20"
        android:layout_below="@+id/btn20"
        android:layout_above="@+id/adView" />

    <ImageButton
        android:layout_width="25dp"
        android:layout_height="50dp"
        android:id="@+id/imageButton2"
        android:background="@android:color/transparent"
        android:src="@drawable/up"
        android:onClick="scrollListUp"
        android:layout_above="@android:id/list"
        android:layout_toStartOf="@+id/imageButton3" />

    <ImageButton
        android:layout_width="25dp"
        android:layout_height="50dp"
        android:id="@+id/imageButton3"
        android:background="@android:color/transparent"
        android:src="@drawable/down"
        android:onClick="scrollListDown"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/imageButton6" />

    <ImageButton
        android:layout_width="40dp"
        android:layout_height="50dp"
        android:id="@+id/imageButton6"
        android:background="@android:color/transparent"
        android:onClick="setTextColor"
        android:layout_above="@android:id/list"
        android:layout_toStartOf="@+id/btn30"
        android:src="@drawable/t" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

The list is populated from rss feed, which the below code will show and what I need to be able to do is share the text when a button is clicked. I want to use the button so I can add multiple share options such as facebook, email or whatsapp.

private void retrieveRSSFeed(String urlToRssFeed,ArrayList<RSSItem> list)
    {
        try
        {
            URL url = new URL(urlToRssFeed);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader xmlreader = parser.getXMLReader();
            srcRSSParser theRssHandler = new srcRSSParser(list);

            xmlreader.setContentHandler(theRssHandler);

            InputSource is = new InputSource(url.openStream());

            xmlreader.parse(is);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private class RetrieveRSSFeeds extends AsyncTask<Integer, Void, Void>
    {
        private ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Integer... params) {
            retrieveRSSFeed("..........................",itemlist);

            rssadaptor = new RSSListAdaptor(rssfeedFragment.this.getActivity(), R.layout.rssitemview,itemlist,params[0]);

            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(
                    rssfeedFragment.this.getActivity(), null, "...");

            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Void result) {
            setListAdapter(rssadaptor);

            progress.dismiss();

            super.onPostExecute(result);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }

    private class RSSListAdaptor extends ArrayAdapter<RSSItem> {
        private List<RSSItem> objects = null;
        private Integer txtSize;

        public RSSListAdaptor(Context context, int textviewid, List<RSSItem> objects,Integer txtSize) {
            super(context, textviewid, objects);

            this.objects = objects;
            this.txtSize = txtSize;
        }

        @Override
        public int getCount() {
            return ((null != objects) ? objects.size() : 0);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public RSSItem getItem(int position) {
            return ((null != objects) ? objects.get(position) : null);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;

            if(null == view)
            {
                LayoutInflater vi = (LayoutInflater)rssfeedFragment.this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.rssitemview, null);
            }

            RSSItem data = objects.get(position);

            if(null != data)
            {
                TextView title = (TextView) view.findViewById(R.id.txtTitle);
                //TextView date = (TextView)view.findViewById(R.id.txtDate);
                TextView description = (TextView) view.findViewById(R.id.txtDescription);

                title.setText(data.title);
                //date.setText("on " + data.date);
                description.setText(data.description);
                description.setTextSize(txtSize);
                if (txtColor == 0)
                {
                    description.setTextColor(Color.BLACK);
                }else
                {
                    description.setTextColor(Color.WHITE);
                }
            }

            return view;
        }
    }

Solution

  • One way would be, to create a custom list adapter. Therefor you must extend your adapter class with an ArrayAdapter (for example).

    Then in your getView(...) you can assign a onClickListener to the button of each row:

    public class EventListAdapter extends ArrayAdapter<EventEntry> {
    
    Context context;
    List<EventEntry> itemList;
    int layoutResID;
    
    public EventListAdapter(Context context, int layoutResourceID, List<EventEntry> listItems) {
        super(context, layoutResourceID, listItems);
        this.context = context;
        this.itemList = listItems;
        this.layoutResID = layoutResourceID;
    }
    
    public void setCourseView(boolean courseView) {
        this.courseView = courseView;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
    
        ItemHolder itemHolder;
    
        if (convertView == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            itemHolder = new ItemHolder();
    
            convertView = inflater.inflate(layoutResID, parent, false);
            itemHolder.button = (Button) convertView.findViewById(R.id.button);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }
        EventEntry item = (EventEntry) this.itemList.get(position);
    
        itemHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    //your stuff goes here. Maybe a callback to your activity
            }
        });
    ...
    

    For more information about Custom Adapters: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown

    Also see section 9.4 for the view holder pattern, as used in my sample code.