简体   繁体   English

Android SQLite游标返回垃圾

[英]Android SQLite cursor returning junk

My database is definitely being populated correctly (<3 SQLite Database Browser), but when I try to list all the records in a view, I get the following: 我的数据库肯定正确地被填充(<3 SQLite数据库浏览器),但是当我尝试列出视图中的所有记录时,得到以下信息:

com.example.AppName.ObjectName@4055e8c6
com.example.AppName.ObjectName@3456e789
com.example.AppName.ObjectName@4563e5b0

I am using simple_list_item_1 and an ArrayAdapter. 我正在使用simple_list_item_1和一个ArrayAdapter。

Create statement in my activity class: 在活动类中创建语句:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saved_objects);

    datasource = new ObjectsDatabase(this);
    datasource.open();

    List<Object> values = datasource.getSavedObjects();

    ArrayAdapter<ObjectName> adapter = new ArrayAdapter<ObjectName>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

XML layout for above activity class: 上述活动类的XML布局:

<LinearLayout
    android:id="@+id/group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onButton1Click"
        android:text="@string/menu_back" />

</LinearLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

Any thoughts? 有什么想法吗?

In my opinion you are trying to see the value of Object class , which returns you a correct value as 我认为您正在尝试查看Object类的值,该类将为您返回正确的值

com.example.AppName.ObjectName@4055e8c6
com.example.AppName.ObjectName@3456e789
com.example.AppName.ObjectName@4563e5b0

Try to return String not Object and create List<String> . 尝试返回String不是Object并创建List<String> You can always create your own type of element , get that custom class object and return the String value of it. 您始终可以创建自己的element类型,获取该自定义类对象并返回其String值。

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saved_objects);

    datasource = new ObjectsDatabase(this);
    datasource.open();

    List<String> values = datasource.getSavedObjects();

    ArrayAdapter<ObjectName> adapter = new ArrayAdapter<ObjectName>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

What you're seeing is the default implementation of toString() for your ObjectName objects. 您看到的是ObjectName对象的toString()的默认实现。

Override toString() in your ObjectName class to produce a string representation for display purposes. ObjectName类中重写toString()以产生用于显示目的的字符串表示形式。

It's also worth considering to use a cursor adapter instead of array adapter since the data apparently comes from a database query. 还值得考虑使用游标适配器而不是数组适配器,因为数据显然来自数据库查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM