2016-06-21 6 views
1

私のアプリでは、私のJSONレスポンス内にブール変数があります。JSONのブール値を読み取る

[ 
{ 
    "id" : "001", 
    "firstName" : "Mark", 
    "lastName" : "Mason", 
    "role" : "CEO", 
    "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
}, 
{ 
    "teamName" : "iOS", 
    "members" : [ 
     { 
      "id" : "002", 
      "firstName" : "Olly", 
      "lastName" : "Berry", 
      "role" : "iOS Team Lead", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png", 
      "teamLead" : true 
     }, 
     { 
      "id" : "003", 
      "firstName" : "James", 
      "lastName" : "Frost", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "004", 
      "firstName" : "Liam", 
      "lastName" : "Nichols", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "005", 
      "firstName" : "Chris", 
      "lastName" : "Watson", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "006", 
      "firstName" : "Richard", 
      "lastName" : "Turton", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "007", 
      "firstName" : "Matt", 
      "lastName" : "Colliss", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "008", 
      "firstName" : "David", 
      "lastName" : "Gibson", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "009", 
      "firstName" : "Tom", 
      "lastName" : "Guy", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "010", 
      "firstName" : "Rich", 
      "lastName" : "Hodgkins", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     } 
    ] 
}, 
{ 
    "teamName" : "Android", 
    "members" : [{ 
      "id" : "011", 
      "firstName" : "David", 
      "lastName" : "Branton", 
      "role" : "Android Team Lead", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png", 
      "teamLead" : true 
     }, 
     { 
      "id" : "012", 
      "firstName" : "Dre", 
      "lastName" : "Pilipczuk", 
      "role" : "Android Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "013", 
      "firstName" : "Ray", 
      "lastName" : "Britton", 
      "role" : "Android Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "014", 
      "firstName" : "Charly", 
      "lastName" : "Murillo", 
      "role" : "Android Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     } 
    ] 
}, 
{ 
    "teamName" : "Web", 
    "members" : [{ 
      "id" : "015", 
      "firstName" : "Ryan", 
      "lastName" : "French", 
      "role" : "Web Team Lead", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png", 
      "teamLead" : true 
     }, 
     { 
      "id" : "016", 
      "firstName" : "James", 
      "lastName" : "Ward", 
      "role" : "Web Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "018", 
      "firstName" : "Adam", 
      "lastName" : "Smith", 
      "role" : "Web Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "019", 
      "firstName" : "Leonard", 
      "lastName" : "Da Costa", 
      "role" : "Web Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     } 
    ] 
}, 
{ 
    "teamName" : "Design", 
    "members" : [{ 
      "id" : "020", 
      "firstName" : "Hannah", 
      "lastName" : "Tempest", 
      "role" : "Design Team Lead", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png", 
      "teamLead" : true 
     }, 
     { 
      "id" : "021", 
      "firstName" : "Ellis", 
      "lastName" : "Reed", 
      "role" : "Designer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "022", 
      "firstName" : "Pete", 
      "lastName" : "Horsham", 
      "role" : "Designer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "023", 
      "firstName" : "Hemel", 
      "lastName" : "Dave", 
      "role" : "Designer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     }, 
     { 
      "id" : "024", 
      "firstName" : "Hannah", 
      "lastName" : "Corke", 
      "role" : "Designer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     } 
    ] 
} 
] 

     @Override 
     public void onResponse(JSONArray response) { 
      Log.d("Theo", response.toString()); 
      //I use i=1 to bypass the first JSON object which doesn't contain 
      the object member. 
      //If i set i=0,then I will get an exception. 
      for(int i = 1;i<response.length();i++){ 


       try { 

        //Here I read the 4 objects. 
        JSONObject jsonObject = response.getJSONObject(i); 

         //I get the members array for JSON object 
         JSONArray teamMembersArray = jsonObject.getJSONArray("members"); 

         for(int j=0;j<teamMembersArray.length();j++){ 

          //The model class which contains the setters/getters 
          //in order to "deserialize" the JSON objects into string objects. 
          Model m = new Model(); 


          JSONObject teamObject = teamMembersArray.getJSONObject(j); 
          //I am searching if the teamLead object exists.If yes 
          //then set it to true by reading it! If not then read 
          //the next members! 
          if (teamObject.has("teamLead") && teamObject.isNull("teamLead")) { 
           m.setTeamLead(teamObject.getBoolean("teamLead")); 
          }else { 

           m.setId(teamObject.getInt("id")); 
           m.setProfileImageURL(teamObject.getString("profileImageURL")); 
           m.setFirstName(teamObject.getString("firstName")); 
           m.setLastName(teamObject.getString("lastName")); 
           m.setRole(teamObject.getString("role")); 


           //Finally I am adding the string objects into an ArrayList. 
           modelArrayList.add(m); 

          } 

         } 



       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 


      //modelArrayList.add(m); 
      // Update list by notifying the adapter of changes 
      myAdapter.notifyDataSetChanged(); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

      //hidePD(); 
     } 
    }); 
    AppController.getInstance().addToRequestQueue(jsObjRequest); 

}

私は私のモデルクラス内に作成セッターメソッドを使用してそれを読みます。ここまでは順調ですね。 今私は自分のカスタムアダプタに行き、そのjson変数が真(teamLead)であれば、対応するすべてのチームリーダーのテキストの色を変更します。

これは私のアダプタコードです。

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

private List<Model> modelList; 
private Context mContext; 
private int focused; 
private ImageLoader mImageLoader; 

public MyAdapter(Activity activity,List<Model> modelList) { 
    this.modelList = modelList; 
    this.mContext = activity; 
} 

@Override 
public RowHolderClass onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_row,null); 

    final RowHolderClass holder = new RowHolderClass(v); 
    return holder; 
} 

@Override 
public void onBindViewHolder(RowHolderClass holder, int position) { 
    final Model listItems = modelList.get(position); 
    holder.itemView.setSelected(focused==position); 

    holder.getLayoutPosition(); 


    mImageLoader = AppController.getInstance().getImageLoader(); 

    holder.imageView.setImageUrl(listItems.getProfileImageURL(),mImageLoader); 

    holder.fName.setText(Html.fromHtml(listItems.getFirstName())); 
    holder.lName.setText(Html.fromHtml(listItems.getLastName())); 
    holder.jobRole.setText(Html.fromHtml(listItems.getRole())); 
    holder.teamLeader.booleanValue(); 
    //holder.fName.setTextColor(mContext.getResources().getColor(R.color.textColors)); 


    holder.relativeLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String first_name = listItems.getFirstName(); 
      String last_name = listItems.getLastName(); 
      String job_role = listItems.getRole(); 
      String image_url = listItems.getProfileImageURL(); 
      String id= String.valueOf(listItems.getId()); 


      Intent i = new Intent(mContext, NewActivity.class); 
      i.putExtra("firstName",first_name); 
      i.putExtra("lastName",last_name); 
      i.putExtra("jobRole",job_role); 
      i.putExtra("imageUrl",image_url); 
      i.putExtra("memberId",id); 
      mContext.startActivity(i); 
     } 
    }); 
} 

