Question

I am trying to create a listview with checkbox, and user can check the options to select which customer to pay the particular expense. I created the listview with checkbox, the problem is how to access database from my custom adapter class? It seems like i can't declare my sqlite database in that class. But i would like to add the expense to checked customer by referring to the customerId. How can i do that? I stuck in onCheckChanged method in my custom adapter class. Sorry for my bad english. Thanks

This is my AddExpense class:

public class AddExpense extends ListActivity implements OnClickListener {

EditText expenseName;
Spinner expenseType;
EditText expensePrice;
EditText expenseQuantity;
EventController controller = new EventController(this);
Button btnadd;
ListView lv;

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addexpense);
    expenseName = (EditText) findViewById(R.id.expenseName);
    expenseType = (Spinner) findViewById(R.id.expenseType);
    // Create an ArrayAdapter using the string array and a default spinner
    // layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.type, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    expenseType.setAdapter(adapter);
    expensePrice = (EditText) findViewById(R.id.expensePrice);
    expenseQuantity = (EditText) findViewById(R.id.expenseQuantity);
    btnadd = (Button) findViewById(R.id.btnaddexp);
    btnadd.setOnClickListener(this);

    @SuppressWarnings("rawtypes")
    ArrayList person = new ArrayList();
    person.clear();
    Intent objIntent = getIntent();
    String eventId = objIntent.getStringExtra("eventId");
    String query = "SELECT  * FROM friends WHERE friendEvent = " + eventId;
    Cursor c1 = controller.selectQuery(query);
    if (c1 != null & c1.getCount() != 0) {
        if (c1.moveToFirst()) {
            do {
                getParticipant person1 = new getParticipant();
                person1.setfriendId(c1.getString(c1
                        .getColumnIndex("friendId")));
                person1.setfriendName(c1.getString(c1.getColumnIndex("friendName")));

                person.add(person1);
            } while (c1.moveToNext());
        }
    }
    c1.close();

    ListAdapter listadapter = new ListAdapter(AddExpense.this,person);

    adapter.notifyDataSetChanged();
    setListAdapter(listadapter);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    HashMap<String, String> queryValues = new HashMap<String, String>();
    Intent objIntent = getIntent();
    String eventId = objIntent.getStringExtra("eventId");
    queryValues.put("eventId", eventId);
    queryValues.put("expenseName", expenseName.getText().toString());
    queryValues
            .put("expenseType", expenseType.getSelectedItem().toString());
    queryValues.put("expensePrice", expensePrice.getText().toString());
    queryValues
            .put("expenseQuantity", expenseQuantity.getText().toString());
    controller.insertExpense(queryValues);
    this.callHomeActivity(v);
    finish();
}

public void callHomeActivity(View view) {
    super.onResume();
}

This is my custom adapter code:

public class ListAdapter extends BaseAdapter implements OnCheckedChangeListener{
Context ctx;
LayoutInflater lInflater;
ArrayList<getParticipant> objects;

ListAdapter(Context context, ArrayList<getParticipant> friendList) {
    ctx = context;
    objects = friendList;
    lInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return objects.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    getParticipant person = objects.get(position);
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.view_expenses_participant_entry, parent, false);
    }
    TextView friendId = (TextView)view.findViewById(R.id.friendId);
    friendId.setText(person.getfriendId());
    TextView friendName = (TextView)view.findViewById(R.id.friendName);
    friendName.setText(person.getfriendName());
    CheckBox cbperson = (CheckBox)view.findViewById(R.id.list_checkbox);
    cbperson.setOnCheckedChangeListener(this);
    return view;
}
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    // TODO Auto-generated method stub

}
Was it helpful?

Solution

why can't you access your database here, You have Context of your activity in ctx variable, you can access db in OnCheckedChanged() method, See below

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    // TODO Auto-generated method stub
YOURSQLITEdatabase db = new YOURSQLITEdatabse(ctx);

db.performYourOperationsHere();

db.close();

}

OTHER TIPS

Cursor Adapter should simplify things for you or you can add the id as tag in the check box view, take a look at setTag and then get the tag in your onCheckedChanged and

  1. do operations in onCheckedChanged OR
  2. keep storing it in an array or removing it depending on weather it's checked or unchecked and use this array of ids to perform db operations when adding expense on click of submit button, if you have something like that on your screen as doing db operation on every click will slow things for the user

Am not sure if you are doing this the correct way, Better would be if you study cursor adapters first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top