Click here to Skip to main content
15,883,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
{ "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.
Posted
Updated 26-Jul-18 21:46pm
v2

1 solution

 
Share this answer
 
Comments
Member 13926061 27-Jul-18 0:37am    
saw the links posted and thx for ur reply.I have an internal json & not a http link is it possible to use the file path or the equivalent of it in the app to display on recyclerview? without creating a new adapter and using the existing adapter?.
and do i need to create a holder class for the viewholder to display the data along with new list to populate the data under the fragments?.
I already have a fragmentlist and a recycleradapter so was thinking of using the existing ones to populate the list, is it possible to do so? or a necessity to create a new adapter and new list?
Richard MacCutchan 27-Jul-18 4:09am    
Sorry, I don't know, you will have to try a few things. The first issue is to find a way to parse the JSON into a format that you can use in the next stage of your code. Then try and feed that data into your adapter.
Member 13926061 27-Jul-18 5:35am    
Thx for taking the time to reply and will try out a formats which i can use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900