ADB
ADB

Reputation: 567

How can i use the Check Boxes with ListView and Insert the value in Sqlite Database in android

I have created one database with three table and place that database into the asset folder. Now I want to show the value from one column on a list view with checkboxes and then on user selection want to insert them into the another table. SO how can I do that.

Till now what I have done is to fetch the data and show on the list. But how can I place the checkboxes and then fetch the value I don't know. The code I used is below

1.Main Activity

package com.example;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity {

    private Cursor employees;
    private MyDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new MyDatabase(this);
        employees = db.getEmployees(); // you would not typically call this on the main thread

        ListAdapter adapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, 
                employees, 
                new String[] {"FullName"}, 
                new int[] {android.R.id.text1});

        getListView().setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        employees.close();
        db.close();
    }

}

2.Mydatabase.java

package com.example;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class MyDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "northwind";
    private static final int DATABASE_VERSION = 2;

    public MyDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);  
        //setForcedUpgradeVersion(2);
    }

    public Cursor getEmployees() {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String [] sqlSelect = {"0 _id", "FullName"}; 
        String sqlTables = "Employees";

        qb.setTables(sqlTables);
        Cursor c = qb.query(db, sqlSelect, null, null,
                null, null, null);

        c.moveToFirst();
        return c;

    }

}

Please suggest me that how can I do this.. Any help must be appreciated

Upvotes: 0

Views: 2199

Answers (1)

10s
10s

Reputation: 1699

In my point of view you should create a custom adapter for the ListView and manually iterate over the result dataset, update an ArrayList<MyObject>(that you should also create).

Then create some functions in your custom adapter in order to obtain the "selected" items, or create some functions like public void setSelected(int position) that changes the checkbox accordingly and private boolean isSelected(int position) which returns whether the checkbox is selected or not.

First of all we need the object class in order to hold the dataset:

  class MyObject {
      //properties with setters and getters
      boolean selected;
  }

In Activity:

private MyActivity extends Activity {
  ...
  ArrayList<MyObject> data = new ArrayList<MyObject>();
  ...
  //get dataset
  for(int i=0; i<cursor.getCount(); i++) {
     //create and set MyObject
     data.add(MyObject); 
  }     
  ...
  CustomAdapter myAdapter = new CustomAdapter(..., data);
  listView.setAdapter(myAdapter);
  ...
}

In CustomAdapter class:

class CustomAdapter extends BaseAdapter {
   ArrayList<MyObject> data;
   //constructor, properties, getters, setters
   //implement methods

   public void setSelected(int position) {
      data.get(position).setSelected(true); //boolean selected property defines whether the checkbox is selected or not
      notifyDatasetChanged(); //update the UI
   }

   @Override
   getView() {
      CheckBox checkBox; //the check box of the list row
      ...
      if(data.get(position).isSelected()) {
         checkBox.setSelected(true);
      } else {
         checkBox.setSelected(false);
      } //update the checkbox accordingly
   }
}

I am writing them down very briefly to give you an overview. There are a lot of code to write but I am giving you the "hot piece".

Upvotes: 2

Related Questions