Widget Recent Post No.

header ads

Custom listView in android with item click


 Step 1 main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">



    <ListView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:id="@+id/listView">


    </ListView>


</LinearLayout>


Step 2 row.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:padding="16dp"

    >


<ImageView

    android:layout_width="75dp"

    android:layout_height="75dp"

    android:src="@mipmap/ic_launcher"

    android:id="@+id/image"

    />

<LinearLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical"

    >


    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Main Title"

        android:textColor="#000"

        android:textStyle="bold"

        android:layout_margin="5dp"

        android:textSize="20sp"

        android:id="@+id/textView1"

        />


    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Sub Title"

        android:textColor="#a9a9a9"

        android:textStyle="bold"

        android:layout_margin="5dp"

        android:textSize="15sp"

        android:id="@+id/textView2"

        />

    <!--so our layout is set-->


</LinearLayout>




</LinearLayout>


Step 3 main.java

package in.learncodewithrk.listview;


import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Context;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    ListView listView;

    String mTitle[] = {"Lorem Ipsum", "Lorem Ipsum", "Lorem Ipsum", "Lorem Ipsum", "Lorem Ipsum", "Lorem Ipsum","Lorem Ipsum", "Lorem Ipsum", "Lorem Ipsum", "Lorem Ipsum"};

    String mDescription[] = {"Lorem Ipsum is simply dummy text of the printing and typesetting industry","Lorem Ipsum is simply dummy text of the printing and typesetting industry","Lorem Ipsum is simply dummy text of the printing and typesetting industry","Lorem Ipsum is simply dummy text of the printing and typesetting industry","Lorem Ipsum is simply dummy text of the printing and typesetting industry","Lorem Ipsum is simply dummy text of the printing and typesetting industry", "Lorem Ipsum is simply dummy text of the printing and typesetting industry", "Lorem Ipsum is simply dummy text of the printing and typesetting industry", "Lorem Ipsum is simply dummy text of the printing and typesetting industry", "Lorem Ipsum is simply dummy text of the printing and typesetting industry"};

    int images[] = {R.drawable.coat, R.drawable.shirt, R.drawable.top, R.drawable.jeans, R.drawable.pant,R.drawable.coat, R.drawable.shirt, R.drawable.top,R.drawable.jeans,R.drawable.pant};

    // so our images and other things are set in array


    // now paste some images in drawable


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        listView = findViewById(R.id.listView);

        // now create an adapter class


        MyAdapter adapter = new MyAdapter(this, mTitle, mDescription, images);

        listView.setAdapter(adapter);

        // there is my mistake...

        // now again check this..


        // now set item click on list view

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position ==  0) {

                    Toast.makeText(MainActivity.this, "coat Description", Toast.LENGTH_SHORT).show();

                }

                if (position ==  0) {

                    Toast.makeText(MainActivity.this, "shirt Description", Toast.LENGTH_SHORT).show();

                }

                if (position ==  0) {

                    Toast.makeText(MainActivity.this, "top Description", Toast.LENGTH_SHORT).show();

                }

                if (position ==  0) {

                    Toast.makeText(MainActivity.this, "jeans Description", Toast.LENGTH_SHORT).show();

                }

                if (position ==  0) {

                    Toast.makeText(MainActivity.this, "pant Description", Toast.LENGTH_SHORT).show();

                }


             

            }

        });

        // so item click is done now check list view

    }


    class MyAdapter extends ArrayAdapter<String> {


        Context context;

        String rTitle[];

        String rDescription[];

        int rImgs[];


        MyAdapter (Context c, String title[], String description[], int imgs[]) {

            super(c, R.layout.row, R.id.textView1, title);

            this.context = c;

            this.rTitle = title;

            this.rDescription = description;

            this.rImgs = imgs;


        }


        @NonNull

        @Override

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

            LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View row = layoutInflater.inflate(R.layout.row, parent, false);

            ImageView images = row.findViewById(R.id.image);

            TextView myTitle = row.findViewById(R.id.textView1);

            TextView myDescription = row.findViewById(R.id.textView2);


            // now set our resources on views

            images.setImageResource(rImgs[position]);

            myTitle.setText(rTitle[position]);

            myDescription.setText(rDescription[position]);





            return row;

        }

    }

}


GITHUB : https://github.com/LearncodeWithRk/ListView


Post a Comment

0 Comments