当我制作我的应用程序在线版本时,我从 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();