[英]Using a database example with Android studio
我正在尝试学习如何在Android中创建数据库,并且尝试遵循以下指南: http : //examples.javacodegeeks.com/android/core/database/android-database-example/
但是,我在AndroidDatabaseExample
活动中AndroidDatabaseExample
有关无法解析符号或方法的错误。
它无法解析的方法是getTitle()
和getId()
,它无法解析的符号是R.layout.row_layout
和R.id.list
。
AndroidDatabaseExample
的代码:
package com.javacodegeeks.androiddatabaseexample;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
public class AndroidDatabaseExample extends ListActivity implements OnItemClickListener {
JCGSQLiteHelper db = new JCGSQLiteHelper(this);
List list;
ArrayAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//drop database if it exists
db.onUpgrade(db.getWritableDatabase(), 1, 2);
db.createBook(new Book("The Great Gatsby", "F. Scott Fitzgerald"));
db.createBook(new Book("Anna Karenina", "Leo Tolstoy"));
//get all books
list = db.getAllBooks();
List listTitle = new ArrayList();
for (int i = 0; i < list.size(); i++) {
listTitle.add(i, list.get(i).getTitle());
}
myAdapter = new ArrayAdapter(this, R.layout.row_layout, R.id.list, listTitle);
getListView().setOnItemClickListener(this);
setListAdapter(myAdapter);
}
@Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
//start BookActivity wih extras the book id
Intent intent = new Intent(this, BookActivity.class);
intent.putExtra("book", list.get(arg2).getId());
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//get all books again
list = db.getAllBooks();
List listTitle = new ArrayList();
for (int i = 0; i < list.size(); i++){
listTitle.add(i, list.get(i).getTitle());
}
myAdapter = new ArrayAdapter(this, R.layout.row_layout, R.id.list, listTitle);
getListView().setOnItemClickListener(this);
setListAdapter(myAdapter);
}
}
getTitle
和getId
的方法在Book
类中定义,其代码为:
package com.javacodegeeks.androiddatabaseexample;
public class Book {
private int id;
private String title;
private String author;
public Book(){}
public Book(String title, String author) {
super();
this.title = title;
this.author = author;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [id=" + id + ", title=" + title + ", author=" + author + "]";
}
}
有谁知道如何解决这一问题? 我以前使用过该网站上的教程,但效果很好,所以我有点困惑为什么它不起作用。 你们提供的任何帮助我都将非常感谢。
是的,这对我有用
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent passIntent = new Intent(summary.this,Update.class);
passIntent.putExtra("book", item.get(position).getId_());
startActivity(passIntent);
}
});
在我的情况下,List item = db.getAllContacts();从数据库中获取所有项目,并在Update Activity中在oncreate()中添加以下代码
Intent intent = getIntent();
int id = intent.getIntExtra("book", 0);
// open the database of the application context
db = new DatabaseHandler(getApplicationContext());
// read the book with "id" from the database
eventitems = db.getContact(id);
eventname .setText(eventitems.getEventname());
firstname.setText(eventitems.getFirstName());
Update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
eventitems.setEventname(((EditText) findViewById(R.id.eventname)).getText().toString());
eventitems.setFirstName(((EditText) findViewById(R.id.firstname)).getText().toString());
// update book with changes
db.updateContact(eventitems);
finish();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.