2013-11-03 52 views
0

時,即時通訊讓我的應用程序網絡版即時得到來自JSON數據這樣的代碼如下現在即時得到來自數據庫的數據看下面的代碼,我怎麼會使用相同的TextView和LinearLayout中和顯示數據相同這樣的我從json解析中顯示在線版應用程序? http://imgur.com/RVrKJ98如何顯示從離線數據,而不JSON

final LinearLayout table3 = (LinearLayout) findViewById(R.id.table3); 

JSONArray school5 = json2.getJSONArray("dish_ingredient"); 
String concatProduct = ""; 

for (int i = 0; i < school5.length(); i++) { 
String name = school5.getJSONObject(i).getString("name"); 
String subIngredient = school5.getJSONObject(i).getString("sub_ingredients"); 
if (i == 0) { 

if (subIngredient.equals("")) { 
concatProduct = " " + "<b>" + name + "</b>"+ ", " + subIngredient; 
} else { 
concatProduct = " " + "<b>" + name + "</b>"+ ": " + subIngredient; 
} 

} else { 
if (subIngredient.equals("")) { 
if (i == school5.length() - 1) { 

concatProduct += " " + "<b>" + name + "</b>" + ". " + subIngredient; 
} else { 

concatProduct += " " + "<b>" + name+ "</b>" + ", " + subIngredient; 
} 
} else { 

concatProduct += " " + "<b>" + name + "</b>"+ ": " + subIngredient; 
} 
} 

ingredient.setText(Html.fromHtml(concatProduct)); 





    now i m geting data from database with array how i will show on same 
    layout????check below code which used to get array from database 


    static ArrayList<String> Ingredient_name = new ArrayList<String>(); 
static ArrayList<String> Ingredient_sub = new ArrayList<String>(); 



     Cursor mCursor2 = db.selectQuery("SELECT i.name, i.sub_ingredients FROM 
    uss_dish_to_ingredient "); 


    if (mCursor2.moveToFirst()) { 
     do { 

    Ingredient_name.add(mCursor2.getString(mCursor2.getColumnIndex("name"))); 

Ingredient_sub.add(mCursor2.getString(mCursor2.getColumnIndex("sub_ingredients"))); 

} while (mCursor2.moveToNext()); 
} 

    mCursor2.close(); 














       FATAL EXCEPTION: main 
    java.lang.NullPointerException 
at com.schoollunchapp.fifthscreen.getFormattedIngredientList(fifthscreen.java:681) 
at com.schoollunchapp.fifthscreen.displayData(fifthscreen.java:561) 
at com.schoollunchapp.fifthscreen.access$0(fifthscreen.java:517) 
at com.schoollunchapp.fifthscreen$TheTask.onPostExecute(fifthscreen.java:274) 
at com.schoollunchapp.fifthscreen$TheTask.onPostExecute(fifthscreen.java:1) 
at android.os.AsyncTask.finish(AsyncTask.java:417) 
at android.os.AsyncTask.access$300(AsyncTask.java:127) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
at dalvik.system.NativeStart.main(Native Method) 








       Cursor mCursor2 = db.selectQuery("SELECT xyz"); 


    if (mCursor2.moveToFirst()) { 
     do { 
      String ingredientName =  
     mCursor2.getString(mCursor2.getColumnIndex("name")); 
      String ingredientSub = 
    mCursor2.getString(mCursor2.getColumnIndex("sub_ingredients")); 

      // create an Ingredient object in your loop and fill its 
      // values 
      Ingredient ingredientItem = new Ingredient(); 
      ingredientItem.setName(ingredientName); 
      ingredientItem.setSub(ingredientSub); 

      // Add your ingredient to your ingredientList 
      ingredientList.add(ingredientItem); 

     } while (mCursor2.moveToNext()); 
    } 

    // Now you got a list of ingredients. 
    String formattedAsString = getFormattedIngredientList(ingredientList); 
    ingredient.setText(Html.fromHtml(formattedAsString)); 

    mCursor2.close(); 

回答

1

你應該更好地定義一個類成分:

public class Ingredient { 
    private String name, sub; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getSub() { 
     return sub; 
    } 

    public void setSub(String sub) { 
     this.sub = sub; 
    } 
} 

而且你既JSON和db結果使用這個包裝類。在兩個JSON使用

 ArrayList<Ingredient> ingredientList = new ArrayList<Ingredient>(); 

     if (mCursor2.moveToFirst()) { 
      do { 
       String ingredientName = mCursor2.getString(mCursor2.getColumnIndex("name")); 
       String ingredientSub = mCursor2.getString(mCursor2.getColumnIndex("sub_ingredients")); 

       // create an Ingredient object in your loop and fill its 
       // values 
       Ingredient ingredientItem = new Ingredient(); 
       ingredientItem.setName(ingredientName); 
       ingredientItem.setSub(ingredientSub); 

       // Add your ingredient to your ingredientList 
       ingredientList.add(ingredientItem); 

      } while (mCursor2.moveToNext()); 
     } 

     // Now you got a list of ingredients. 
     String formattedAsString = getFormattedIngredientList(ingredientList); 
     ingredientTextView.setText(Html.fromHtml(formattedAsString)); 

getFormattedIngredientList方法和db結果

private String getFormattedIngredientList(ArrayList<Ingredient> ingredientList) { 

    String concatProduct = ""; 
    for (int i = 0; i < ingredientList.size(); i++) { 
     Ingredient currentIngredient = ingredientList.get(i); 
        if(currentIngredient.getSub() == null) { 
         currentIngredient.setSub(""); 
        } 
     if (i == 0) { 
      if ((currentIngredient.getSub() == null || currentIngredient.getSub().equals("")) { 
       concatProduct = " " + "<b>" + currentIngredient.getName() + "</b>" + ", " + currentIngredient.getSub(); 
      } else { 
       concatProduct = " " + "<b>" + currentIngredient.getName() + "</b>" + ": " + currentIngredient.getSub(); 
      } 

     } else { 
      if ((currentIngredient.getSub() == null || currentIngredient.getSub().equals("")) { 
       if (i == ingredientList.size() - 1) { 
        concatProduct += " " + "<b>" + currentIngredient.getName() + "</b>" + ". " + currentIngredient.getSub(); 
       } else { 
        concatProduct += " " + "<b>" + currentIngredient.getName() + "</b>" + ", " + currentIngredient.getSub(); 
       } 
      } else { 
       concatProduct += " " + "<b>" + currentIngredient.getName() + "</b>" + ": " + currentIngredient.getSub(); 
      } 
     } 
    } 

    return concatProduct; 
} 

而且改變你JSON的邏輯是:

 ArrayList<Ingredient> ingredientList = new ArrayList<Ingredient>(); 
     for (int i = 0; i < school5.length(); i++) { 
      String ingredientName = school5.getJSONObject(i).getString("name"); 
      String ingredientSub = school5.getJSONObject(i).getString("sub_ingredients"); 


      Ingredient ingredientItem = new Ingredient(); 
      ingredientItem.setName(ingredientName); 
      ingredientItem.setSub(ingredientSub); 

      // Add your ingredient to your ingredientList 
      ingredientList.add(ingredientItem); 
     } 

     // Now you got a list of ingredients. 
     String formattedAsString = getFormattedIngredientList(ingredientList); 
     ingredientTextView.setText(Html.fromHtml(formattedAsString)); 

現在你有一個單一的邏輯格式和一組json和db結果的結果。這很容易實施和維護。

+0

我做了,但應用程序崩潰 – user2844177

+1

您可以通過跟蹤日誌貓來解決崩潰。在我的回答中,我嘗試向您展示一個實現示例。主要想法是使用一個對象(Ingredient class)並填充它的json和db結果。之後,一個單一的方法來處理你的成分列表。如果你分享你的日誌貓輸出,我可以嘗試解決你目前的問題。 – Devrim

+0

玉任我粘貼上主要的logcat以上僅2分鐘 – user2844177

0

您可以使用databaseassets

+0

沒有我的意思是即時通訊這個數組Ingredient_name Ingredient_sub我將如何forlooop ahow中獲取數據? – user2844177