samedi 26 septembre 2020

Custom listview adapter with textview, checkbox and image button

Hello everyone, there are certainly numerous examples online, but I can't find a suitable example for my code. I hope someone can help. So I just want the existing functions to be retained, but also to save the CheckboxStatus. It would be great if someone could tell me how this works with a code example. LG

row:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/task_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:text="Example"
        android:textSize="20sp" />

    <ImageButton
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="2dp"
        android:layout_marginRight="2dp"
        android:background="#F44336"
        android:onClick="deleteTask"
        android:src="@android:drawable/ic_menu_delete" />

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="36dp"
        android:focusable="false"
        android:focusableInTouchMode="false" />


</RelativeLayout>

MainActivity:

    private void loadTaskList() {

        ArrayList<String> taskList = dbHelper.getTaskList();
        if (mAdapter == null) {
                    mAdapter = new ArrayAdapter<String>(this, layout.row, id.task_title, taskList);
            lstTask.setAdapter(mAdapter);
                    lstTask.getAdapter().getCount();
            Toast.makeText(getApplicationContext(), "Aufgabenanzahl:" + lstTask.getAdapter().getCount(), Toast.LENGTH_LONG).show();
            textView3.setText("" + lstTask.getAdapter().getCount());

            } else {
            mAdapter.clear();
            mAdapter.addAll(taskList);
            mAdapter.notifyDataSetChanged();
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);

        Drawable icon = menu.getItem(0).getIcon();
        icon.mutate();
        icon.setColorFilter(getResources().getColor(android.R.color.white), PorterDuff.Mode.SRC_IN);

        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        switch (item.getItemId()) {
            case  id.action_add_task:
                final EditText taskEditText = new EditText(this);
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Aufgabe hinzufügen");
                builder.setMessage("Was möchtest du als nächstes tun?");
                builder.setView(taskEditText);
                builder.setPositiveButton("Hinzufügen", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                    String task = String.valueOf(taskEditText.getText());
                                    dbHelper.insertNewTask(task);
                                    loadTaskList();
                                lstTask.getAdapter().getCount();
                                textView3.setText("" + lstTask.getAdapter().getCount());
                                Toast.makeText(getApplicationContext(), "Aufgabenanzahl:" + lstTask.getAdapter().getCount(), Toast.LENGTH_LONG).show();
                                }
                             });

                builder.setNegativeButton("Zurück", null);
                AlertDialog dialog = builder
                        .create();
                dialog.show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void deleteTask(View view) {
        View parent = (View) view.getParent();
        TextView taskTextView = (TextView) parent.findViewById(id.task_title);
        Log.e("String1", (String) taskTextView.getText());
        String task = String.valueOf(taskTextView.getText());
        dbHelper.deleteTask(task);
        loadTaskList();
        lstTask.getAdapter().getCount();
        Toast.makeText(getApplicationContext(), "Aufgabenanzahl:" + lstTask.getAdapter().getCount(), Toast.LENGTH_LONG).show();
        textView3.setText("" + lstTask.getAdapter().getCount());

        }

Database:

public class Dbhelper extends SQLiteOpenHelper {

    private static final String DB_NAME="EDMTDev";
    private static final int DB_VER = 1;
    public static final String DB_TABLE="Task";
    public static final String DB_COLUMN = "TaskName";
    public static final String DB_ANZAHL = "Anzahl";


    public Dbhelper(Context context) {
        super(context, DB_NAME, null, DB_VER);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = String.format("CREATE TABLE %s (ID INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT NOT NULL);",DB_TABLE,DB_COLUMN,DB_ANZAHL);
        db.execSQL(query);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = String.format("DELETE TABLE IF EXISTS %s",DB_TABLE);
        db.execSQL(query);
        onCreate(db);

    }



    public void insertNewTask(String task){
        SQLiteDatabase db= this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DB_COLUMN,task);
        db.insertWithOnConflict(DB_TABLE,null,values,SQLiteDatabase.CONFLICT_REPLACE);
        db.close();
    }

    public void deleteTask(String task){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(DB_TABLE,DB_COLUMN + " = ?",new String[]{task});
        db.close();
    }


    public ArrayList<String> getTaskList() {
        ArrayList<String> taskList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(DB_TABLE, new String[]{DB_COLUMN}, null, null, null, null, null);
        while (cursor.moveToNext()) {
            int index = cursor.getColumnIndex(DB_COLUMN);
            taskList.add(cursor.getString(index));
        }
        cursor.close();
        db.close();
        return taskList;
    }

}



Aucun commentaire:

Enregistrer un commentaire