mercredi 21 octobre 2015

How do I add a checkbox widget to a todo list in Android Studio?

In Android Studio, I am working on making a ToDo list where you can add, remove, and check off tasks. So far, I have a ToDo list that allows you to add items and delete them, but I am looking for a way to incorporate a checkBox widget with each item that the user can click after they complete a task.

My .java file:

private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_urgent__important);

    lvItems = (ListView) findViewById(R.id.lvItems);
    items = new ArrayList<String>();
    readItems();
    itemsAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, items);
    lvItems.setAdapter(itemsAdapter);
    items.add("First Item");
    items.add("Second Item");
    setupListViewListener();
}

private void setupListViewListener() {
    lvItems.setOnItemLongClickListener(
            new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> adapter,
                                               View item, int pos, long id) {
                    // Remove the item within array at position
                    items.remove(pos);
                    // Refresh the adapter
                    itemsAdapter.notifyDataSetChanged();
                    // Return true consumes the long click event (marks it handled)
                    writeItems();
                    return true;
                }

            });
}

public void onAddItem(View v){
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    itemsAdapter.add(itemText);
    etNewItem.setText("");
    writeItems();
}

How do I create a checkBox widget with each new item?




Aucun commentaire:

Enregistrer un commentaire