当我尝试将2个json对象推送到RecyclerView
时,它似乎只显示了一次,我使用了调试器,但仍然找不到该错误
我解析了两个对象的JSONArray
,并将其显示在我的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);
输出
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中存储每个循环,然后将其传递给我的ArrayList<Canciones>
,但它显示重复项
答案 0 :(得分:2)
如果您这样写,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())
答案 1 :(得分:1)
我已经更新了您的方法,似乎您没有将所有对象都添加到arraylist
只需使用此方法
public getJsonResults() {
ArrayList<Canciones> cancionesArrayList = new ArrayList<>();
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);
cancionesArrayList.add(canciones);
}
} catch (JSONException e) {
e.printStackTrace();
}
mAdapter = new RecyclerAdapter(this, R.layout.list_item, cancionesArrayList);
mRecyclerView.setAdapter(mAdapter);
}
现在使用
mRecyclerView = findViewById(R.id.songsRecycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
getJsonResults();