gpt4 book ai didi

android - 如何解析本地 JSON 并填充 RecyclerView?

转载 作者:行者123 更新时间:2023-11-30 00:38:36 26 4
gpt4 key购买 nike

我对如何解析本地 Json 感到困惑。我尝试了很多,但找不到解决方案。
我想填充一个仅包含一个简单标题的 ArrayList - 我想将该名称从 JSON 添加到 RecylerView。

{
"hollywood": [{
"_id": "58d3264f93f24d2bc87bebf5",
"title": "sagar rana",
"image": "encrypted-tbn1.gstatic.com/...",
"genre": "hollywood",
"description": "This for only demo purpose",
"release": 2010,
"download": "No download",
"youtube": "youtube.com/embed/C-5EOd8xavw...",
"__v ": 0,
"upload ": "2017-03-23T01:35:11.182Z"
}]
}

主要 Activity .java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import static android.R.attr.data;

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
MyAdapter recycleradapter;
private ArrayList<Moviedemo> mArrayList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("hollywood");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;

for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
Log.d("Details-->", jo_inside.getString("hollywood"));
String formula_value = jo_inside.getString("hollywood");
String url_value = jo_inside.getString("url");

//Add your values in your `ArrayList` as below:
m_li = new HashMap<String, String>();
m_li.put("formule", formula_value);
m_li.put("url", url_value);

formList.add(m_li);
}
} catch (JSONException e) {
e.printStackTrace();
}
recycleradapter = new MyAdapter(mArrayList);
recyclerView.setAdapter(recycleradapter);

recyclerViewstart();
loadJSONFromAsset();


}

private String loadJSONFromAsset() {
String json = null;
try {

InputStream is = getAssets().open("homepage.json");

int size = is.available();

byte[] buffer = new byte[size];

is.read(buffer);

is.close();

json = new String(buffer, "UTF-8");


} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;


}

private void recyclerViewstart() {
recyclerView=(RecyclerView)findViewById(R.id.recylce);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);

}
}

最佳答案

这就是您的适配器。

recycleradapter = new MyAdapter(mArrayList);
recyclerView.setAdapter(recycleradapter);

您从未初始化过mArrayList...所以先初始化

mArrayList = new ArrayList<Moviedemo>();

然后,您需要添加到该列表以确定要在 JSON 中解析的内容。

您已经展示了如何获取数组和字符串。

I want to add title from json to recyler view

只是标题?作为一个字符串?那为什么适配器持有整个Moviedemo对象呢?

// Set the adapter
recycleradapter = new MyAdapter(mArrayList);
recyclerView.setAdapter(recycleradapter);

// Open and parse file
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("hollywood");

for (int i = 0; i < m_jArry.length(); i++) {
// Start an object
Moviedemo movie = new Moviedemo();

// Parse the JSON
JSONObject jo_inside = m_jArry.getJSONObject(i);
String title = jo_inside.getString("title");

// Build the object
movie.setTitle(title);

// Add the object to the list
mArrayList.add(movie);
}

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

// Update adapter
recycleradapter.notifyDataSetChanged();

关于android - 如何解析本地 JSON 并填充 RecyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42956761/

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