Android Studio Edit the appropriate files to create an Update function.Specifically: 1.When a user clicks a list
Fantastic news! We've Found the answer you've been seeking!
Question:
Android Studio
Edit the appropriate files to create an Update function.Specifically:
1.When a user clicks a list item, display the list item’s textin the input text area
2.When a list item’s text has been displayed in the input textarea, change the Add button to an Update button
3.When the Update button is clicked, perform the update, clearthe input text field, and change the button back to Add
Add code only to the 3 parts indicated
// add an appropriate addBtn() call here
// add your code here
// add an appropriate addBtn() call here
MainActivity.java
package com.three19.todolist;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;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 com.three19.todolist.database.ToDoListDB;import com.three19.todolist.model.ToDo;import java.util.ArrayList;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity { ToDoListDB toDoListDB; List<ToDo> arrayList; ToDoListAdapter adapter; ToDo selectedToDo; int selectedPosition; EditText txtName; Button addBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtName = (EditText) findViewById(R.id.txtName); toDoListDB = new ToDoListDB(this); arrayList = toDoListDB.getList(); adapter = new ToDoListAdapter(this, (ArrayList<ToDo>) 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) { ToDo toDo = toDoListDB.add(name); arrayList.add(toDo); adapter.notifyDataSetChanged(); txtName.setText(""); } } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectedToDo = arrayList.get(position); selectedPosition = position; txtName.setText(selectedToDo.getName()); // add an appropriate addBtn() call here } }); addBtn = (Button) findViewById(R.id.btnAdd); addBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String name = txtName.getText().toString(); // add your code here } }); Button clearBtn = (Button) findViewById(R.id.btnClear); clearBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { reset(); } }); 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(); toDoListDB.remove(toDo.getId()); reset(); } }); alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.show(); } protected void reset() { txtName.setText(""); // add an appropriate addBtn() call here selectedToDo = null; selectedPosition = -1; }}class ToDoListAdapter extends ArrayAdapter<ToDo>{ public ToDoListAdapter(Context context, ArrayList<ToDo> toDoList) { super(context, 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.getName()); return convertView; }
ToDo.java
package com.three19.todolist.model;import android.content.ContentValues;import android.database.Cursor;public class ToDo { int _id; String _name; public int getId() { return _id; } public void setId(int value) { _id = value; } public String getName() { return _name; } public void setName(String value) { _name = value; } public ContentValues getContentValuesToAdd(){ ContentValues values = new ContentValues(); values.put("name", getName()); return values; } public ContentValues getContentValuesToUpdate(){ ContentValues values = new ContentValues(); values.put("id", getId()); values.put("name", getName()); return values; } public void parse(Cursor cursor) { setId(cursor.getInt(0)); setName(cursor.getString(1)); }}
AllTasksActivity.java
package com.three19.todolist;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ListView;import com.three19.todolist.database.ToDoListDB;import com.three19.todolist.model.ToDo;import java.util.ArrayList;import java.util.List;public class AllTasksActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_all_tasks); setTitle("All Tasks"); ToDoListDB toDoListDB = new ToDoListDB(this); List< ToDo > arrayList = toDoListDB.getList(); ToDoListAdapter adapter = new ToDoListAdapter(this, (ArrayList<ToDo>) arrayList); ListView listView = (ListView) findViewById(R.id.lstView); listView.setAdapter(adapter); Button backBtn = (Button) findViewById(R.id.btnBack); backBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(getBaseContext(), MainActivity.class); startActivity(intent); } }); }}
activity_all_tasks.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" android:orientation="vertical" tools:context=".AllTasksActivity"> <Button android:id="@+id/btnBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back" /> <ListView android:id="@+id/lstView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="20px" /></LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20px" android:layout_marginBottom="20px" android:text="Your Name Here" /> <EditText android:id="@+id/txtName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20px" android:layout_marginBottom="20px" android:ems="10" android:hint="To Do Name" android:inputType="textPersonName" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add" android:layout_gravity="right"/> <Button android:id="@+id/btnClear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" /> <Button android:id="@+id/btnAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show All Tasks" /> </LinearLayout> <ListView android:id="@+id/lstView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="20px" /></LinearLayout>
Related Book For
Systems analysis and design
ISBN: 978-0136089162
8th Edition
Authors: kenneth e. kendall, julie e. kendall
Posted Date: