Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm working on an app in Android studio but it keeps crashing and I don't know why. I am looking for some direction on what

I'm working on an app in Android studio but it keeps crashing and I don't know why. I am looking for some direction on what is wrong with my code.

I have to meet the following:

1. Has a Textview at the top, below that is a Listview which takes the full height of the activity, followed by a horizontal row including an Edit text, Toggle button and a Button

2. Uses a layout file for each item in the Listview

3. Has a Java object to hold each of the list items and contains a string with text "todo" and a boolean for if the Toggle button is on or not.

4. Needs a List to hold items and adapter to display them

5. when Button is clicked text from the EditText is added to the list and the EditText is then cleared

6. When the BaseAdapter is Implemented to power the Listview it must have the following:

  1. int getCount(){return list.size()}
  2. Object getItem(int position) {return list.get(position)}
  3. View getView(int position, View convertView,ViewGroup parent)
  • this gives layout that's positioned at specified row in list with the "todo" item text added.
  • if the toggle switch Is on, the background colour of that item is set to red and the text colour is set to white

4. long getItemId(int position){return position}

7. I must use an onItemLonClick() listener to Listview so that when an item of the list is selected an AlertDialog shows up with the title: "Do you want to delete this?", the message should display the "The selected row is: ", followed by the index that was clicked.

  • AlertDialog has a positive and negative button where the positive button deletes the item at the specified row and deletes the list

Below is my work so far. Any help would be appreciated as I can't figure out what is wrong with it.

Java FIle:

package com.example.androidlabs; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.ListView; import android.widget.Switch; import android.widget.TextView; import java.util.ArrayList; import java.util.Arrays; public class MainActivity extends AppCompatActivity { private ArrayList elements = new ArrayList<>( Arrays.asList( ) ); private MyListAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); EditText editText= (EditText)findViewById(R.id.editText); String listItem= editText.getText().toString(); Button addButton = findViewById(R.id.myButton); addButton.setOnClickListener( click -> { elements.add(listItem); myAdapter.notifyDataSetChanged(); }); ListView myList = findViewById(R.id.myList); myList.setAdapter( myAdapter = new MyListAdapter()); myList.setOnItemClickListener( (parent, view, pos, id) -> { elements.remove(pos); myAdapter.notifyDataSetChanged(); } ); myList.setOnItemLongClickListener( (p, b, pos, id) -> { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setTitle("Do you want to delete this?") //What is the message: .setMessage("The selected row is" + elements.indexOf(listItem)) //what the Yes button does: .setPositiveButton("Yes", (click, arg) -> { elements.remove(listItem); myAdapter.notifyDataSetChanged(); }) //What the No button does: .setNegativeButton("No", (click, arg) -> { }) //You can add extra layout elements: .setView(getLayoutInflater().inflate(R.layout.todo, null) ) //Show the dialog .create().show(); return true; }); //Whenever you swipe down on the list, do something: SwipeRefreshLayout refresher = findViewById(R.id.refresher); refresher.setOnRefreshListener( () -> refresher.setRefreshing(false) ); } private class MyListAdapter extends BaseAdapter { public int getCount() { return elements.size(); } public Object getItem(int position) { return elements.get(position); } public long getItemId(int position) { return (long) position; } public View getView(int position, View old, ViewGroup parent) { View newView = old; LayoutInflater inflater = getLayoutInflater(); //make a new row: if(newView == null) { newView = inflater.inflate(R.layout.todo, parent, false); } //set what the text should be for this row: TextView tView = newView.findViewById(R.id.textGoesHere); tView.setText( getItem(position).toString() ); Switch urgent = (Switch) findViewById(R.id.toggle); urgent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { tView.setBackgroundColor(Color.RED); tView.setTextColor(Color.WHITE); } }); //return it to be put in the table return newView; } }}

main_activity.xml

Step by Step Solution

3.51 Rating (161 Votes )

There are 3 Steps involved in it

Step: 1

Improved Code with Explanations MainActivityjava Java package comexampleandroidlabs import androidxappcompatappAlertDialogimport androidxappcompatappAppCompatActivityimport androidgraphicsColorimport ... 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

Organizational Behavior

Authors: Andrzej A. Huczynski, David A. Buchanan

8th Edition

273774816, 273774815, 978-0273774815

More Books

Students also viewed these Programming questions

Question

Evaluate each expression if possible. -V0.49

Answered: 1 week ago