Post Top Ad

Post Top Ad

Saturday 8 July 2017

Android ListView Adapter

A ListView item here contains a text (item name) and an ID (item ID), so whenever the user selects an item in the pop up, you’ll be able to get the text and ID of that selected item. This is very useful if the items are from a database – records with IDs and names. The tags (item IDs) were set using the TextView’s setTag() method.

1.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kiran.lesson15_listviewadapter.MainActivity">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView">
    </ListView>

    <Button
        android:text="Add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="addFruit"
        android:id="@+id/add_btn"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="12dp"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

2.ItemLayout.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:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:id="@+id/animal_tv" />
</LinearLayout>


3.MainActivity.java

package com.example.kiran.lesson15_listviewadapter;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    private Context mContext;
    private ArrayList<String> mAnimalList;
    private LayoutInflater mLayoutInflater;
    private MyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.listView);
        mAnimalList = new ArrayList<String>();
        mAnimalList.add("Dog");
        mAnimalList.add("Cat");
        mAnimalList.add("Mouse");
        mAnimalList.add("Elephant");
        mAnimalList.add("Horse");
        mAnimalList.add("Dog");
        mAnimalList.add("Cat");
        mAnimalList.add("Mouse");
        mAnimalList.add("Elephant");
        mAnimalList.add("Horse");
        mAnimalList.add("Cat");
        mAnimalList.add("Mouse");
        mAnimalList.add("Elephant");
        mAnimalList.add("Horse");
        mAnimalList.add("Dog");
        mAnimalList.add("Cat");
        mAnimalList.add("Mouse");
        mAnimalList.add("Elephant");
        mAnimalList.add("Horse");
        adapter = new MyAdapter(MainActivity.this,mAnimalList);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mAnimalList.remove(position);
                adapter.setmAnimalList(mAnimalList);
                adapter.notifyDataSetChanged();
                Toast.makeText(MainActivity.this,"Deleted",Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void addFruit(View view) {
        mAnimalList.add("Apple");
        mAnimalList.add("Grapes");
        mAnimalList.add("Orange");
        mAnimalList.add("Banana");
        mAnimalList.add("Mango");
        mAnimalList.add("Pineapple");
        adapter.setmAnimalList(mAnimalList);
        adapter.notifyDataSetChanged();
    }
}

4.MyAdapter.java:

package com.example.kiran.lesson15_listviewadapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;


public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<String> mAnimalList;
    private LayoutInflater mLayoutInflater;

    public MyAdapter(Context mContext, ArrayList<String> animalList) {
        this.mContext = mContext;
        this.mAnimalList = animalList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setmAnimalList(ArrayList<String> mAnimalList) {
        this.mAnimalList = mAnimalList;
    }

    @Override
    public int getCount() {
        return mAnimalList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = mLayoutInflater.inflate(R.layout.item_layout,parent,false);
        }
        TextView mAnimalTv = (TextView) convertView.findViewById(R.id.animal_tv);
        mAnimalTv.setText(mAnimalList.get(position));
        Log.i("MyAdapter","position"+position);
        return convertView;
    }
}

No comments:

Post a Comment