{
"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.