Android App Development | Lecture#39 | Hive Learners

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Dear Hive Learners, I hope you all are well. In lecture 38th we learn how to implement a Custom Adapter and populate ListView by using a model class. Today we will learn how to implement the OnClickListenere interface that we can use in our Blogs_Activity.java code. We can also implement the simple click listeners in the Adapter class with the button but we need to learn how to use the interface to do some useful tasks.

GitHub Link

Use this GitHub project to clone into your directory. The following lecture will constantly update it so you will never miss the latest code. Happy Coding!

What Should I Learn

  • How to use the interface
  • How to delete the item from the list

Assignment

  • Implement the OnClickListener

Procedure

First, we need to declare the ItemClickListener and implement the Interface like this in the Adapter class. I have also added the ArrayList of the type Model class. and initialize in the constructor with the items.

ArrayList<MyList_POJO> arrayList;
    onListItemClickListener mListener;

    public interface onListItemClickListener {
        void onDeleteClick(int position);
    }

Now on the delete button click listeners, we need to pass our interface and the mListener that we have already declared. We will also set a check if delete_btn is not null.

Coding in the Adapter class is done now we need to implement it in our Blogs_Activity.java class. Implement the Adapter Class interface and override the method provided by this interface.

Here in this override, we can use the delete button to click the listener trigger. When a user clicks on the delete button we will get the position of this delete button in the listview, and with the position, we can do anything. Like if we want to delete that item we can remove it from the Model class at that position.


hl_divider.png

Thank You

hl_footer_banner.png



0
0
0.000
4 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000
avatar

Thanks for this interface step for mobile programing
!1UP

You can earn passive income by delegation of tribe tokens to "The Cartel".

dlmmqb-TheCartel-banner
Click this banner to join "The Cartel" discord server to know more.

0
0
0.000