アプリケーションのオンライン バージョンを作成するとき、以下のこのコードのように 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();