create account

Android App Development | Lecture#38 | Hive Learners by faisalamin

View this thread on: hive.blogpeakd.comecency.com
· @faisalamin ·
$22.56
Android App Development | Lecture#38 | Hive Learners
<center><h2>𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼</h2></center>
<div class="text-justify">

Hello everyone, I hope you all are well. In our recent lecture, we learn how to populate static data in the list. We use the default list view. But most of the time we need a custom layout for the list items and also need to populate multiple values in it. So today we will create a custom layout and use it in our listview items.

![](https://images.ecency.com/DQmbVJY6FsTBEd8c2DLonUhi12j1DLmkp5DaoMqEfq7BiTR/multi_purpose.png)

### [GitHub Link](https://github.com/faisalamin9696/HiveLearners2)

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

- Add Custom Adapter
- Custom Listview

### Assignment

- Populate Custom ListView

### Procedure

First, we need to create a new layout. I set the name **my_list_item**. It creates an XML file on this name and we can write our listview item design code in it.

![](https://images.ecency.com/DQmfTN3S5MoDKwrLwarPFBoFyUBrS2KoCHNLc8gcfbGGDPv/image.png)

I create this layout with 2 TextView and a material button.

```
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:cardCornerRadius="4dp"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title_lv_item"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/subTitle_lv_item"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="SubTitle"
                android:textSize="14sp" />


        </LinearLayout>

        <com.google.android.material.button.MaterialButton
            android:id="@+id/delete_lv_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DELETE"
            app:backgroundTint="@android:color/holo_red_dark" />
    </LinearLayout>

</androidx.cardview.widget.CardView>
```

![](https://images.ecency.com/DQmV8fXWUNFb4Ji17WUcUcWhrDXv6ZZJWuzScN9omEomTJG/image.png)

Now we need to add a model class. We will add work POJO with it. It stands for  Plain Old Java Object. We will add the variable and set getter and setter of them. We will also create a constructor for it.

```
package com.example.hivelearners2;

public class MyList_POJO {
    String title, subTitle;

    public MyList_POJO(String title, String subTitle) {
        this.title = title;
        this.subTitle = subTitle;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSubTitle() {
        return subTitle;
    }

    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
    }
}

```

![](https://images.ecency.com/DQmRm1L18Bz1mYwyjwnXoqYUcJYD9TeJuirTJdnwv9bK8p5/image.png)

Now we need to create a JAVA class and we will write the code for the Custom Adapter there. I create **MyAdapter.java**. 

```
package com.example.hivelearners2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class MyAdapter extends ArrayAdapter<MyList_POJO> {

    private int resourceLayout;
    private Context mContext;

    public MyAdapter(Context context, int resource, ArrayList<MyList_POJO> items) {
        super(context, resource, items);
        this.resourceLayout = resource;
        this.mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(mContext);
            v = vi.inflate(resourceLayout, null);
        }

        MyList_POJO pojo = getItem(position);

        if (pojo != null) {
            TextView title_tv = (TextView) v.findViewById(R.id.title);
            TextView subTitle_tv = (TextView) v.findViewById(R.id.subTitle_lv_item);
            TextView delete_btn = (TextView) v.findViewById(R.id.delete_lv_btn);

            if (title_tv != null) {
                title_tv.setText(pojo.getTitle());
            }

            if (subTitle_tv != null) {
                subTitle_tv.setText(pojo.getSubTitle());
            }

        }

        return v;
    }

}
```

![](https://images.ecency.com/DQmfUbVFYd8bFppsS6yHnggTZA75pqi2enatjNSWXbKrmHE/image.png)


Now we need to the custom adapter and set the listview adapter to this adapter. We also need some random data in our POJO class.</div>

```
ArrayList<MyList_POJO> myList_pojos = new ArrayList<>();
        myList_pojos.add(new MyList_POJO("Pakistan", "Lahore"));
        myList_pojos.add(new MyList_POJO("India", "Punjab"));
        myList_pojos.add(new MyList_POJO("United Kingdom", "London"));
        MyAdapter customAdapter = new MyAdapter(this, R.layout.my_list_item, myList_pojos);

        posts_lv.setAdapter(customAdapter);
```

![](https://images.ecency.com/DQmXDYWLAqSpY5yVcP6PYZffsAUwCgKqSmbTE2gSLz2YS7W/image.png)

![](https://images.ecency.com/DQmTRuteGLvwGWuoYoQ98W8UcG72Yc1ZecfhxbactsQQ6bJ/image.png)


<center>
![hl_divider.png](https://images.ecency.com/DQmabQycJhW8DT8a634z3wQaVXvPZ5KMii2LYbaWZ3PsaRy/hl_divider.png)

<h2>Thank You</h2>

![hl_footer_banner.png](https://images.ecency.com/DQmZ4HF3hjV4HwJXuw8vRJ6B6CiMC3kUkkoihjtm7z2Gii7/hl_footer_banner.png)
<div class="pull-left"><a href="https://discord.gg/7Bzqv4qUMT">
<img src="https://cdn.steemitimages.com/DQmXd6PwGUHRgSDkWtwKfDvdFpaLHXvXBdK7wnYZeqe1GUa/discord_animation_logo.gif"/></a>
</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 263 others
properties (23)
authorfaisalamin
permlinkandroid-app-development-or-lecture-3965ef8a14339
categoryhive-153850
json_metadata{"links":["https://github.com/faisalamin9696/HiveLearners2","http://schemas.android.com/apk/res/android","http://schemas.android.com/apk/res-auto","https://discord.gg/7Bzqv4qUMT"],"image":["https://images.ecency.com/DQmbVJY6FsTBEd8c2DLonUhi12j1DLmkp5DaoMqEfq7BiTR/multi_purpose.png","https://images.ecency.com/DQmfTN3S5MoDKwrLwarPFBoFyUBrS2KoCHNLc8gcfbGGDPv/image.png","https://images.ecency.com/DQmV8fXWUNFb4Ji17WUcUcWhrDXv6ZZJWuzScN9omEomTJG/image.png","https://images.ecency.com/DQmRm1L18Bz1mYwyjwnXoqYUcJYD9TeJuirTJdnwv9bK8p5/image.png","https://images.ecency.com/DQmfUbVFYd8bFppsS6yHnggTZA75pqi2enatjNSWXbKrmHE/image.png","https://images.ecency.com/DQmXDYWLAqSpY5yVcP6PYZffsAUwCgKqSmbTE2gSLz2YS7W/image.png","https://images.ecency.com/DQmTRuteGLvwGWuoYoQ98W8UcG72Yc1ZecfhxbactsQQ6bJ/image.png","https://images.ecency.com/DQmabQycJhW8DT8a634z3wQaVXvPZ5KMii2LYbaWZ3PsaRy/hl_divider.png","https://images.ecency.com/DQmZ4HF3hjV4HwJXuw8vRJ6B6CiMC3kUkkoihjtm7z2Gii7/hl_footer_banner.png"],"thumbnails":["https://images.ecency.com/DQmbVJY6FsTBEd8c2DLonUhi12j1DLmkp5DaoMqEfq7BiTR/multi_purpose.png","https://images.ecency.com/DQmfTN3S5MoDKwrLwarPFBoFyUBrS2KoCHNLc8gcfbGGDPv/image.png","https://images.ecency.com/DQmV8fXWUNFb4Ji17WUcUcWhrDXv6ZZJWuzScN9omEomTJG/image.png","https://images.ecency.com/DQmRm1L18Bz1mYwyjwnXoqYUcJYD9TeJuirTJdnwv9bK8p5/image.png","https://images.ecency.com/DQmfUbVFYd8bFppsS6yHnggTZA75pqi2enatjNSWXbKrmHE/image.png","https://images.ecency.com/DQmXDYWLAqSpY5yVcP6PYZffsAUwCgKqSmbTE2gSLz2YS7W/image.png","https://images.ecency.com/DQmTRuteGLvwGWuoYoQ98W8UcG72Yc1ZecfhxbactsQQ6bJ/image.png","https://images.ecency.com/DQmabQycJhW8DT8a634z3wQaVXvPZ5KMii2LYbaWZ3PsaRy/hl_divider.png","https://images.ecency.com/DQmZ4HF3hjV4HwJXuw8vRJ6B6CiMC3kUkkoihjtm7z2Gii7/hl_footer_banner.png","https://cdn.steemitimages.com/DQmXd6PwGUHRgSDkWtwKfDvdFpaLHXvXBdK7wnYZeqe1GUa/discord_animation_logo.gif"],"users":["Override"],"tags":["hive-153850","stem","neoxian","development","pakistan","ocd","android","firebase","java"],"description":"","app":"ecency/3.0.24-vision","format":"markdown+html"}
created2022-08-05 11:03:00
last_update2022-08-05 11:03:00
depth0
children4
last_payout2022-08-12 11:03:00
cashout_time1969-12-31 23:59:59
total_payout_value11.329 HBD
curator_payout_value11.228 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,645
author_reputation104,701,778,605,493
root_title"Android App Development | Lecture#38 | Hive Learners"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id115,455,276
net_rshares29,157,824,135,688
author_curate_reward""
vote details (327)
@curation-cartel ·
![1UP-PIZZA.png](https://files.peakd.com/file/peakd-hive/curation-cartel/23xediR4hotaNsS5pUJrmYVg3YGeTLpui41uCij2jhUDZ4uFT84zoGJf8a8VnfELXLJgt.png) |  <div class="phishy"><u><h4>You have received a __1UP__ from @gwajnberg!</h4></u></div> The @oneup-cartel will soon upvote you with:<hr> __@stem-curator, @neoxag-curator__ <hr>_And they will bring !PIZZA 🍕._
-|-

<sup>[Learn more](https://peakd.com/hive-102223/@flauwy/the-curation-cartel-1up-trigger-smart-voting-mana-and-high-delegation-returns-for-14-different-tribes) about our delegation service to earn daily rewards. Join the Cartel on [Discord](https://discord.gg/mvtAneE3Ca).</sup>
properties (22)
authorcuration-cartel
permlinkre-android-app-development-or-lecture-3965ef8a14339-20220805t164932z
categoryhive-153850
json_metadata"{"app": "beem/0.24.26"}"
created2022-08-05 16:49:33
last_update2022-08-05 16:49:33
depth1
children0
last_payout2022-08-12 16:49:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length640
author_reputation1,123,281,846,602
root_title"Android App Development | Lecture#38 | Hive Learners"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id115,464,152
net_rshares0
@gwajnberg ·
A list view will help fo that development!
!1UP
<div class=text-center> You can earn passive income by delegation of tribe tokens to "The Cartel".

[![dlmmqb-TheCartel-banner](https://images.hive.blog/p/FUkUE5bzkAZT3HzV5tJDiU2ik81PCd4JCyhWnRcDN8XJsVFY3UNB8DCSmoM2ujRpecRrPXsPH9dFMuJKxLKfeZPBgLnZuUbnYbwhioLnFLQPnge8EMm1SCsLqC5EQSHEM7uy7Bso7uBpiM6je1T8qfEb8c4wuu3hF5XU?format=match&mode=fit)](https://discord.gg/zQrvxAu7mu) 
Click this banner to join "The Cartel" discord server to know more. </div>
👍  , , ,
properties (23)
authorgwajnberg
permlinkrg5iax
categoryhive-153850
json_metadata{"tags":["stem"],"image":["https://images.hive.blog/p/FUkUE5bzkAZT3HzV5tJDiU2ik81PCd4JCyhWnRcDN8XJsVFY3UNB8DCSmoM2ujRpecRrPXsPH9dFMuJKxLKfeZPBgLnZuUbnYbwhioLnFLQPnge8EMm1SCsLqC5EQSHEM7uy7Bso7uBpiM6je1T8qfEb8c4wuu3hF5XU?format=match&mode=fit"],"links":["https://discord.gg/zQrvxAu7mu"],"app":"stemgeeks/0.1","canonical_url":"https://stemgeeks.net/@gwajnberg/rg5iax"}
created2022-08-05 16:40:09
last_update2022-08-05 16:40:09
depth1
children1
last_payout2022-08-12 16:40:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length498
author_reputation149,619,952,421,366
root_title"Android App Development | Lecture#38 | Hive Learners"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id115,463,918
net_rshares1,856,506,200
author_curate_reward""
vote details (4)
@faisalamin ·
Thank you for your love and support.
👍  , , ,
properties (23)
authorfaisalamin
permlinkre-gwajnberg-202285t221753833z
categoryhive-153850
json_metadata{"tags":["stem"],"app":"ecency/3.0.24-vision","format":"markdown+html"}
created2022-08-05 17:17:54
last_update2022-08-05 17:17:54
depth2
children0
last_payout2022-08-12 17:17:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length36
author_reputation104,701,778,605,493
root_title"Android App Development | Lecture#38 | Hive Learners"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id115,464,933
net_rshares1,854,190,780
author_curate_reward""
vote details (4)
@stemsocial ·
re-faisalamin-android-app-development-or-lecture-3965ef8a14339-20220805t171255500z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> 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.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-faisalamin-android-app-development-or-lecture-3965ef8a14339-20220805t171255500z
categoryhive-153850
json_metadata{"app":"STEMsocial"}
created2022-08-05 17:12:54
last_update2022-08-05 17:12:54
depth1
children0
last_payout2022-08-12 17:12:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,460,334,324,555
root_title"Android App Development | Lecture#38 | Hive Learners"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id115,464,770
net_rshares0