2017-02-16 130 views
0

我有json數據的URL鏈接&我在RecyclerView中顯示顏色名稱,顏色代碼等數據,除了形狀數據。 如何顯示形狀數據也??? 這就是我的Json URL鏈接:https://raw.githubusercontent.com/ianbar20/JSON-Volley-Tutorial/master/Example-JSON-Files/Example-Array.JSON如何使用Gson顯示來自JSON的數據

enter image description here

這是我AboutTask類

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; 
} 

} 

這是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; 
} 
} 

這是我的AboutResponse代碼:

public class AboutResponse { 

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

這是AboutMainResponse:

public class AboutMainResponse extends AboutResponse { 

// public AboutResponse aboutResponse; 

} 

這是我的AboutAdapter代碼:

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(); 
} 

} 

回答

0

POJO的創建爲您的JSON數據使用GSON象下面這樣解析:

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; 
} 

} 

你可以爲你的json數據在線生成pojos鏈接如下:

http://www.jsonschema2pojo.org/

像下面的代碼,你可以分析你的數據和獲得的顏色和形狀列表形式具有解析數據實例類對象。

然後設置此列表爲ListviewRecyclerView

Gson gson = new Gson(); 
Type listType = new TypeToken<List<Example>>(){}.getType(); 
List<Example> details = gson.fromJson(response, listType); 
+0

我創建了pojo,那麼如何綁定數據? –

+0

一旦看看我的關於片段代碼,我綁定這樣的數據,,, –

+0

您可以將顏色設置爲textview的背景,而不是將顏色代碼設置爲文本到更有效的setText。 –

0

經過this .Pass由volley.it是一個單一的文本view.But可以爲你

0

使用是有幫助以下代碼片段

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); 
相關問題