1

i have URL Link of json data & I displayed data like Color Name ,Color Code in RecyclerView in my app Except Shape data. how to display Shape Data also??? this is my Json Url Link : https://raw.githubusercontent.com/ianbar20/JSON-Volley-Tutorial/master/Example-JSON-Files/Example-Array.JSON

enter image description here

this is my AboutTask class

public class AboutTask extends AsyncTask<JSONObject, String, AboutResponse> {

OkHttpClient okHttpClient = new OkHttpClient();
Gson gson = new Gson();

@Override
protected AboutResponse doInBackground(JSONObject... params) {
    try {

        JSONObject jsonObject = params[0];

        //String url = "http://bitstobyte.in/api/about";
        String url = "https://raw.githubusercontent.com/ianbar20/JSON-Volley-Tutorial/master/Example-JSON-Files/Example-Array.JSON";

        Request request = new Request.Builder().url(url).build();

        Response response = okHttpClient.newCall(request).execute();

        if (!response.isSuccessful()) {
            Log.v("About_Answer", "About_Failed");
            throw new IOException("about_UnExpected Code" + response);
        }

        String strResponse = response.body().string();

        Log.e("about_strResponse", strResponse);

        AboutMainResponse[] aboutMainResponse = gson.fromJson(strResponse, AboutMainResponse[].class);

        //System.out.println(Arrays.toString(aboutMainResponse));

        System.out.println(aboutMainResponse);

        Log.e("aboutResp", "AboutResponse" + aboutMainResponse);
        return aboutMainResponse[0];

    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        Log.e("About_searchTask", e.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

this is About_Fragment

public class About_Fragment extends android.support.v4.app.Fragment {
JSONObject jsonObject;

private List<AboutData> aboutDataList = new ArrayList<>();
private RecyclerView recyclerView;
private AboutAdapter aboutAdapter;
public About_Fragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.fragment_about_,container,false);

    recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerView);

    //recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    recyclerView.setHasFixedSize(true);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    //recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    recyclerView.setAdapter(aboutAdapter);
    aboutAdapter = new AboutAdapter(aboutDataList,getContext());

    new AboutTask(){
        @Override
        protected void onPostExecute(AboutResponse aboutResponse) {
            if (aboutResponse != null && aboutResponse.colorArray != null && aboutResponse.colorArray.size() > 0) {
                //if (aboutResponse != null && aboutResponse.shapeArray != null & aboutResponse.shapeArray.size() > 0)

                    aboutDataList = aboutResponse.colorArray;

                    //aboutDataList = aboutResponse.shapeArray;

                    aboutAdapter = new AboutAdapter(aboutDataList, getContext());

                    recyclerView.setAdapter(aboutAdapter);
                }
                Log.i("resp", "onPostExecute");
            }
    }.execute(jsonObject);
    return rootView;
}
}

This is my AboutResponse code:

public class AboutResponse {

public List<AboutData> shapeArray;
public List<AboutData> colorArray;
}

This is AboutMainResponse:

public class AboutMainResponse extends AboutResponse {

// public AboutResponse aboutResponse;

}

This is My AboutAdapter code :

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

private List<AboutData> aboutDataList;
private Context context;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView colorName;
    public ImageView nextArrowimage;
    public TextView hexaValue,title_three;
    private AboutData aboutData=null;

    public MyViewHolder(View view) {
        super(view);
        colorName = (TextView) view.findViewById(R.id.title_one);
        nextArrowimage = (ImageView)view.findViewById(R.id.about_NextArrow);
        hexaValue = (TextView) view.findViewById(R.id.title_two);
        title_three = (TextView)view.findViewById(R.id.title_three);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }
}


public AboutAdapter(List<AboutData> aboutDataList,Context context) {
    this.aboutDataList = aboutDataList;
    this.context = context;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items, parent, false);
    MyViewHolder holder = new MyViewHolder(itemView);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    final AboutData aboutData = aboutDataList.get(position);
    holder.hexaValue.setText(aboutData.getHexValue());
    holder.colorName.setText(aboutData.getColorName());
    holder.title_three.setText(aboutData.getShapeArray());

    holder.nextArrowimage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String colorName = aboutData.getColorName();
            String hexaValue = aboutData.getHexValue();
            Intent intent = new Intent(context,Display_Activity.class);
            intent.putExtra("colorName",colorName);
            intent.putExtra("hexaValue",hexaValue);
            context.startActivity(intent);

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

}
Community
  • 1
  • 1