@Override 
public int getItemCount() { 
    return (null != modelList?modelList.size() :0); 
} 
//ViewHolder algorithm used to read the data of the row fast. 
class RowHolderClass extends RecyclerView.ViewHolder { 

    protected NetworkImageView imageView; 
    protected TextView fName, lName, jobRole; 
    protected RelativeLayout relativeLayout; 
    protected Boolean teamLeader; 

    public RowHolderClass(View itemView) { 
     super(itemView); 

     this.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.recLayout); 
     this.imageView = (NetworkImageView) itemView.findViewById(R.id.imageView); 
     this.fName = (TextView) itemView.findViewById(R.id.firstName); 
     this.lName = (TextView) itemView.findViewById(R.id.lastName); 
     this.jobRole = (TextView) itemView.findViewById(R.id.roleView); 

    } 
} 
} 

ご覧のとおり、私が望むすべての値が得られ、表示されます。しかし、私はこの

if(modelList.getTeamLead()) 
{ 
    // Change color 
} 
else 
{ 
    // Do not change 
} 

は、API呼び出しのあなたの他の部分でこれを追加してみ

if(teamLeader is true){ 
    //then change the text colours of the corresponding team leaders. 
} 

おかげで、

テオ

答えて

2

ためのブロック今では解決されなければならない

