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;
}
}
No comments:
Post a Comment