0

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

5
  • are u facing problem in adding checkboxes to listview ??? Commented Jul 12, 2012 at 7:39
  • Like I have used the list adapter and then how can I use the checkboxes inside it and also then I want to insert that selected value into the another table. I am new so don't know much sorry if my question is stupid @Goofy Commented Jul 12, 2012 at 7:43
  • so u want check boxes in the listview... right? Commented Jul 12, 2012 at 7:53
  • Yes I want to and then I want to insert that selected values in the table @Goofy Commented Jul 12, 2012 at 8:37
  • You can refer this link for checkboxes in listview windrealm.org/tutorials/android/… Commented Jul 12, 2012 at 8:50

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

1 Comment

added code to my answer. I guess this would help you understand but this code is not for copy-paste.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.