当我正在使我的应用程序在线版本即时获取来自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();
答案 0 :(得分: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));
在json和db结果中使用的 getFormattedIngredientList
方法
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结果的结果。它易于实施和维护。
答案 1 :(得分:0)