在适配器,Java,Android中拟合2个文本视图

发布于 2025-02-13 04:43:28 字数 3790 浏览 1 评论 0原文

我正在为自己创建一个笔记应用程序,我创建了一个适配器,可以在主屏幕上显示带有标题视图和文本视图的笔记,但是当我启动它时,该应用程序会崩溃 这是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);



    }
}


请指导我错误。

更新:多亏了@StealtheStt,应用程序正在运行,但在屏幕上找不到任何视图,我该怎么办。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2025-02-20 04:43:29

上更改此

尝试在OnBindViewHolder holder.titleview.setText(arr [position]) ; holder.textview.setText(arr2 [position]);

Try to change this on onBindViewHolder

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文