Fit 2 textview在一个适配器,java, android



我正在为自己创建一个笔记应用程序,我创建了一个适配器来在主屏幕上显示带有标题视图和文本视图的笔记,但是当我启动它时应用程序崩溃了这里是RecyclerViewAdapter.java。我正在使用基本的java,我没有太多的经验,所以请尽量简单地解释,这样我才能理解,


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);
}
}
}

这是XML代码,


<?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>

这是我的主要活动

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);

}
}

请指引我,我错了。

更新:感谢@stealthoust,应用程序正在运行,但在屏幕上找不到任何视图,我能做些什么。

尝试更改onBindViewHolder

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

相关内容

最新更新