2011-10-18 11 views
0

HYを示しています!JSON解析は奇妙な行動

私は、JSON文字列を持っている:http://json.parser.online.fr/

にコード過去より良く理解するために

{"responseData":{"days":[{"date":1289430000,"lessons":[{"lesson":"3","classname":"XXXX","oldTeacher":"RUMET","newTeacher":"JAKOB","oldSubject":"0AM","newSubject":"0AM","oldRoom":"104","newRoom":"104 ","comment":""},{"lesson":"4","classname":"XXXX","oldTeacher":"RUMET","newTeacher":"JAKOB","oldSubject":"0AM","newSubject":"0APH","oldRoom":"104","newRoom":"107 ","comment":"Verlegtvon"},{"lesson":"8","classname":"XXXX","oldTeacher":"JAKOB","newTeacher":"","oldSubject":"0APH","newSubject":"","oldRoom":"107","newRoom":" ","comment":"Entfall"}]},{"date":1289516400,"lessons":[{"lesson":"1","classname":"XXXX","oldTeacher":"KAIS","newTeacher":"","oldSubject":"0RW1","newSubject":"","oldRoom":"107","newRoom":" ","comment":"Entfall"},{"lesson":"2","classname":"XXXX","oldTeacher":"KAIS","newTeacher":"TRAUN","oldSubject":"0RW1","newSubject":"0BO","oldRoom":"107","newRoom":"107 ","comment":""}]},{"date":1289948400,"lessons":[{"lesson":"5","classname":"XXXX","oldTeacher":"KIES","newTeacher":"","oldSubject":"0RK","newSubject":"","oldRoom":"107","newRoom":" ","comment":"Entfall"}]},{"date":1290121200,"lessons":[{"lesson":"6","classname":"XXXX","oldTeacher":"KIES","newTeacher":"","oldSubject":"0RK","newSubject":"","oldRoom":"107","newRoom":" ","comment":"Entfall"}]}]},"responseDetails":null,"responseStatus":200} 

と私はエントリオブジェクト(SPEntry)のリストにそれを解析:

public class EntryParse{ 

    ArrayList<SPEntry> list; 
    public EntryParse(Context ctx, String par_json) 
    { 
     try 
     { 
      list = new ArrayList<SPEntry>(); 
       JSONArray array = new JSONArray(par_json); 

       for (int i = 0; i < array.length(); i++) {    //Datum 
        JSONObject json = array.getJSONObject(i); 

        Date date = new Date(json.getLong("date")*1000); 
        SimpleDateFormat ft = new SimpleDateFormat ("dd.MM.yyyy"); 

        JSONArray lessons = json.getJSONArray("lessons"); 

        for (int j = 0; j < lessons.length(); j++) {  //Stunden 

         JSONObject obj = lessons.getJSONObject(j); 
         SPEntry entry = new SPEntry(); 
         entry.date = ft.format(date); 
         entry.lesson = obj.optString("lesson"); 
         entry.teacher = obj.optString("oldTeacher"); 
         entry.newTeacher = obj.optString("newTeacher"); 
         entry.lesson = obj.optString("oldSubject"); 
         entry.newlesson = obj.optString("newSubject"); 
         entry.oldRoom = obj.optString("oldRoom"); 
         entry.newRoom = obj.optString("newRoom"); 
         entry.comment = obj.optString("comment"); 
         if(entry.comment.equals("Entfall")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.entfall); 
         } 
         if(entry.comment.equals("Betreuung")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.betreung); 
         } 
         if(entry.comment.equals("Verlegtvon")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.verlegt); 
         } 
         else 
         { 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.empty); 
         } 


         list.add(entry); 

        } 

       } 

私の問題は、画像の設定が異常な動作を示していることです。 2番目のレッスンの要素では、私は絵を手に入れません。他のすべての場合には、私は "Entfall"絵しか得ません。 resoursesで画像が

は、リストの

スクリーンショット助けてください異なっている:あなたのJSON文字列に7つのレッスンエントリの

http://img6.imagebanana.com/img/fnmedlrr/device20111018181454.png

+2

プットこの場合(entry.comment.equals( "Verlegtvon")){entry.picture = BitmapFactory.decodeResource(ctx.getResources()、R.drawable.entfallを追加しました); }また、2番目の要素についても同じイメージを取得することを確認してください。 – user370305

+0

とYashwanth Kumarは、単純な場合の代わりにそれ以外の場合は適切です。 – user370305

+0

2番目の要素(リスト行)に同じイメージがある場合は、ドロブルを取得する際にプロブラムがあります。私は条件が – user370305

答えて

4
if(entry.comment.equals("Entfall")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.entfall); 
         } 
         if(entry.comment.equals("Betreuung")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.betreung); 
         } 
         if(entry.comment.equals("Verlegtvon")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.verlegt); 
         } 
         else 
         { 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.empty); 
         } 

あなたは慎重にコードを見ると、最終的にはあなたが問題を解決する場合は、他の使用、2枚の画像、emptyまたはverlegtvonのいずれかで終わるでしょう。以下のコードを試してみてください、私はちょうどelse if

if(entry.comment.equals("Entfall")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.entfall); 
         } 
         else if(entry.comment.equals("Betreuung")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.betreung); 
         } 
         else if(entry.comment.equals("Verlegtvon")){ 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.verlegt); 
         } 
         else 
         { 
          entry.picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.empty); 
         } 
+0

HY私はすでに前であれば、他の部分を持っていたと – test123123

+0

を示す第二の画像のみが存在する場合、それはリストビューを移入して行うには問題があり、いくつかの倍の画像が中に繰り返し変更したときに –

+0

私の解決策を掲載しました....あなたの助けにthx – test123123

1

は、それらの一方のみが一度使用されたが、それはVerlegtvonです(これも2番目のエントリです)。私はdrawableの何かが間違っていると思いますR.drawable.verlegt

+0

問題はoccureていませんリストビュー。このような問題を解決します。 – test123123