0

当我制作我的应用程序在线版本时,我从 json 中获取数据,如下面的代码现在我从数据库中获取数据参见下面的代码我将如何使用相同的 textview 和 linearlayout 并显示与我在 json 解析的应用程序在线版本中显示的数据相同的数据???http://imgur.com/RVrKJ98

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

2 回答 2

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 结果中使用这个包装类。

        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用于 json 和 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 结果的结果。它易于实施和维护。

于 2013-11-03T22:10:30.647 回答
0

您可以使用数据库资产

于 2013-11-03T20:46:32.567 回答