我正在尝试使用listView和listAdaptor来使用数据库值填充屏幕。我的列表未正确显示。 下面是我的DataBaseHandler类的代码
public List<Notifications> getNotificationList() {
List<Notifications> notificationList = new ArrayList<Notifications>();
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Notifications objN = cursorToNotifications(cursor);
notificationList.add(objN);
} while (cursor.moveToNext());
}
// return contact list
return notificationList;
}
以下是我如何打印调用函数
的值ArrayAdapter<Notifications> adapter = new ArrayAdapter<Notifications>(this,
android.R.layout.simple_list_item_1, objN);
setListAdapter(adapter);
当我运行应用程序时,它显示两个已被委托,这是正确的,但显示正确的数据。它显示类似
的内容com.example.appname@41448fa0
com.example.appname@41449278
有人能指出我哪里出错了。 谢谢
对不起Android世界的新手。你的意思是添加
@Override
public String toString() {
return "something useful"; // define this
}
在我的通知类中,类似于
public class Notifications {
//private variables
String _type;
String _message;
String _id;
// Empty constructor
public Notifications() {}
// constructor
public Notifications(String type, String message, String id){
this._type = type;
this._message = message;
this._id = id;
}
// getting ID
public String getId(){
return this._id;
}
public void setId(String id){
this._id = id;
}
public String getType(){
return this._type;
}
// setting id
public void setType(String type){
this._type = type;
}
// getting name
public String getMessage(){
return this._message;
}
// setting name
public void setMessage(String message){
this._message = message;
}
@Override
public String toString() {
return "something useful"; // define this
}
}
对不起Android世界的新手。你的意思是添加
@Override
public String toString() {
return "something useful"; // define this
}
在我的通知类中,类似于
public class Notifications {
//private variables
String _type;
String _message;
String _id;
// Empty constructor
public Notifications(){
}
// constructor
public Notifications(String type, String message, String id){
this._type = type;
this._message = message;
this._id = id;
}
// getting ID
public String getId(){
return this._id;
}
public void setId(String id){
this._id = id;
}
public String getType(){
return this._type;
}
// setting id
public void setType(String type){
this._type = type;
}
// getting name
public String getMessage(){
return this._message;
}
// setting name
public void setMessage(String message){
this._message = message;
}
@Override
public String toString() {
return "something useful"; // define this
}
}
我是否需要在任何地方进行任何其他更改。
答案 0 :(得分:0)
您似乎只需在通知类中覆盖toString()
。
@Override
public String toString() {
return "something useful"; // define this
}
现在我已经看到你的代码使用了:
@Override
public String toString() {
return _message; // + " (" + _id + ", " + type + ")";
}
答案 1 :(得分:0)
然后你必须生成toString()
个probs,假设你有一个名为City
的模型,为此生成toString()
,如下所示:
@Override
public String toString() {
return "City [name=" + name + ", latitude=" + latitude + ", longitude="
+ longitude + "]";
}
生成我的模型后如下所示:
public class City {
private String name;
private double latitude;
private double longitude;
public City(String name, double latitude, double longitude) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
@Override
public String toString() {
return "City [name=" + name + ", latitude=" + latitude + ", longitude="
+ longitude + "]";
}
}