Tuesday, 1 August 2017

Custom Listview in Android


  Listview: -

It is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter pulls content from a source such as an array or database query and converts each item result into a view that's placed on the list.

 

Step 1: - Open your project.In your activity.xml creates list view as mention below.                          
         
activity_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"
    tools:context="com.example.hp.mylistview.MainActivity">
   <ListView
       android:id="@+id/product_list"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       />
</LinearLayout>

          
Step 2: -Now create another xml which attach to our list view.

row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
      <TextView
          android:id="@+id/product_name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
      <TextView
          android:id="@+id/product_price"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
</LinearLayout>

In it, I am using two text-view to show two texts in our list view.

Step 3: -Now create a bean class to describe our component.

Product.java

public class Product {
    private String productName;
    private String productPrice;
    public Product(String productName,String productPrice){
        this.productName=productName;
        this.productPrice=productPrice;
    }

    public String getProductName() {
        return productName;
    }

    public String getProductPrice() {
        return productPrice;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public void setProductPrice(String productPrice) {
        this.productPrice = productPrice;
    }
}

Step 4: - I am creating custom Listview.In it I am using two text view for that we need to create an adapter class.If I need to display only one text in one row then we do not need to create custom adapter there is default adapter that we use.

ListAdapter.java

public class ListAdapter extends ArrayAdapter<Product>{
    private Context context;
    private List<Product> productList;
    private int id;
    public ListAdapter(Context context,int id,List<Product> productList){
        super(context,id,productList);
        this.context=context;
        this.id=id;
        this.productList=productList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView==null){
            LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(id,parent,false);
        }
        TextView productName=(TextView) convertView.findViewById(R.id.product_name);
        TextView productPrice=(TextView) convertView.findViewById(R.id.product_price);
        productName.setText(productList.get(position).getProductName());
        productPrice.setText(productList.get(position).getProductPrice());
        return  convertView;
    }
}

Step 5: -Now time to write code in MainActivity.java.

MainActivity.java
package com.example.hp.mylistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<Product> productList=new ArrayList<>();
        productList.add(new Product("Kurkure","10"));
        productList.add(new Product("Kurkure","20"));
        productList.add(new Product("Kurkure","30"));
        productList.add(new Product("Kurkure","40"));
        productList.add(new Product("Kurkure","50"));
        productList.add(new Product("Kurkure","60"));
        productList.add(new Product("Kurkure","70"));
        ListView listView=(ListView)findViewById(R.id.product_list);
        ListAdapter adapter=new com.example.hp.mylistview.ListAdapter(this,R.layout.row,productList);
        listView.setAdapter(adapter);



    }
}


No comments:

Post a Comment

Change Fragment on button click in Android

In example mention below on click of a button, we attach the desired fragment in our frame layout which was present in activity_main.xml. ...