Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify this code, My requirement is I need a todo list with an option to add items Upto 7 days. In the below code, it

Modify this code, My requirement is I need a todo list with an option to add items Upto 7 days.

In the below code, it is a todo list which adds item and delete item but I need an option to add items Upto 7 days.

------------------------ activity_main.xml ----------------------------------

 
      

------------------ MainActivity.java ---------------------

 import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; import androidx.appcompat.app.AlertDialog; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener { private EditText ed; private ImageView adding_items; private ListView item_list; private ArrayList values=new ArrayList(); private ArrayAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed = findViewById(R.id.text_edit); adding_items = findViewById(R.id.add_text); item_list = findViewById(R.id.listview); adding_items.setOnClickListener(this); item_list.setOnItemLongClickListener(this); } @Override public void onClick(View view) { String add_item=ed.getText().toString(); if(values.contains(add_item)) { Toast.makeText(getBaseContext(),"Item Already Exist", Toast.LENGTH_LONG).show(); } else { values.add(add_item); adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,values); item_list.setAdapter(adapter); ed.setText(""); } } @Override public boolean onItemLongClick(AdapterView adapterView, View view, int position, long l) { final int removing_item=position; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("Do you want to delete").setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { values.remove(removing_item); adapter.notifyDataSetChanged(); Toast.makeText(getBaseContext(), "Item Deleted", Toast.LENGTH_LONG).show(); } }).setNegativeButton("Cancel", null).show(); return true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 9 For Linux UNIX And Windows Advanced Database Administration Certification Certification Study Guide

Authors: Roger E. Sanders, Dwaine R Snow

1st Edition

1583470808, 978-1583470800

More Books

Students also viewed these Databases questions