我的Android应用程序遇到了问题。问题是当我尝试从我的数据库中检索数据时,我得到的代码是我无法读取的。我是一名业余程序员,对Android编程知之甚少。这是我的输出:
[com.example.foodsaver2.Food@42ae3420,
com.example.foodsaver2.Food@42ae3720,
com.example.foodsaver2.Food@42ae3968,
com.example.foodsaver2.Food@42ae3bd0,
com.example.foodsaver2.Food@42ae3e18,
com.example.foodsaver2.Food@42ae4060,
com.example.foodsaver2.Food@42ae42a8, com.example.foodsaver2.Food@42ae4510, com.example.foodsaver2.Food@42ae4758, com.example.foodsaver2.Food@42ae49a0, com.example.foodsaver2.Food@42ae4c08, com.example.foodsaver2.Food@42ae4e50, com.example.foodsaver2.Food@42ae50b8, com.example.foodsaver2.Food@42ae5360, com.example.foodsaver2.Food@42ae55a8, com.example.foodsaver2.Food@42ae5810, com.example.foodsaver2.Food@42ae5a58, com.example.foodsaver2.Food@42ae5ca0, com.example.foodsaver2.Food@42ae5ee8, com.example.foodsaver2.Food@42ae61d0, com.example.foodsaver2.Food@42ae6418, com.example.foodsaver2.Food@42ae6660, com.example.foodsaver2.Food@42ae68c8, com.example.foodsaver2.Food@42ae6b10, com.example.foodsaver2.Food@42ae6d58, com.example.foodsaver2.Food@42ae6fa0, com.example.foodsaver2.Food@42ae7208, com.example.foodsaver2.Food@42ae7450, com.example.foodsaver2.Food@42ae7790, com.example.foodsaver2.Food@42ae79d8, com.example.foodsaver2.Food@42ae7c60, com.example.foodsaver2.Food@42ae7ea8, com.example.foodsaver2.Food@42ae8130, com.example.foodsaver2.Food@42ae8378, com.example.foodsaver2.Food@42ae85e0, com.example.foodsaver2.Food@42ae8848, com.example.foodsaver2.Food@42ae8ab0, com.example.foodsaver2.Food@42ae8d18, com.example.foodsaver2.Food@42ae8f80, com.example.foodsaver2.Food@42ae91e8, com.example.foodsaver2.Food@42ae9450, com.example.foodsaver2.Food@42ae97c0, com.example.foodsaver2.Food@42ae9a28, com.example.foodsaver2.Food@42ae9c70, com.example.foodsaver2.Food@42ae9ee0, com.example.foodsaver2.Food@42aea128, com.example.foodsaver2.Food@42aea370, com.example.foodsaver2.Food@42aea5d8, com.example.foodsaver2.Food@42aea820, com.example.foodsaver2.Food@42aeaa88, com.example.foodsaver2.Food@42aeacd0, com.example.foodsaver2.Food@42aeaf38, com.example.foodsaver2.Food@42aeb180, com.example.foodsaver2.Food@42aeb3e8, com.example.foodsaver2.Food@42aeb630, com.example.foodsaver2.Food@42aeb878, com.example.foodsaver2.Food@42aebae0, com.example.foodsaver2.Food@42aebd28, com.example.foodsaver2.Food@42aebf90, com.example.foodsaver2.Food@42aec1d8, com.example.foodsaver2.Food@42aec440, com.example.foodsaver2.Food@42aec808, com.example.foodsaver2.Food@42aeca70, com.example.foodsaver2.Food@42aeccb8, com.example.foodsaver2.Food@42aecf00, com.example.foodsaver2.Food@42aed168, com.example.foodsaver2.Food@42aed3b0, com.example.foodsaver2.Food@42aed618, com.example.foodsaver2.Food@42aed860, com.example.foodsaver2.Food@42aedaa8, com.example.foodsaver2.Food@42aedd10, com.example.foodsaver2.Food@42aedf58, com.example.foodsaver2.Food@42aee1a0, com.example.foodsaver2.Food@42aee408, com.example.foodsaver2.Food@42aee650, com.example.foodsaver2.Food@42aee8b8, com.example.foodsaver2.Food@42aeeb00, com.example.foodsaver2.Food@42aeed68, com.example.foodsaver2.Food@42aeefb0, com.example.foodsaver2.Food@42aef218, com.example.foodsaver2.Food@42aef460, com.example.foodsaver2.Food@42aef6c8, com.example.foodsaver2.Food@42aef910, com.example.foodsaver2.Food@42aefb58, com.example.foodsaver2.Food@42aefdc0, com.example.foodsaver2.Food@42af0008, com.example.foodsaver2.Food@42af0250, com.example.foodsaver2.Food@42af04b8, com.example.foodsaver2.Food@42af0700, com.example.foodsaver2.Food@42af0948, com.example.foodsaver2.Food@42af0bb0, com.example.foodsaver2.Food@42af1028, com.example.foodsaver2.Food@42af1270, com.example.foodsaver2.Food@42af14d8, com.example.foodsaver2.Food@42af1720, com.example.foodsaver2.Food@42af1968, com.example.foodsaver2.Food@42af1bd0, com.example.foodsaver2.Food@42af1e18, com.example.foodsaver2.Food@42af2060, com.example.foodsaver2.Food@42af22c8, com.example.foodsaver2.Food@42af2510, com.example.foodsaver2.Food@42af2778, com.example.foodsaver2.Food@42af29c0, com.example.foodsaver2.Food@42af2c08, com.example.foodsaver2.Food@42af2e70, com.example.foodsaver2.Food@42af30b8, com.example.foodsaver2.Food@42af3300, com.example.foodsaver2.Food@42af3568]
这是我做的:
public void send (View v) {
List<String> strlist = new ArrayList<String>();
strlist = mDbHelper.getAllComments();
GmailSender sender = new GmailSender("omitted", "omitted");
try {
sender.sendMail("DietWatcher Feedback",
strlist + "",
"ommited",
"omitted");
Toast.makeText(getApplicationContext(), "Sent!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Cannot send message!", Toast.LENGTH_SHORT).show();
}
}
以下是我的一些数据库代码:
public List getAllComments() {
List customers = new ArrayList();
String inputText = "";
String query =
KEY_CUSTOMER + /*"INTEGER," + */ "," +
KEY_NAME + "," +
KEY_ADDRESS1 + "," +
KEY_ADDRESS2 + "," +
KEY_CITY + "," +
KEY_STATE + "," +
KEY_ZIP + "," +
KEY_SEARCH + "," +
TOTAL_CARB + "," +
FIBER +
SUGAR +
PROTEIN+
SODIUM +
TOTALCALORIES +
FTS_VIRTUAL_TABLE +
KEY_SEARCH;
Log.w(TAG, query);
//Cursor mCursor = mDb.rawQuery(query,null);
Cursor cursor = mDb.query(FTS_VIRTUAL_TABLE, new String[]{KEY_CUSTOMER + "," +
KEY_NAME + "," +
KEY_ADDRESS1 + "," +
KEY_ADDRESS2 + "," +
KEY_CITY + "," +
KEY_STATE + "," +
KEY_ZIP + "," +
TOTAL_CARB /*+
FIBER +
SUGAR +
PROTEIN+
SODIUM +
TOTALCALORIES +
FTS_VIRTUAL_TABLE +
KEY_SEARCH */}, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Food customer = cursorToCustomer(cursor);
customers.add(customer);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return customers;
}
private Food cursorToCustomer(Cursor cursor) {
Food customer = new Food();
customer.setCustomer(cursor.getString(0));
customer.setName(cursor.getString(1));
customer.setAddress1(cursor.getString(2));
customer.setAddress2(cursor.getString(3));
customer.setCity(cursor.getString(4));
customer.setState(cursor.getString(5));
customer.setZipCode(cursor.getString(6));
return customer;
}
这是Food.java: package com.example.foodsaver2;
public class Food {
String customer = null;
String name = null;
String address1 = null;
String address2 = null;
String city = null;
String state = null;
String zipCode = null;
String carb = null;
String fiber = null;
String sugar = null;
String protein = null;
String sodium = null;
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getCarb() {return carb;}
public void setCarb(String carb) {this.carb = carb;}
public String getFiber() {return fiber;}
public void setFiber() {this.fiber = fiber;}
public String getSugar() {return sugar;}
public void setSugar() {this.sugar = sugar;}
public String getProtein() {return protein;}
public void setProtein() {this.protein = protein;}
public String getSodium() {return sodium;}
public void setSodium() {this.sodium = sodium;}
}
那我在这里做错了什么?关于这个问题的任何帮助将不胜感激。
答案 0 :(得分:1)
mDbHelper.getAllComments()
返回Food
类对象的列表而不是String,因此响应不是可读格式。如果您想将详细信息从strlist
传递到sender.sendMail
,那么您需要准备另一个List,其中将strlist
的值存储为字符串。尝试是:
ArrayList<Food> arrallcomments = mDbHelper.getAllComments();
List<String> strlist = new ArrayList<String>();
for(Food object: list){
strlist.add("Name : "+object.getName() +
" Customer :"+object.getCustomer()+...);
}
现在使用strlist
发送带有Mail的ArrayList。