3 Answers3

1

Create Pojo for your json data to parse using gson like below :

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ColorArray {

@SerializedName("colorName")
@Expose
private String colorName;
@SerializedName("hexValue")
@Expose
private String hexValue;

public String getColorName() {
return colorName;
}

public void setColorName(String colorName) {
this.colorName = colorName;
}

public String getHexValue() {
return hexValue;
}

public void setHexValue(String hexValue) {
this.hexValue = hexValue;
}

}

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("colorArray")
@Expose
private List<ColorArray> colorArray = null;
@SerializedName("shapeArray")
@Expose
private List<ShapeArray> shapeArray = null;

public List<ColorArray> getColorArray() {
return colorArray;
}

public void setColorArray(List<ColorArray> colorArray) {
this.colorArray = colorArray;
}

public List<ShapeArray> getShapeArray() {
return shapeArray;
}

public void setShapeArray(List<ShapeArray> shapeArray) {
this.shapeArray = shapeArray;
}

}

-----------------------------------com.example.ShapeArray.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ShapeArray {

@SerializedName("shapeName")
@Expose
private String shapeName;

public String getShapeName() {
return shapeName;
}

public void setShapeName(String shapeName) {
this.shapeName = shapeName;
}

}

you can generate pojos for your json data online through below link:

http://www.jsonschema2pojo.org/

Like below code you can parse your data and get Example class Object having parse data in form of List of colors and Shapes.

And then you set this list into Listview with RecyclerView

Gson gson = new Gson();
Type listType = new TypeToken<List<Example>>(){}.getType();
List<Example> details = gson.fromJson(response, listType);
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • I created pojo , then how to bind the data?? – kranthi kumar Feb 17 '17 at 02:15
  • once look at my About_Fragment code,i bind the data like that,,, – kranthi kumar Feb 17 '17 at 02:16
  • You can set color into backgrounds of textview rather set color code as text into setText that is more efficient. – Chetan Joshi Feb 17 '17 at 05:12
  • Example example = gson.fromJson(strResponse, Example.class); //System.out.println(Arrays.toString(aboutMainResponse)); System.out.println(example); Log.e("aboutResp", "AboutResponse" + example); return example; – kranthi kumar Feb 17 '17 at 08:40
  • new AboutTask(){ @Override protected void onPostExecute(Example example) { if (example != null) { aboutDataList = example.getColorArray(); shapeList = example.getShapeArray(); aboutAdapter = new AboutAdapter(aboutDataList, shapeList, getContext()); recyclerView.setAdapter(aboutAdapter); } Log.i("resp", "onPostExecute"); } }.execute(jsonObject); return rootView; – kranthi kumar Feb 17 '17 at 08:42
  • this is my log cat : 02-17 14:06:00.030 17144-17160/com.example.ravali.displaydata W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ – kranthi kumar Feb 17 '17 at 08:44
  • new AboutTask(){ public void onPostExecute(Example example){ aboutListdata = example.getcolorArray(); shapeList = example.getShapeArray(); – kranthi kumar Feb 17 '17 at 14:08
  • If your shape and color array size remain same then. No problem in your code other wise it may cause for Array Index Out of Exception. – Chetan Joshi Feb 18 '17 at 00:36
  • Two model classes are there like List colorList & List shapeList ... then my question is : in OnPostExecute method how to bind the data and how to set the adapter >>? i set adapter like this : adapter = new Adapter(shapeList,colorList,getContext()); but didnot getting output – kranthi kumar Feb 18 '17 at 03:29
  • bro even i didnot get , i waste of my 5days time....plz send me full code of OnPostExecute method , AsyncTask Method... – kranthi kumar Feb 18 '17 at 03:57
0

go through this.Pass by a volley.it is for a single text view.But may be helpful for you

Community
  • 1
  • 1
Nazim ch
  • 834
  • 8
  • 20
0

Use the following snippet

Gson gson = new Gson(); 
String jsonOutput = "Your JSON String"; 
Type listType = new TypeToken<List<Post>>(){}.getType(); 
List<Post> posts = (List<Post>) gson.fromJson(jsonOutput, listType);