0

我正在尝试 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
}

}

我是否需要在任何地方进行任何其他更改。

4

2 回答 2

0

然后你必须生成toString()概率,假设你有这样的模型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 + "]";
    }
}
于 2012-11-19T23:18:29.090 回答
0

看起来您只需要toString()在 Notifications 类中覆盖。

@Override
public String toString() {
    return "something useful";  // define this
}

现在我已经看到你的代码使用了:

@Override
public String toString() {
    return _message; //  + " (" + _id + ", " + type + ")";
}
于 2012-11-19T22:26:15.860 回答