在recyclerview中显示下面的json数据。 [英] Display the json data below in recyclerview.
问题描述
{
values:[{
deviceParam:11,
阅读:0
},{
deviceParam:12,
阅读:14910
},{
deviceParam:14,
阅读:1
},{
deviceParam :13,
阅读:254
}],
deviceId:Huelight2,
deviceType:0,
roomType:0,
noOfValues:4
}
MainActivity:
公共类MainActivity扩展AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
ViewPager viewpager = findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
String json = {
values:[{
deviceParam:11,
阅读:0
},{
deviceParam:12,
阅读:14910
},{
deviceParam:14,
阅读:1
} ,{
deviceParam:13,
阅读:254
}],
deviceId:Huelight2,
deviceType:0,
roomType:0,
noOfValues:4
}
JSONObject rootJsonObj = new JSONObject();
try {
rootJsonObj = new JSONObject(json);
JSONArray items = new JSONArray(rootJsonObj.get(deviceDetails)。toString());
for(int i = 0;我<的Integer.parseInt(rootJsonObj.get( configuredDevices)的toString()); i ++){
{
JSONObject item = items.getJSONObject(i);
System.out。 println(jsonObject+ i +:+ item);
}
}
} catch(异常e){
Log.d(JSONFeedTask,e.getLocalizedMessage());
}
}
类ViewPagerAdapter扩展FragmentPagerAdapter {
私人最终列表< fragmentlist> mFragmentList = new ArrayList<>();
private final List< string> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager){
super(manager);
}
@Override
public FragmentList getItem(int position){
返回mFragmentList。得到(位置);
}
@Override
public int getCount(){
返回mFragmentList.size();
}
public void addFragment(FragmentList fragment,String title){<< br / >
mFragmentList.add(片段);
mFragmentTitleList.add(标题);
}
@Override
public CharSequence getPageTitle(int position){
返回mFragmentTitleList.get(position);
}
}
}
Myrecycleradapter:< br $>
公共类MyItemRecyclerViewAdapter扩展了RecyclerView.Adapter< myitemrecyclerviewadapter.viewholder> {
私人最终名单< dummycontent.dummyitem> mValues;
private final FragmentList.OnListFragmentInteractionListener mListener;
public MyItemRecyclerViewAdapter(List< dummycontent.dummyitem> items,FragmentList.OnListFragmentInteractionListener listener){
mValues = items;
mListener =听众;
}
@覆盖
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View view = LayoutInflater.from(parent.getContext())
。 inflate(R.layout.fragment_item_list,parent,false);
返回新的ViewHolder(查看);
}
@Override
public void onBindViewHolder(最终ViewHolder持有者,int位置){
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(p ()。内容);
holder.mView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(查看v){
if(null!= mListener){
//通知活动回调界面(活动,如果
//片段附加到一个项目已被选中。
mListener.onListFragmentInteraction(holder.mItem);
}
}
});
}
@Override
public int getItemCount(){
返回mValues.size();
}
公共课ViewHolder扩展了RecyclerView.ViewHolder {
public final查看mView;
公共最终TextView mIdView;
公共最终TextView mContentView;
public DummyContent.DummyItem mItem;
public ViewHolder(View view){
super(view);
mView = view;
mIdView =(TextView)view.findViewById(R.id.editText);
mContentView =(TextView)view.findViewById(R.id.editText2 );
}
@Override
public String toString(){
返回super.toString()+'+ mContentView.getText()+';
}
}
} < br $>
FragmentList:
公共类FragmentList扩展片段{
// TODO :自定义参数参数名称
private static final String ARG_COLUMN_COUNT =column-count;
// TODO:自定义参数
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
私人名单< string> list;
public FragmentList(){
list = getArguments()。getStringArrayList(data);
//Toast.makeText(,,Toast.LENGTH_SHORT).show();
System.out.println(list.toString());
if(getArguments()!= null){
mColumnCount = getArguments()。getInt(ARG_COLUMN_COUNT);
}
}
// TODO:自定义参数初始化
@SuppressWarnings(unused)
public static FragmentList newInstance(int columnCount ){
FragmentList fragment = new FragmentList();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT,columnCount) ;
fragment.setArguments(args);
返回片段;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public查看onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){
查看视图= inflater.inflate(R.layout.fragment_item,容器,false);
//设置适配器
if(查看instanceof RecyclerView){
上下文context = view.getContext();
RecyclerView recyclerView =(RecyclerView)视图;
if(mColumnCount< = 1){
recyclerView.setLayoutManager(new LinearLayoutManager(context));
}
else {
recyclerView.setLayoutManager(new GridLayoutManager(context,mColumnCount));
}
recyclerView.setAdapter(new MyItemRecyclerViewAdapter (DummyContent.ITEMS,mListener));
}
返回视图;
}
我是什么已经尝试过:
我能够读取json并且能够覆盖消息。我需要在recyclerview下显示数据但是无法得到如何解析来自内部json文件的数据,因为大多数在线示例都使用http链接。我已经实现了recyclerview以及片段列表,因此在显示部分需要一些指针。或者我使用错误的概念来显示数据?。我第一次使用java和android概念。
i有片段列表和recyclerviewadapter可以填充使用上面的2个组件代码。
android parse json - Google搜索 [ ^ ]
{
"values": [{
"deviceParam": "11",
"reading": "0"
}, {
"deviceParam": "12",
"reading": "14910"
}, {
"deviceParam": "14",
"reading": "1"
}, {
"deviceParam": "13",
"reading": "254"
}],
"deviceId": "Huelight2",
"deviceType": "0",
"roomType": "0",
"noOfValues": "4"
}
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
ViewPager viewpager = findViewById(R.id.pager);
ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
String json = {
"values": [{
"deviceParam": "11",
"reading": "0"
}, {
"deviceParam": "12",
"reading": "14910"
}, {
"deviceParam": "14",
"reading": "1"
}, {
"deviceParam": "13",
"reading": "254"
}],
"deviceId": "Huelight2",
"deviceType": "0",
"roomType": "0",
"noOfValues": "4"
}
JSONObject rootJsonObj = new JSONObject();
try {
rootJsonObj = new JSONObject(json);
JSONArray items = new JSONArray(rootJsonObj.get("deviceDetails").toString());
for (int i = 0; i < Integer.parseInt(rootJsonObj.get("configuredDevices").toString()); i++) {
{
JSONObject item = items.getJSONObject(i);
System.out.println("jsonObject " + i + ": " + item);
}
}
} catch (Exception e) {
Log.d("JSONFeedTask", e.getLocalizedMessage());
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<fragmentlist> mFragmentList = new ArrayList<>();
private final List<string> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public FragmentList getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(FragmentList fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Myrecycleradapter:
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<myitemrecyclerviewadapter.viewholder> {
private final List<dummycontent.dummyitem> mValues;
private final FragmentList.OnListFragmentInteractionListener mListener;
public MyItemRecyclerViewAdapter(List<dummycontent.dummyitem> items, FragmentList.OnListFragmentInteractionListener listener) {
mValues = items;
mListener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_item_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mListener.onListFragmentInteraction(holder.mItem);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyContent.DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.editText);
mContentView = (TextView) view.findViewById(R.id.editText2);
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
FragmentList:
public class FragmentList extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private List<string> list;
public FragmentList() {
list = getArguments().getStringArrayList("data");
//Toast.makeText(, "", Toast.LENGTH_SHORT).show();
System.out.println(list.toString());
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static FragmentList newInstance(int columnCount) {
FragmentList fragment = new FragmentList();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1){
recyclerView.setLayoutManager(new LinearLayoutManager(context));
}
else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
}
return view;
}
What I have tried:
I am able to read json and can toast the message.I need to display the data under a recyclerview but cant quite get how to parse the data from a internal json file as most of online examples are using http links.I have already implemented the recyclerview along with a fragment list so need some pointers in the display part. or am i using the wrong concept to display the data?.My first time working with java and android concepts.
i have the fragmentlist and the recyclerviewadapter is it possible to populate the using the 2 components of the above code.
android parse json - Google Search[^]
这篇关于在recyclerview中显示下面的json数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!