0

我正在尝试创建一个带有复选框的列表视图,用户可以检查选项以选择哪个客户支付特定费用。我用复选框创建了列表视图,问题是如何从我的自定义适配器类访问数据库?似乎我无法在该类中声明我的 sqlite 数据库。但我想通过引用 customerId 将费用添加到已检查的客户。我怎样才能做到这一点?我在自定义适配器类中使用了 onCheckChanged 方法。对不起,我的英语不好。谢谢

这是我的 AddExpense 类:

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();
}

这是我的自定义适配器代码:

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

}
4

2 回答 2

0

为什么你不能在这里访问你的数据库,你在 ctx 变量中有你的活动上下文,你可以在 OnCheckedChanged() 方法中访问 db,见下文

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

db.performYourOperationsHere();

db.close();

}
于 2013-11-08T12:02:26.357 回答
0

光标适配器应该为您简化事情,或者您可以将 id 添加为复选框视图中的标签,查看setTag,然后在您的 onCheckedChanged 和

  1. 在 onCheckedChanged 中执行操作或
  2. 继续将其存储在一个数组中或根据天气将其删除,它已选中或未选中,并在单击提交按钮时添加费用时使用此 ids 数组来执行 db 操作,如果您的屏幕上有类似的内容,例如对每个执行 db 操作点击会减慢用户的速度

不确定您是否以正确的方式执行此操作,最好先研究光标适配器。

于 2013-11-08T12:28:00.820 回答