【问题标题】:Android - How to make Checkbox in ListView to work?Android - 如何使 ListView 中的复选框起作用?
【发布时间】:2013-11-08 08:46:45
【问题描述】:

我试图在活动中放置一个列表视图,然后从我的 sqlite 数据库中检索数据。然后,用户可以从包含Checkbox的列表查看中查看/取消选中多个选项,其中包含客户名称。检查复选框时,我希望执行函数,例如在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>

【问题讨论】:

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


【解决方案1】:

为 ListView 制作自定义适配器,就像 ArrayAdapter 一样。

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

在这里,您可以根据单击复选框来更新代码。如果选中复选框,则插入记录,如果未选中,则按您的意愿操作。

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

【讨论】:

  • 嗨@iDroidExplorer,感谢您的回答,但我在将简单的适配器转换为自定义适配器时遇到问题,看起来很难从自定义适配器中检索数据。你能告诉我一些关于如何做到这一点的想法吗?谢谢
  • 好的,让我给你一些示例链接。您可以参考它并相应地更新您的代码。请等待我的最新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
相关资源
最近更新 更多