1

我使用糖 orm 库添加数据和检索数据,但数据未正确检索以下是我的代码 main.xml

  <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edtTitle"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edtEdition"
        android:layout_below="@+id/edtTitle"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/buttonSave"
        android:layout_below="@+id/edtEdition"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="47dp" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/buttonSave" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display"
        android:id="@+id/buttonDisplay"
        android:layout_above="@+id/listView"
        android:layout_toRightOf="@+id/buttonSave"
        android:layout_toEndOf="@+id/buttonSave" />

图书.java

public class Book extends SugarRecord<Book> {

    String title;
    String edition;

    public Book()
    {

    }

    public Book(String title,String edition)
    {
        this.title=title;
        this.edition=edition;
    }
}

MainActivity.java

public class MainActivity extends ActionBarActivity {

    EditText title,edition;
    Button saveButton;
    ListView listView;
    List<Book> list;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        title=(EditText)findViewById(R.id.edtTitle);
        edition=(EditText)findViewById(R.id.edtEdition);
        listView= (ListView) findViewById(R.id.listView);

        saveButton= (Button) findViewById(R.id.buttonSave);

        list= Book.listAll(Book.class);
        ArrayAdapter<Book> adapter = new ArrayAdapter<Book>(MainActivity.this, android.R.layout.simple_list_item_1, Integer.parseInt(list.toString()));

        listView.setAdapter(adapter);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               Book book = new Book(title.getText().toString(),edition.getText().toString());
               // Book book = new Book("hello","World");

                book.save();

            }
        });
    }
}

我想检索数据并在列表视图中显示数据显示在列表视图中,但像这种类型的“@azx1lz”它不是当前数据,所以如何提前检索当前数据

4

1 回答 1

1

添加 toString 方法

公共类书扩展了 SugarRecord{

String title;
String Edition;

public Book()
{

}
public Book(String title,String Edition)
{
    this.title=title;
    this.Edition=Edition;
}

@Override
public String toString() {
    return   title + '\'' +
              Edition ;
}

}

于 2015-04-25T12:55:18.840 回答