Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying data in Recyclerview from a JSONobject single string with multiple values

I have a DataList jsonarray with a jsonobject as Data , the string has different values which is seprated by character "´" , the values are respectively corresponding to the "Headers" object , i need to display this in a recycler view as SL.,InNo,etc., how can i achieve this by spliting the characher "´" which gives a string array,i furthur need to display this data from adapter to different textview, any ideas would be really helpful.

   "MainData": {
       "Headers": "SL.>´InNo. - Supp<´InvNo.<´Date^´Value>´Disc.>´Rate´Others>´Amount>",
       "FieldSeparator": "´",
       "DataList": [
           {
               "Data": "1. ´19110 / Textiles´003220´01-sep-2019´70,605.00´0.00´530.25´982.75´118.00´",
               "DataInputType": 1
           },
            {
               "Data": "2. ´19111 / Textiles´7041´01-sep-2019´8,895.00´0.00´444.75´173.25´513.00´",
               "DataInputType": 1
           },

like image 330
Android_id Avatar asked May 04 '20 08:05

Android_id


2 Answers

You have multiply approaches in order to preform that task,

first of all extract the needed information into string then you can use

replace function to change '`' into '' read more about string handling in java

extraction: Converting JSON data to Java object

replace function: How to remove special characters from a string?

like image 61
dt170 Avatar answered Nov 09 '22 08:11

dt170


Assuming you want your data to be in a usable structure like that.

 [
    {
        "SL" : "1"
        "InNo": "19910"
        ...
    },
    {
        "SL" : "2"
        "InNo": "19911"
        ...
    }

 ]

As others have mentioned the idea is to use the split("´") the rest are how you want to structure you data.

Use a class or a method to create the above structure:

public class DefineData {

// Assuming the below desired structure

// [
//    {
//        SL : 1
//        InNo: 19910
//        ...
//    },
//    {
//        SL : 2
//        InNo: 19911
//        ...
//    }
//
// ]

private ArrayList<HashMap<String, String>> dataArrayList;

// Helper method please use your own JsonObject instead of that method
public JSONObject getJsonObject() {
    String json = "{ \"MainData\":{ \"Headers\":\"SL.>´InNo. - Supp<´InvNo.<´Date^´Value>´Disc.>´Rate´Others>´Amount>\", \"FieldSeparator\":\"´\", \"DataList\": [ { \"Data\": \"1. ´19110 / Textiles´003220´01-sep-2019´70,605.00´0.00´530.25´982.75´118.00´\", \"DataInputType\":1 }, { \"Data\":\"2. ´19111 / Textiles´7041´01-sep-2019´8,895.00´0.00´444.75´173.25´513.00´\", \"DataInputType\":1 }] } }";
    try {
        JSONObject obj = new JSONObject(json);
        return obj;
    } catch (Throwable tx) {
        Log.e("TAG", "getJsonObject: ", tx.getCause());
        throw new RuntimeException("");
    }
}

public DefineData() throws JSONException {
    dataArrayList = new ArrayList<>();


    // Assuming everything is a String for now
    JSONObject obj = getJsonObject();
    JSONObject mainData = obj.getJSONObject("MainData");
    String headers = mainData.getString("Headers");
    // In your case "´" but it's a good practise to grab that from the JsonObject
    String fieldSeparator = mainData.getString("FieldSeparator");
    JSONArray dataList = mainData.getJSONArray("DataList");

    // Loop through dataList and populate the data map and split the data using the FieldSeparator
    String[] headersArray = headers.split(fieldSeparator);

    for (int i = 0; i < dataList.length(); i++) {
        JSONObject dataJsonObject = dataList.getJSONObject(i);
        String dataString = dataJsonObject.getString("Data");
        String[] dataArray = dataString.split(fieldSeparator);
        // Loop through the dataArray
        HashMap<String, String> dataMap = new HashMap<>();
        for (int j = 0; j < dataArray.length; j++) {
            String dataItem = dataArray[j];
            String header = headersArray[j];
            dataMap.put(dataItem, header);
        }
        dataArrayList.add(dataMap);
    }

}

public ArrayList<HashMap<String, String>> getDataArrayList() {
    return dataArrayList;
}
}

Your Adapter for the RecyclerView should look similar to that:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private ArrayList<HashMap<String, String>> dataArrayList;

public MyAdapter(ArrayList<HashMap<String, String>> dataArrayList) {
    this.dataArrayList = dataArrayList;
}

@Override
public int getItemCount() {
    return dataArrayList.size();
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    // Your root layout here instead of view..
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_item, parent, false);

    // TextView txtView = view.findViewById(R.id.textView);

    MyViewHolder vh = new MyViewHolder(view);
    // vh.textView = txtView;
    return vh;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    // The position will be similar to DataList position but this time we have the
    // information from the header
    HashMap<String, String> key = dataArrayList.get(position);

    //String slVal = key.get("SL");
    //String inNoVal = key.get("InNo");
    // Or simply iterate through them whatever works best

    holder.textView.setText("The desired value");
    // Do the same for the rest..
}


// VIEW HOLDER

public static class MyViewHolder extends RecyclerView.ViewHolder {
    public View view;
    public TextView textView;
    // Views....
    // Pass in your view or layout - RelativeLayout, ConstraintLayout
    public MyViewHolder(View view) {
        super(view);
        this.view = view;
    }
}


public ArrayList<HashMap<String, String>> getDataArrayList() {
    return dataArrayList;
}

public void setDataArrayList(ArrayList<HashMap<String, String>> dataArrayList) {
    this.dataArrayList = dataArrayList;
}

}

Then it should be as simple as:

    MyAdapter myAdapter;
    RecyclerView recyclerView;
    // ...
    // ...

    DefineData defineData = null;
    try {
        // Don't forget to pass in the jsonObject you want!!
        defineData = new DefineData();
    } catch (Exception e) {
        Log.e("TAG", "MyAdapter: ", e.getLocalizedMessage());
    }

    mAdapter = new MyAdapter(defineData.getDataArrayList());
    recyclerView.setAdapter(mAdapter);
like image 2
insomniusWalker Avatar answered Nov 09 '22 06:11

insomniusWalker