Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The assignment is to create a multiscreen To Do List app using incomplete code(please do not comment out existing code). The instructor has already discovered

The assignment is to create a multiscreen To Do List app using incomplete code(please do not comment out existing code). The instructor has already discovered two errors in the Assignment

mainactivity.java add 'implements serializable' to your ToDo class:

class ToDo implements Serializable { Integer id; String name; }

In your onTasksClicked go ahead and use (Serializable) to cast your arraylist:

public void onTasksClicked(View view) { Intent intent = new Intent(this, AllTasksActivity.class); intent.putExtra("array", (Serializable) arrayList); startActivity(intent); }

for your alltasksactivity.java you will then access that list as follows:

Intent i = getIntent(); arrayList = (ArrayList) i.getSerializableExtra("array");
  1. Display all tasks on a new All Tasks screen when the Show All Tasks button is clicked
  2. Provide navigation from the All Tasks screen back to the main screen.
  3. I have completed the XML files however I cannot initialize the ToDo Class in MainActivity or the ToDoList adaptor
    MainActivity.java package com.three19.todolist; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.IntegerRes; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { List ArrayList ToDoListAdapter adapter; Integer lastKeyID = 0; EditText editToDo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editToDo = findViewById(R.id.txtName); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); Map allSharedPreferences = preferences.getAll(); arrayList = new ArrayList(); for (Map.Entry entry : allSharedPreferences.entrySet()) { ToDo toDo = new ToDo(); toDo.id = Integer.parseInt(entry.getKey()); toDo.name = entry.getValue().toString(); arrayList.add(toDo); lastKeyID = toDo.id; } adapter = new ToDoListAdapter(this, (ArrayList) arrayList); ListView listView = (ListView) findViewById(R.id.lstView); listView.setAdapter(adapter); listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView arg0, View arg1, int position, long arg3) { removeItemFromList(position); return true; } }); Button addBtn = (Button) findViewById(R.id.btnAdd); addBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { EditText txtName = (EditText) findViewById(R.id.txtName); String name = txtName.getText().toString(); if (name.trim().length() > 0) { lastKeyID++; ToDo toDo = new ToDo(); toDo.id = lastKeyID; toDo.name = name; arrayList.add(toDo); adapter.notifyDataSetChanged(); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = preferences.edit(); editor.putString(toDo.id.toString(), toDo.name); editor.apply(); txtName.setText(""); } } }); Button clearBtn = (Button) findViewById(R.id.btnClear); clearBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { EditText txtName = (EditText) findViewById(R.id.txtName); txtName.setText(""); } }); // button listener goes here Button allBtn = (Button) findViewById(R.id.btnAll); allBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(getBaseContext(), AllTasksActivity.class); startActivity(intent); } protected void removeItemFromList(final int position) { AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("Delete"); alert.setMessage("Do you want delete this item?"); alert.setPositiveButton("YES", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToDo toDo = arrayList.get(position); arrayList.remove(position); adapter.notifyDataSetChanged(); adapter.notifyDataSetInvalidated(); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = preferences.edit(); editor.remove(toDo.id.toString()); editor.apply(); } }); alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.show(); } } class ToDo { Integer id; String name; } class ToDoListAdapter extends ArrayAdapter { public ToDoListAdapter(Context context, ArrayList) toDoList) super(context, resource:0, toDolist); } @Override public View getView(int position, View convertView, ViewGroup parent) { ToDo toDo = getItem(position); if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false); } TextView name = (TextView) convertView.findViewById(android.R.id.text1); name.setText(toDo.name); return convertView; } }

AllTasksAcitivity.java

package com.three19.todolist; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; import java.util.List; import java.util.Map; public class AllTasksActivity extends AppCompatActivity { private ArrayList arrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent i = getIntent(); arrayList = (ArrayList) i.getSerializableExtra("array"); } } 

MainActivity.xml

     

activity_all_tasks.xml

   

AGAIN please do not comment out code to make the code function properly and please add comments that provide a good explanation of what the program is doing.

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

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions