我正在尝试使用复选框创建列表视图,用户可以检查选项以选择支付特定费用的客户。我用复选框创建了listview,问题是如何从我的自定义适配器类访问数据库?好像我不能在那个类中声明我的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
}
答案 0 :(得分: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();
}
答案 1 :(得分:0)
Cursor Adapter应为您简化操作,或者您可以在复选框视图中添加ID作为标记,查看setTag,然后在onCheckedChanged和
中获取标记我不确定你是否以正确的方式这样做,如果你先研究游标适配器,那就更好了。