gpt4 book ai didi

java - RecyclerView 适配器没有填满我的数据数组

转载 作者:行者123 更新时间:2023-12-01 19:47:44 24 4
gpt4 key购买 nike

似乎当我尝试将 2 个 json 对象推送到我的 RecyclerView 时它只显示一次,我已经使用了调试器但仍然找不到错误

我正在解析 JSONArray 2 个对象并将其显示到我的 RecyclerView

解析器

  public Canciones getJsonResults(){

Canciones canciones = new Canciones();
InputStream is = getResources().openRawResource(R.raw.song_data);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}

}catch (IOException e) {
e.printStackTrace();
}

finally {

try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

String jsonString = writer.toString();
try {

JSONArray objects = new JSONArray(jsonString);
for (int i = 0; i < objects.length(); i++) {
JSONObject cancionesObj = objects.getJSONObject(i);
int id = cancionesObj.getInt("id");
String song_title = cancionesObj.getString("song_title");
String album = cancionesObj.getString("album");
String cover = cancionesObj.getString("cover");
String song_id = cancionesObj.getString("song_id");
String artist = cancionesObj.getString("artist");
canciones.setId(id);
canciones.setSongTitle(song_title);
canciones.setAlbum(album);
canciones.setCover(cover);
canciones.setSongId(song_id);
canciones.setArtist(artist);
}

} catch (JSONException e) {
e.printStackTrace();
}


return canciones;
}

我将它添加到我的 ArrayList 中,如下所示

mRecyclerView = findViewById(R.id.songsRecycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
misCanciones.add(getJsonResults());
mAdapter = new RecyclerAdapter(this,R.layout.list_item,misCanciones);
mRecyclerView.setAdapter(mAdapter);

输出

enter image description here

Json数据结构

[
{
"id": 1,
"song_title": "Master Of Puppets",
"album": "Master Of Puppets",
"artist": "Metallica",
"cover": "metallica_mop",
"song_id": "biumbis"
},
{
"id": 2,
"song_title": "Flight Of Icarus",
"album": "Piece Of Mind",
"artist": "Iron Maiden",
"cover": "iron_pom",
"song_id": "biumbis"
}
]

list_item.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="horizontal">

<ImageView
android:layout_width="50dp"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:id="@+id/rowImage"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rowText"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
/>



</LinearLayout>

适配器

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MusicaViewHolder> {

private Context mContext;
private int mLayoutResourceId;
private ArrayList<Canciones> mArrayListCanciones;

public RecyclerAdapter(Context mContext, int mLayoutResourceId, ArrayList<Canciones> mCanciones) {
this.mContext = mContext;
this.mLayoutResourceId = mLayoutResourceId;
this.mArrayListCanciones = mCanciones;
}



@NonNull
@Override
public MusicaViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(mContext).inflate(mLayoutResourceId, viewGroup, false);
return new MusicaViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull MusicaViewHolder musicaViewHolder, int position) {

Canciones canciones = mArrayListCanciones.get(position);

int drawableId = mContext.getResources().getIdentifier(canciones.getCover(), "drawable", mContext.getPackageName());
musicaViewHolder.mRowImage.setImageResource(drawableId);
musicaViewHolder.mTextImage.setText(canciones.getSongTitle());
}

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

@Override
public int getItemViewType(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

public class MusicaViewHolder extends RecyclerView.ViewHolder{

private ImageView mRowImage;
private TextView mTextImage;

public MusicaViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mRowImage = itemView.findViewById(R.id.rowImage);
mTextImage = itemView.findViewById(R.id.rowText);


}

似乎 JSONArray 中的 for 循环仅采用最后一个值,我正在考虑将每个循环存储在另一个数组列表中,然后将其传递给我的 ArrayList<Canciones>但它显示重复项

最佳答案

如果你这样写,getJsonResults() 方法只会返回最后一个值。

编辑getJsonResults()方法:

public ArrayList<Canciones> getJsonResults(){
ArrayList<Canciones> cancioesList = new ArrayList();

InputStream is = getResources().openRawResource(R.raw.song_data);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}

}catch (IOException e) {
e.printStackTrace();
}

finally {

try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

String jsonString = writer.toString();
try {

JSONArray objects = new JSONArray(jsonString);
for (int i = 0; i < objects.length(); i++) {
Canciones canciones = new Canciones();
JSONObject cancionesObj = objects.getJSONObject(i);
int id = cancionesObj.getInt("id");
String song_title = cancionesObj.getString("song_title");
String album = cancionesObj.getString("album");
String cover = cancionesObj.getString("cover");
String song_id = cancionesObj.getString("song_id");
String artist = cancionesObj.getString("artist");
canciones.setId(id);
canciones.setSongTitle(song_title);
canciones.setAlbum(album);
canciones.setCover(cover);
canciones.setSongId(song_id);
canciones.setArtist(artist);
cancioesList.add(canciones);
}

} catch (JSONException e) {
e.printStackTrace();
}


return cancioesList;
}

当您获得 Canciones 对象列表时,使用以下方法添加到misCanciones:

misCanciones.addAll(getJsonResults())

关于java - RecyclerView 适配器没有填满我的数据数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52509961/

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