Android - 如何使ListView中的Checkbox工作?

时间:2013-11-08 08:46:46

标签: android listview checkbox android-listview android-checkbox

我试图将listview放在一个活动中,然后从我的sqlite数据库中检索数据。然后,用户可以从列表视图中选中/取消选中包含显示客户名称的复选框的多个选项。当选中该复选框时,我希望执行诸如在sqlite数据库中的表中向特定客户添加费用等功能。由用户检查。问题是,我在每一行都创建了带有复选框的listView,但是我被困了,因为我不知道如何对它执行功能。谁能指导我在这里?或者让我知道如何做到这一点?感谢。

这是我的listView代码:

public class AddExpense extends ListActivity implements OnClickListener{

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

@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);

    HashMap<String, String> queryValues = new HashMap<String, String>();
    Intent objIntent = getIntent();
    String eventId = objIntent.getStringExtra("eventId");
    queryValues.put("eventId", eventId);

    //Create Listview that retrieve all the customer data in that event
    ArrayList<HashMap<String, String>> friendList = controller
            .getAllFriends(queryValues);
    if (friendList.size() != 0) {
        lv = getListView();
    }
    SimpleAdapter adapter1 = new SimpleAdapter(AddExpense.this,
            friendList, R.layout.view_expenses_participant_entry, new String[] {
                    "friendId", "friendName" },
            new int[] { R.id.friendId, R.id.friendName});
    adapter.notifyDataSetChanged();
    setListAdapter(adapter1);
}

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

}

这是每一行的xml文件:

<CheckBox
    android:id="@+id/list_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false" >
</CheckBox>

<TextView
    android:id="@+id/friendId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

<TextView
    android:id="@+id/friendName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:paddingLeft="6dip"
    android:paddingTop="6dip"
    android:textColor="#A4C739"
    android:textSize="17sp"
    android:textStyle="bold" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

为ListView创建自定义适配器,就像ArrayAdapter一样。

在该适配器类中,您必须对该类的getView()方法进行编码。

这里你可以根据点击复选框更新代码。如果复选框被cheked然后插入记录,如果未选中,则按照您的意愿执行。

如果您有任何疑问,请告诉我。