Search code examples
javaandroidjsongsonjson-deserialization

Json text to RecyclerView in Android


I need help with my RecyclerView. I have a atring array from my SQL Database which looks like this:

{"success":true,"0":{"order_number":"078","typ_first":"E3rft","typ_last":"Split","order_date_time":"2016-10-11 19:20:03"},"1":{"order_number":"166","typ_first":"E483f","typ_last":"Split_test","order_date_time":"2016-10-12 18:39:30"}}

In my RecyclerView I have the following fields:

  • order_number
  • typ_all (type first and last)
  • date(only a date without time)

This is how I get my string array:

String plansData = plansPreferenceData.getString("plansPreferenceData", "");

This is how I set the data to my RecyclerView:

// Set plan data
Plans plan = new Plans("123", "E3rft Split", "11.10.2016");
// Add Object to list
planList.add(plan);

// Notify data changes
pAdapter.notifyDataSetChanged();

My Plans class:

public class Plans {
    private String planTitle, planType, planDate;

    public Plans(String planTitle, String planType, String planDate) {
        this.planTitle = planTitle;
        this.planType = planType;
        this.planDate = planDate;
    }

    public void setPlanTitle(String planTitle) {
        this.planTitle = planTitle;
    }

    public String getPlanTitle() {
        return planTitle;
    }

    public void setPlanType(String planType) {
        this.planType = planType;
    }

    public String getPlanType() {
        return planType;
    }

    public void setPlanDate(String planDate) {
        this.planDate = planDate;
    }

    public String getPlanDate() {
        return planDate;
    }
}

My onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_training, container, false);

    preparePlansData();

    return view;
}

My preparePlansData():

private void preparePlansData() {

    // Set plan data
    Plans plan = new Plans("123", "fkfjfjeje", "21.04.1977");
    // Add Object to list
    planList.add(plan);

    plan = new Plans("test", "tttt", "22.01.2017");
    planList.add(plan);

    // Notify data changes
    pAdapter.notifyDataSetChanged();

}

My question is how can I get the information out of the string array into my adapter? I also need to format the date before adding. Thanks for your help!


Solution

  • read about Gson here:

    http://guides.codepath.com/android/leveraging-the-gson-library

    After that you will be able to write code like that:

     Type type = new TypeToken<Map<Integer, Plans>>(){}.getType();
     Map<Integer, Plans> myMap = gson.fromJson("your json from db", type); 
    

    and use this map.values() in your adapter

    your Plans class should look like this:

    class Plans {
        String order_number;
        String typ_first;
        String typ_last;
        String order_date_time;
    }
    

    If you want another field names you have to use @SerializedName annotation

    Finally, you should write something like that, (I do not know if syntax is 100% do not have IDE open now) :

    private void preparePlansData() {
        String plansData = plansPreferenceData.getString("plansPreferenceData", "");
        Type type = new TypeToken<Map<Integer, Plans>>(){}.getType();
        Map<Integer, Plans> myMap = gson.fromJson(plansData, type); 
        planList.addAll(myMap.values());
    
        // Notify data changes
        pAdapter.notifyDataSetChanged();
    }
    

    and modify your model class:

    public class Plans {
        @SerializedName("order_number")
        String planTitle;
        @SerializedName("typ_last") 
        String planType; 
        @SerializedName("order_date_time")  
        String planDate;
        ....
    

    I hope it will help you.