2016-04-06 55 views
-2

我試圖按照本教程https://examples.javacodegeeks.com/android/core/database/android-database-example/來創建數據庫應用程序錯誤在我的Android Studio應用程序上來

在我AndroidDatabaseExample類,代碼如下:

package com.example.database; 


import java.util.ArrayList; 
import java.util.List; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
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 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    // drop this database if already 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")); 
    db.createBook(new Book("The Grapes of Wrath", "John Steinbeck")); 
    db.createBook(new Book("Invisible Man", "Ralph Ellison")); 
    db.createBook(new Book("Gone with the Wind", "Margaret Mitchell")); 
    db.createBook(new Book("Pride and Prejudice", "Jane Austen")); 
    db.createBook(new Book("Sense and Sensibility", "Jane Austen")); 
    db.createBook(new Book("Mansfield Park", "Jane Austen")); 
    db.createBook(new Book("The Color Purple", "Alice Walker")); 
    db.createBook(new Book("The Temple of My Familiar", "Alice Walker")); 
    db.createBook(new Book("The waves", "Virginia Woolf")); 
    db.createBook(new Book("Mrs Dalloway", "Virginia Woolf")); 
    db.createBook(new Book("War and Peace", "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.listText, listTitle); 
    getListView().setOnItemClickListener(this); 
    setListAdapter(myAdapter); 
} 

@Override 
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { 
    // start BookActivity with 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, because something changed 
    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.listText, listTitle); 
    getListView().setOnItemClickListener(this); 
    setListAdapter(myAdapter); 
} 
} 

它有出現getTitle,row_layout,listText和getId的'Can not resolve'錯誤。

這些東西都被定義並實施了被稱爲「Book.java」一個單獨的類,作爲教程指示:

package com.example.database; 

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 "com.example.database.Book [id=" + id + ", title=" + title + ", author=" + author 
      + "]"; 
} 
} 

所以應該認識到這些事情,但事實並非如此。我該如何解決?

+0

請寫整個堆棧跟蹤。它很清楚究竟發生了什麼。第一個「import com.example.database.R」爲 –

+0

。然後學習如何使用列表。將列表更改爲「列表」,如果您不定義類型,則無法訪問書籍中的方法。如果你不知道這些,我建議先學習基礎知識。 – uguboz

回答

0

您要定義的變量錯誤

List listTitle = new ArrayList(); 

請正確

List<String> listTitle = new Arraylist<>(); 

改變它所以,當您嘗試使用項目的一個在你的名單,這將是實例書POJO

然後你可以使用getIdgetTitle和你在y中定義的其他方法我們的模型

+0

好吧,我刪除了「List listTitle = new ArrayList();」並將其替換爲「List listTitle = new Arraylist <>();」。我仍然遇到同樣的錯誤。我需要編輯其他東西嗎? – DinnerPlate

+0

您需要將您的Book.class導入到您的活動中。如果你使用android studio,你可以使用組織進口' –

+0

好吧,我花了大約20分鐘試圖找出我應該輸入這個。我試着輸入「import com.example.database.Book;」但這表示這是一個未使用的導入聲明。 – DinnerPlate

相關問題