Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a function that shows the student main student records (StudentID, FirstName and LastName) in a list (or table). For each row in the list,

Implement a function that shows the student main student records (StudentID, FirstName and LastName) in a list (or table). For each row in the list, include a check-box to delete the selected students. Note: the deletion can be triggered via a button or a menu option.

I have done the checkbox part so far. Instead of deleting it..it pops up a toast (im not sure how to do deletion)

//Content_main.xml

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"  android:paddingLeft="16dp"  android:paddingRight="16dp" > <ListView  android:id="@+id/listView"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentStart="true"  android:layout_marginStart="13dp"  android:layout_marginTop="60dp"  android:layout_weight="1"  android:visibility="visible" /> <Button  android:id="@+id/button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentBottom="true"  android:layout_alignParentStart="true"  android:layout_marginBottom="12dp"  android:layout_marginStart="30dp"  android:onClick="Submit"  android:text="Submit" /> RelativeLayout> 

//rowlayout.xml

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="wrap_content"  android:layout_height="wrap_content" > <ImageView  android:id="@+id/icon"  android:layout_width="42px"  android:layout_height="42px"  android:layout_marginLeft="4px"  android:layout_marginRight="10px"  android:layout_marginTop="4px"  android:src="@drawable/ic_launcher_background" > ImageView> <TextView  android:id="@+id/label"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="@+id/label"  android:textSize="40px" > TextView> <CheckBox  android:id="@+id/checkBox"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_weight="1"  android:focusable="false"/> LinearLayout> 

//MainActivity

package com.user.qs4; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ListView list; boolean[] checkBoxes; int boxIndex = 0; CustomAdapter adapter; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.activity_main); list = (ListView) findViewById(R.id.listView); String[] values = new String[]{ "Hayyat, Chorbaji, 1902038", "Samiha, Yusuf, 209030"}; checkBoxes = new boolean[values.length]; // default adapter  //ArrayAdapter adapter = new ArrayAdapter(this, R.layout.rowlayout, R.id.label, values);   // use your custom layout  adapter = new CustomAdapter(this, values); list = (ListView) findViewById(R.id.listView); list.setAdapter(adapter); } public void Submit (View v) { boolean[] checkboxes = adapter.getCheckBoxState(); String st = "You select "; for (int i = 0; i < list.getCount(); i++) { if (checkboxes[i] == true) st = st + adapter.getName(i) + " ";// list.getAdapter().getItem(i).toString();   } Toast.makeText(getApplicationContext(), st + "out of " + list.getCount() + " items! ", Toast.LENGTH_LONG).show(); } } 

//CustomAdapter

package com.user.qs4; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class CustomAdapter extends ArrayAdapter { private final Context context; private final String[] values; private boolean checked = false; // boolean array for storing  //the state of each CheckBox  boolean[] checkBoxState; ViewHolder viewHolder; public CustomAdapter(Context context, String[] values) { super(context, R.layout.rowlayout, values); this.context = context; this.values = values; checkBoxState = new boolean[values.length]; } private class ViewHolder { ImageView photo; TextView name; CheckBox checkBox; public CheckBox getCheckBox() { return checkBox; } public TextView getName() { return name; } } @Override public View getView(final int position, View convertView, ViewGroup parent) { if(convertView==null) { LayoutInflater inflater = (LayoutInflater) context  .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView=inflater.inflate(R.layout.rowlayout, null); viewHolder=new ViewHolder(); //cache the views  viewHolder.photo=(ImageView) convertView.findViewById(R.id.icon); viewHolder.name=(TextView) convertView.findViewById(R.id.label); viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox); //link the cached views to the convertview  convertView.setTag( viewHolder); } else  viewHolder=(ViewHolder) convertView.getTag(); viewHolder.name.setText(values[position]); String s = values[position]; //set icon image    //VITAL PART!!! Set the state of the  //CheckBox using the boolean array  viewHolder.checkBox.setChecked(checkBoxState[position]); //for managing the state of the boolean  //array according to the state of the  //CheckBox   viewHolder.checkBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(((CheckBox)v).isChecked()) { checkBoxState[position] = true; } else  checkBoxState[position]=false; } }); //return the view to be displayed  return convertView; } public boolean[] getCheckBoxState(){ return checkBoxState; } public String getName(int pos){ String val = values[pos]; return val; } } 

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

More Books

Students also viewed these Databases questions

Question

Identify and control your anxieties

Answered: 1 week ago

Question

Understanding and Addressing Anxiety

Answered: 1 week ago