Skip to main content

Remove specific item from Recyclerview in android studio

For Full video click on below link :-

https://youtu.be/sUz4fqeanjI?si=A8AqBWCgu-5NnBOJ


Working with the activity_main.xml file.


Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/white">

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/coursename"
android:hint="Enter Course Name"
tools:ignore="Autofill,HardcodedText"
android:textColorHint="@color/black"
android:textColor="@color/black"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"/>

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/courseDur"
android:textColor="@color/black"
android:hint="Enter Course Duration"
tools:ignore="Autofill,HardcodedText"
android:textColorHint="@color/black"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"/>

<EditText
android:layout_width="350dp"
android:layout_height="50dp"
android:inputType="text"
android:id="@+id/coursedes"
android:textColor="@color/black"
android:hint="Enter Course Description"
tools:ignore="Autofill,HardcodedText"
android:textColorHint="@color/black"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/add"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="Add course"
android:textColor="@color/white"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/view"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="View Data"
android:textColor="@color/white"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/update"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="Update Data"
android:textColor="@color/white"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/delete"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:text="Delete Data"
android:textColor="@color/white"
tools:ignore="HardcodedText" />

</LinearLayout>




Working with the MainActivity.java file.


Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. 


package com.example.sqlite_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
public Button add,view,update,delete;
public EditText name,des,dur;
public OpenHelper openHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

name=findViewById(R.id.coursename);
des=findViewById(R.id.coursedes);
dur=findViewById(R.id.courseDur);
add = findViewById(R.id.add);
view = findViewById(R.id.view);
update=findViewById(R.id.update);
delete=findViewById(R.id.delete);

openHelper=new OpenHelper(MainActivity.this);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String c_name=name.getText().toString();
String c_dur=dur.getText().toString();
String c_des=des.getText().toString();

if(c_name.isEmpty() && c_dur.isEmpty() && c_des.isEmpty()){
Toast.makeText(MainActivity.this, "Oops!! look like some fields are missing!!", Toast.LENGTH_SHORT).show();
return;
}
else {
openHelper.addNewCourse(c_name,c_dur,c_des);
Toast.makeText(MainActivity.this, "Course has been added!!", Toast.LENGTH_SHORT).show();
name.setText("");
dur.setText("");
des.setText("");
}
}
});

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});

update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(MainActivity.this,MainActivity3.class);
startActivity(intent);
}
});

delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,MainActivity4.class);
startActivity(intent);
}
});

}
}



Creating a new Layout Resource file for the recyclerview adapter design.


Below is the code for the userentry.xml file. 


<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="370dp"
android:layout_height="150dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="15dp"
android:backgroundTint="#D8CFCF"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Name : "
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/textname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="10dp"
android:layout_marginStart="200dp"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Duration : "
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="50dp"
android:layout_marginStart="10dp"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/textdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="50dp"
android:layout_marginStart="200dp"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Description : "
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="90dp"
android:layout_marginStart="10dp"
tools:ignore="HardcodedText" />

<TextView
android:id="@+id/textdes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="90dp"
android:layout_marginStart="200dp"
tools:ignore="HardcodedText" />

<ImageView
android:id="@+id/delbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_delete_24"
android:layout_marginStart="330dp"
android:layout_marginTop="15dp"
tools:ignore="ContentDescription" />

</androidx.cardview.widget.CardView>



Below is the code for the MyAdapter.xml file. 


package com.example.sqlite_demo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

public Context context;
public ArrayList cou_id;
public ArrayList cou_name;
public ArrayList cou_dur;
public ArrayList cou_des;
public OpenHelper openHelper;

public int itemid;

public MyAdapter(Context context,ArrayList cou_id,ArrayList cou_name,ArrayList cou_dur,ArrayList cou_des){
this.context=context;
this.cou_id=cou_id;
this.cou_name=cou_name;
this.cou_dur=cou_dur;
this.cou_des=cou_des;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.userentry,parent,false);
return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

holder.cou_name.setText(String.valueOf(cou_name.get(position)));
holder.cou_dur.setText(String.valueOf(cou_dur.get(position)));
holder.cou_des.setText(String.valueOf(cou_des.get(position)));

holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemid = holder.getAdapterPosition();
deleteitem(itemid);
}
});

}

@Override
public int getItemCount() {
return cou_name.size();
}


public void deleteitem(int itemid){
cou_name.remove(itemid);
notifyItemRemoved(itemid);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView cou_name,cou_des,cou_dur;
ImageView btn;
public MyViewHolder(@NonNull View itemView) {

super(itemView);

cou_name=itemView.findViewById(R.id.textname);
cou_dur=itemView.findViewById(R.id.textdur);
cou_des=itemView.findViewById(R.id.textdes);
btn=itemView.findViewById(R.id.delbtn);
}
}
}


Comments

Popular posts from this blog

C++ programming language

                c++ is a general programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features.C++ runs on lots of platforms like Windows, Linux, Unix, Mac etc.   ❖❖  C++ with oops ❖❖

7. Switch Statement in c++

 ❖ C++ switch Statement :-                Consider a situation in which, only one block of code needs to be executed among many blocks. This type of situation can be handled using nested if...else statement but, the better way of handling this type of problem is using switch...case statement.  Syntax of switch:-      Switch(expression) {            Case value1 : statement1;                       Break;             Case value2 : statement2;                      Break;             Default : default statements;  }               The expression is either an integer or a character in above syntax. If the expression matches constant in case, the relevant codes a...

3. Input /output in c++

  ➢   Input/ Output in C++ :-        C++ comes with libraries which provides us many ways for performing input and output. In C++.        input and output is performed in the form of sequence of bytes or more commonly known as  streams. ● Input Stream: If the direction of flow of bytes is from device(for example: Keyboard) to the main memory then this process is called input. ● Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device(display screen ) then this process is called output. ● Header files available in C++ for Input – Output operation are: • iostream: iostream stands for standard input output stream. This header file contains definitions to objects like cin, cout, cerr etc. • fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.       ...