Android Sqlite Data Show To Custom Listview

Please Follow Previews Post To Insert Data Link

next step activityShowdata.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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".Showdata">
<Button
    android:id="@+id/show"
    android:text="show"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>
</LinearLayout>

next step Showdata.java

package olee.tech.sqlitelistview;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class Showdata extends AppCompatActivity {
    SQLiteDatabase database;
    Dbhelper dbhelper;
    Cursor cursor;
    DisplayAdapter displayAdapter;

    ArrayList<String> namelist;
    ArrayList<String> agelist;

    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showdata);

        listView = findViewById(R.id.list);

        namelist = new ArrayList<>();
        agelist = new ArrayList<>();
        dbhelper = new Dbhelper(Showdata.this);
        database = dbhelper.getReadableDatabase();
        cursor = database.rawQuery("SELECT * FROM STUDENT",null);
        namelist.clear();
        agelist.clear();
        if (cursor.moveToFirst()){

            do {
                namelist.add(cursor.getString(cursor.getColumnIndex("name")));
                agelist.add(cursor.getString(cursor.getColumnIndex("age")));

            }

            while (cursor.moveToNext());

            DisplayAdapter displayAdapter = new DisplayAdapter(this,namelist,agelist);
            listView.setAdapter(displayAdapter);
            cursor.close();
        }


    }
}

next step mylist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    android:layout_height="wrap_content" />

<TextView
    android:padding="5dp"
    android:id="@+id/nametext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:padding="5dp"
    android:id="@+id/agetext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

custom adapter

package olee.tech.sqlitelistview;


import android.content.Context;
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 DisplayAdapter extends BaseAdapter {

Context context;
ArrayList<String> name;
ArrayList<String> age;

    public DisplayAdapter(Context context,  ArrayList<String> name, ArrayList<String> age) {
        this.context = context;
        this.name = name;
        this.age = age;
    }

    @Override
    public int getCount() {
        return name.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) {
        LayoutInflater layoutInflater;
         layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View view = layoutInflater.inflate(R.layout.mylist,parent,false);
        TextView nametext = view.findViewById(R.id.nametext);
        TextView agetext = view.findViewById(R.id.agetext);
        nametext.setText(name.get(position));
        agetext.setText(age.get(position));
        return view;
    }
}

Published by

Unknown's avatar

Nusrat Faria

I Am A Web Developer And A Android Developer. This Is My Personal Blog So Noted My Work For Helping People .

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.