Blog purpose for android basic example for android app developer. any query please my contact

Sunday, 14 February 2021

How to bind data from a database to an Android CheckBox in a ListView

 How to bind data from a database to an Android CheckBox in a ListView


In this Tutorial Today learn How to bind data from a database to an Android CheckBox in a ListView.

for more information follow the  bind data from a database to an Android CheckBox in a ListView

follow the full Source code How to bind data from a database to an Android CheckBox in a ListView.


1.MainActivity.java:

package com.akash.myapplication;

import android.app.Activity;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ListView;

import java.util.ArrayList;

import android.os.Bundle;

public class MainActivity extends Activity {

    ArrayList dataModels;

    ListView listView;

    private CustomAdapter adapter;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);

        dataModels = new ArrayList();

        dataModels.add(new DataModel("Apple ", false));

        dataModels.add(new DataModel("Banana ", false));

        dataModels.add(new DataModel("Orange", false));

        dataModels.add(new DataModel("Mango", true));

        dataModels.add(new DataModel("Papaya", true));

        dataModels.add(new DataModel("Pineapple", true));

  

        adapter = new CustomAdapter(dataModels, getApplicationContext());

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

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

                DataModel dataModel= (DataModel) dataModels.get(position);

                dataModel.checked = !dataModel.checked;

                adapter.notifyDataSetChanged();

            }

        });

    }

}



2.activity_main.xml:

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

<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">


    <ListView

        android:id="@+id/listView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        tools:ignore="MissingConstraints" />


</androidx.constraintlayout.widget.ConstraintLayout>


3.CustomAdapter.java:

package com.akash.myapplication;


import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.CheckBox;

import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;

public class CustomAdapter extends ArrayAdapter{

    private ArrayList dataSet;

    Context mContext;

    // View lookup cache

    private static class ViewHolder {

        TextView txtName;

        CheckBox checkBox;

    }

    public CustomAdapter(ArrayList data, Context context) {

        super(context, R.layout.row_item, data);

        this.dataSet = data;

        this.mContext = context;

    }

    @Override

    public int getCount() {

        return dataSet.size();

    }

    @Override

    public DataModel getItem(int position) {

        return (DataModel) dataSet.get(position);

    }

    @Override

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

        ViewHolder viewHolder;

        final View result;

        if (convertView == null) {

            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);

            viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);

            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);

            result=convertView;

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

            result=convertView;

        }

        DataModel item = getItem(position);

        viewHolder.txtName.setText(item.name);

        viewHolder.checkBox.setChecked(item.checked);

        return result;

    }

}



4.DataModel.java:

package com.akash.myapplication;

public class DataModel {
    public String name;
    boolean checked;
    DataModel(String name, boolean checked) {
        this.name = name;
        this.checked = checked;
    }
}




5.row_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/txtName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:textSize="16sp" />
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>


6. AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

7.colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
</resources>

8.styles.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

9.strings.xml;

<resources>
    <string name="app_name">My Application</string>
</resources>

10.Output:

How to bind data from a database to an Android CheckBox in a ListView




No comments:

Post a Comment