Home > database >  Fit 2 textViews in an adapter , java , android
Fit 2 textViews in an adapter , java , android

Time:07-05

I am creating a note-taking app for myself, I created an adapter to show notes on the main screen with a title view and text view but the app crashes when I launch it here is RecyclerViewAdapter.java. I am using basic java, I don't have much experience so please try to explain simply so could I understand,


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.myViewHolder> {

    String[] arr;
    String[] arr2;

    public RecyclerViewAdapter(String[] arr, String[] arr2) {
        this.arr = arr;
        this.arr2 = arr2;
    }

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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view, parent,
                false);
        return new myViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
        holder.titleView.setText(position);
        holder.textView.setText(position);

    }

    @Override
    public int getItemCount() {
        return arr.length;
    }

    public class myViewHolder extends RecyclerView.ViewHolder {
        TextView titleView;
        TextView textView;


        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            titleView = itemView.findViewById(R.id.text_title_view);
            textView = itemView.findViewById(R.id.text_text_view);

        }
    }
}

Here is the XML code,


    <?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="wrap_content"
    android:orientation="vertical">


    <TextView
        android:id="@ id/text_title_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="start"
        android:text="Title" />

    <TextView
        android:id="@ id/text_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="start"
        android:text="Body will go here" />
</LinearLayout>


and here is my main activity

    package com.example.keepnotes;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;


public class MainActivity extends AppCompatActivity {


    RecyclerView.LayoutManager layoutManager;
    private RecyclerView recyclerView;
    private Toolbar toolbar1;
    RecyclerViewAdapter recyclerViewAdapter;
    String []arr = {"First Heading ","Second Heading"};
    String []arr2 = {"First body ","Second body"};

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

        //setting up recycler view
        recyclerView = findViewById(R.id.recycler_view);
        layoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerViewAdapter= new RecyclerViewAdapter(arr, arr2);
        recyclerView.setAdapter(recyclerViewAdapter);



    }
}


please guide me that where I am mistaken.

update: Thanks to @stealthoust, App is running but can't find any view on the screen, what can I do about that.

CodePudding user response:

Try to change this on onBindViewHolder

holder.titleView.setText(arr[position]); holder.textView.setText(arr2[position]);

  • Related