0

当我显示显示垃圾数据的数据时,我使用列表视图来显示数据。

public class MainActivity extends ActionBarActivity {

    Button btnSave, btnDelete;
    EditText title, edition;
    ListView mListView;
    ArrayAdapter<Book> adapter;

    Book book = new Book();


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

        title = (EditText) findViewById(R.id.editTextTitle);
        edition = (EditText) findViewById(R.id.editTextEdition);

        btnSave = (Button) findViewById(R.id.buttonSave);
        btnDelete = (Button) findViewById(R.id.buttonDelete);

        mListView = (ListView) findViewById(R.id.listView);

        // List<Book> list= Select.from(Book.class).list();
        List<Book> list = Book.listAll(Book.class);

        adapter = new ArrayAdapter<Book>(MainActivity.this, android.R.layout.simple_list_item_1, list);
        mListView.setAdapter(adapter);

        Toast.makeText(getApplicationContext(), "" + list, Toast.LENGTH_LONG).show();


        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                book = new Book(title.getText().toString(), edition.getText().toString());
                book.save();

                Toast.makeText(getApplicationContext(), "" + book, Toast.LENGTH_LONG).show();
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Book book = Book.findById(Book.class, (long) 1);
                book.delete();
            }
        });


    }

}

图书.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;
    }
}

当我删除数据时,它也会删除,但是当显示它显示垃圾数据时,我怎样才能显示正确的数据?

4

2 回答 2

0

您需要创建一个自定义适配器来呈现书籍对象中的值。最简单的选择是在 Book 类中实现 toString() 方法。那应该解决这个问题。

于 2015-04-26T02:58:04.560 回答
-1

您必须使用 toString() 方法才能删除 juck 值,确保插入单个值,因为 toString() 只能返回单个值

import com.orm.SugarRecord;       // package


public class Book  extends SugarRecord{     /// inheritance

String title;                   //var

public  Book()                  //constructor
{
}

public  Book(String title)      //method
{
this.title=title;                 //  this keyword refer current object
}


 public String toString()            // method
 {
 return title;                    //string return value
 }

如果您有任何问题,可以观看此视频 https://youtu.be/lv4QanVZfs4

于 2016-11-22T17:38:38.053 回答