我正在尝试创建一个带有复选框的列表视图,用户可以检查选项以选择哪个客户支付特定费用。我用复选框创建了列表视图,问题是如何从我的自定义适配器类访问数据库?似乎我无法在该类中声明我的 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
}