if (teamObject.has("teamLead") && (!teamObject.isNull("teamLead"))) { 
          m.setTeamLead(teamObject.getBoolean("teamLead")); 
         }else { 
         m.setTeamLead(false); 
        //DEFAULTVALUE 
         }  
          m.setId(teamObject.getInt("id")); 
          m.setProfileImageURL(teamObject.getString("profileImageURL")); 
          m.setFirstName(teamObject.getString("firstName")); 
          m.setLastName(teamObject.getString("lastName")); 
          m.setRole(teamObject.getString("role")); 

          //Finally I am adding the string objects into an ArrayList. 


     modelArrayList.add(m); 

のうち ...

それは非常に複雑にする必要はありません。 Jsonは、get(getString()、getBoolean()、getJsonArray())の代わりにopt(optString()、optBoolean()、optJsonArray())を簡単に実行するオプションも提供します。この選択肢は、指定されたキーがJSON文字列内に存在するかどうかをチェックします。 json文字列にキーがある場合は値を返し、そうでない場合はnullを返します。デフォルトではブール値はfalseです.Json例外は発生しません。これは、不必要なif条件を減らします。あなたのケースでは、 "teamLead"ブールオブジェクトが存在するかどうかをチェックしました。これは以下のように簡単に達成できます。

m.setTeamLead(teamObject.optBoolean("teamLead", false)); 

if条件を使用してブール値をチェックするには、次のようにします。

if (m.getTeamLead()) { 
//True 
} else { 
//False 
} 

これが役に立ちます。

+0

答えをありがとう。しかし、奇妙なことが起こります。私が最初にアプリを起動すると、チームのメンバーだけがハイライトされています。しかし、私がrecyclerviewをスクロールすると、一部の非鉛のメンバーも強調表示されます。これは私が色を変える方法です。 if(listItems.getTeamLead()){ holder.fName.setTextColor(mContext.getResources()。getColor(R.color.textColors)); holder.lName.setTextColor(mContext.getResources()。getColor(R.color.textColors)); holder.jobRole.setTextColor(mContext.getResources()。getColor(R.color.textColors)); } – Theo

+0

そこに何か問題がありますが、私はそれを見ません。 – Theo

+0

@テオ:テキストの色をデフォルトに変更するelse条件を記述する必要がありました。今あなたの問題は修正されると確信しています。 – Jeevanandhan

1

を言うのか分かりません。

m.setTeamLead(false); 
+0

返信いただきありがとうございます。私はjson例外が発生します:(java.lang.NullPointerException:NULLオブジェクト参照で仮想メソッド 'boolean java.lang.Boolean.booleanValue()'を呼び出そうとします。 – Theo

+0

これは、Boolean変数が初期化されていないことを意味します。 – Theo

+0

@Theoデバッグしようとしている可能性があります – Nisarg

1

使用..

if(listItems.getTeamLead()) 
    { 
     //then change the text colours of the corresponding team leaders. 
    } 

または

if(listItems.getTeamLead()==true) 
{ 
    //then change the text colours of the corresponding team leaders. 
} 

とデフォルト値を入れて、modelArrayList.add(m)に追加します。アダプタ

if(listItems.getTeamLead()){ 
//color 
}else{ 
//default setcolor 

}

+0

nope。 nullポインタ例外: – Theo

+0

modelListの代わりに "listItems"を使用 – Kush

+0

ここでは "if(teamObject.has(" teamLead ")&& teamObject.isNull(" teamLead ")){ m.setTeamLead(teamObject.getBoolean(" teamLead " );} "応答からヌル値を取得する必要がありますのでご確認ください – Kush

関連する問題