vendredi 9 avril 2021

Display checked items on Button click using a RecyclerView

Ive an app which will get data(movie titles) from the sqlite database and display in a custom recycler view ,In the layout contains a button with the Id clickme and a recycler view.The custom recyclerview contains a 2 textboxes(movie number(id) and title ) and a checkbox. What I want to do is to display the checked movie names using a toast message or a system.out.println when the user clicks on the button.Please help me figure it out!

MainActivity class

public class MainActivity extends AppCompatActivity {

DataBaseHelper myDB;
ArrayList<String> movie_id,movie_title;
DisplayCustomAdapter displayCustomAdapter;
RecyclerView recyclerView;

Button clickMe;
CheckBox checkBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.display_recyclerView);
    clickMe=(Button) findViewById(R.id.clickMe);
    checkBox=(CheckBox)findViewById(R.id.checkBox);

    myDB = new DataBaseHelper(MainActivity.this);
    movie_id = new ArrayList<>();
    movie_title = new ArrayList<>();

    displayData();

    displayCustomAdapter = new DisplayCustomAdapter(MainActivity.this,movie_id,movie_title);
    recyclerView.setAdapter(displayCustomAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}

public void buttonAction(){
    clickMe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

DisplayCustomAdapter class

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

private Context context;
private ArrayList display_id,display_title;

DisplayCustomAdapter(Context context,ArrayList display_id,ArrayList display_title){
    this.context=context;
    this.display_id=display_id;
    this.display_title=display_title;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.display_row,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.display_id_txt.setText(String.valueOf(display_id.get(position)));
    holder.display_title_txt.setText(String.valueOf(display_title.get(position)));
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView display_id_txt, display_title_txt;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        display_id_txt = itemView.findViewById(R.id.displayM_id);
        display_title_txt = itemView.findViewById(R.id.displayM_title);

    }
}
}


public void displayData(){
    Cursor cursor = myDB.getAllData();
    if(cursor.getCount()==0){
        Toast.makeText(this,"No data", Toast.LENGTH_SHORT).show();
    }else{
        while(cursor.moveToNext()){
            movie_id.add(cursor.getString(0));
            movie_title.add(cursor.getString(1));
        }
    }
}
}

DataBaseHelper class Method related to getting data from the database

public Cursor getAllData(){
    String query=" SELECT * FROM "+TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = null;
    if (db != null){
        cursor=db.rawQuery(query,null);
    }
    return cursor;
}

display_row.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="wrap_content">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/displayM_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:textSize="30sp"
            android:text="1" />

        <TextView
            android:id="@+id/displayM_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/displayM_id"
            tools:layout_editor_absoluteY="28dp" />

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/displayM_title"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/display_recyclerView"
    android:layout_width="415dp"
    android:layout_height="656dp"
    android:paddingTop="?attr/actionBarSize"
    app:layout_constraintBottom_toTopOf="@+id/clickMe"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

    <Button
    android:id="@+id/clickMe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    tools:layout_editor_absoluteX="288dp" />
   </androidx.constraintlayout.widget.ConstraintLayout>



Aucun commentaire:

Enregistrer un commentaire