mercredi 10 mars 2021

How To Add Checkboxes To ListView (Each list) in Android Studio

basically I am creating a Task/ToDo List app and I can't figure this part out. I want to add a checkbox next to each task and the ability to check them.

Here is the MainActivity.java


public class MainActivity extends AppCompatActivity {

static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;


public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main_menu, menu);

    return super.onCreateOptionsMenu(menu);
}


public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    if (item.getItemId() == R.id.add_note) {
        Intent intent = new Intent(getApplicationContext(),
                note_editor.class);
        startActivity(intent);
        return true;
    }
    return false;
}

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

    ListView listView = (ListView) findViewById(R.id.listView);

    SharedPreferences sharedPreferences =
            getApplicationContext().getSharedPreferences
                    ("com.example.assignment1", Context.MODE_PRIVATE);


    HashSet<String> set = (HashSet<String>)
            sharedPreferences.getStringSet("notes", null);

    if (set == null) {
        notes.add("Add Your Task Hereee");
    } else {
        notes = new ArrayList(set);
    }


    arrayAdapter = new ArrayAdapter
            (this, android.R.layout.simple_list_item_1, notes);

    listView.setAdapter(arrayAdapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick
        (AdapterView<?> adapterView, View view, int i, long l) {

            Intent intent = new Intent(getApplicationContext(),
                    note_editor.class);
            intent.putExtra("noteId", i);
            startActivity(intent);


        }

    });

My main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/listView" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="28dp"
        android:backgroundTint="#4BB3A9"
        android:src="@drawable/add_task"/>

</RelativeLayout>

I want it to look something like this": I drew the checkboxes here

I also have another class for when editing the tasks as well as for a splash screen but I don't think it's necessary here. Any help would be really appreciated!




Aucun commentaire:

Enregistrer un commentaire