gpt4 book ai didi

java - Android java 中的 Recyclerview

转载 作者:行者123 更新时间:2023-12-02 02:04:44 25 4
gpt4 key购买 nike

回收器 View 没有迭代到数组的长度。这是代码,我缺少什么。我遵循了描述整个过程的指南,在该指南中,数组的所有项目在 ListView 中都是可见的。这是托管 recyclerView 的主要 Activity 文件,位于过滤数组并弹出 ListView 的适配器 java 类下方。请帮助我

PickupActivity.java

package com.demo.driverconsole;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerViewAccessibilityDelegate;
import android.util.Log;
import android.widget.Adapter;
import android.widget.Toast;
import java.util.ArrayList;

public class pickupActivity extends AppCompatActivity {
private static final String TAG = "pickupActivity";
private RecyclerView studentList;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<String> myDataset = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pickup);
myDataset.add("John Doe");
myDataset.add("Jane Doe");
myDataset.add("Susan");
initRecycler();

}

public void initRecycler() {
Log.d(TAG, "Called");
RecyclerView recyclerView = findViewById(R.id.recyclerView);
StudentListAdapter adapter = new StudentListAdapter(myDataset);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}

StudentListAdapter.java

package com.datastoneglobal.driverconsole;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;

public class StudentListAdapter extends RecyclerView.Adapter<StudentListAdapter.ViewHolder> {

private static final String TAG = "StudentListAdapter";
private ArrayList<String> names = new ArrayList<>();

public StudentListAdapter(ArrayList<String> names) {
Log.d(TAG, "StudentListAdapter: " + names);
this.names = names;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_item, parent, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.text.setText(names.get(position));
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
TextView text;
RelativeLayout layout;
public ViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.textView);
}
}


}

ActivityPickup.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

StudentItem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/layout"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Loading ...">
</TextView>
</RelativeLayout>

最佳答案

代码看起来正确,我发现的唯一问题是 StudentItem.xmlRelativeLayout 的高度

StudentItem.xml 中将 RelativeLayout 的高度更改为 android:layout_height="wrap_content"

示例代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/layout"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Loading ...">
</TextView>
</RelativeLayout>

编辑

使用这个

private  StudentListAdapter adapter;

而不是这个

private  RecyclerView.Adapter adapter;

关于java - Android java 中的 Recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50987059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com