i get stuck in my data in RecyclerView
. I have a json in asset folder like this :
"tTisch": {
"t-tisch": [
{
"tischnr": 1,
"departement": 1,
"normalbeleg": 0,
"kellner-nr": 0,
"bezeich": "TABLE 01",
"roomcharge": false,
"betriebsnr": 0
},
{
"tischnr": 2,
"departement": 1,
"normalbeleg": 0,
"kellner-nr": 0,
"bezeich": "TABLE 02",
"roomcharge": false,
"betriebsnr": 0
},
{
"tischnr": 3,
"departement": 1,
"normalbeleg": 0,
"kellner-nr": 0,
"bezeich": "TABLE 03",
"roomcharge": false,
"betriebsnr": 0
},
.......
I want to load this data into my RecyclerView
and CardView
. I have read many tutorials but there is none regarding my case. Every tutorial for RecyclerView
always loads json from internet. This is my codes :
Item.java
public class Item {
private String txt_no_table;
private String txt_pax;
private String txt_time;
private String txt_guestname;
private String txt_bill;
Item(String txt_no_table, String txt_pax, String txt_time, String txt_guestname, String txt_bill) {
this.txt_no_table = txt_no_table;
this.txt_pax = txt_pax;
this.txt_time = txt_time;
this.txt_guestname = txt_guestname;
this.txt_bill = txt_bill;
}
public String getTxt_no_table() {
return txt_no_table;
}
public String getTxt_pax() {
return txt_pax;
}
public String getTxt_time() {
return txt_time;
}
public String getTxt_guestname() {
return txt_guestname;
}
public String getTxt_bill() { return txt_bill; }
}
This is Adapter.java :
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
@SuppressWarnings("unused")
private static final String TAG = Adapter.class.getSimpleName();
private List<Item> items;
Context context;
List<Item> item;
public Adapter() {
super();
this.item = item;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_occupied, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Item item = items.get(position);
holder.txt_no_table.setText(item.getTxt_no_table());
holder.txt_pax.setText(item.getTxt_pax());
holder.txt_time.setText(item.getTxt_time());
holder.txt_guestname.setText(item.getTxt_guestname());
holder.txt_bill.setText(item.getTxt_bill());
}
@Override
public int getItemCount() {
return items.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView txt_no_table;
TextView txt_pax;
TextView txt_time;
TextView txt_guestname;
TextView txt_bill;
public ViewHolder(View itemView) {
super(itemView);
txt_no_table= (TextView) itemView.findViewById(R.id.txt_no_table);
txt_pax = (TextView) itemView.findViewById(R.id.txt_pax);
txt_time = (TextView) itemView.findViewById(R.id.txt_time);
txt_guestname = (TextView) itemView.findViewById(R.id.txt_guestname);
txt_bill = (TextView) itemView.findViewById(R.id.txt_bill);
}
}
}
This is my Activity :
public class TableActivity extends AppCompatActivity {
@SuppressWarnings("unused")
private static final String TAG = TableActivity.class.getSimpleName();
List<Item> items;
RecyclerView recyclerView;
RecyclerView.Adapter recyclerViewadapter;
JSONArray array;
public static String AssetJSONFile (String filename, Context context) throws IOException {
AssetManager manager = context.getAssets();
InputStream file = manager.open(filename);
byte[] formArray = new byte[file.available()];
file.read(formArray);
file.close();
return new String(formArray);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table);
try {
loadJSONFromAsset();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
recyclerView = (RecyclerView) findViewById(R.id.table_rv);
items = new ArrayList<>();
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 6));
//recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public void loadJSONFromAsset() throws IOException, JSONException {
JSONObject json = null;
Item items2 = new Item();
InputStream is = getAssets().open("tableplan.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
for (int i = 0; i<array.length(); i++) {
try {
json = array.getJSONObject(i);
items2.set_txt_no_table(json.getString("bezeich"));
items2.set_txt_pax(json.getString("normalbeleg"));
items2.set_txt_time(json.getString("betriebsnr"));
items2.set_txt_guestname(json.getString("betriebsnr"));
items2.set_txt_bill(json.getString("betriebsnr"));
} catch (JSONException e) {
e.printStackTrace();
}
items.add(items2);
}
recyclerViewadapter = new Adapter(items);
recyclerView.setAdapter(recyclerViewadapter);
}
My referencec : https://www.codeproject.com/tips/631546/parse-json-data-in-android EDITED: my app force stop and i get this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.development_laptop.vhp_restotemp, PID: 7897
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.development_laptop.vhp_restotemp/com.example.development_laptop.vhp_restotemp.TableActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
at com.example.development_laptop.vhp_restotemp.TableActivity.loadJSONFromAsset(TableActivity.java:79)
at com.example.development_laptop.vhp_restotemp.TableActivity.onCreate(TableActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Every answer is very helpful for me. Thanks in advance