0
數據庫:問題從數據庫中顯示的數據
IM試圖從一個數據庫中的數據在ListView顯示出來,但它不會顯示出來。它強制關閉,所以我看着Logcat,它說我的populateFrom()
和bindView()
是零點例外,但我不認爲這是問題。然後我嘗試調試它們,我注意到它來自我認爲的query()
。因爲它的鏈接與查詢多數民衆贊成爲什麼該方法不能工作,從而顯示錯誤?也許。
我嘗試搜索有用的資源,但他們的查詢與我的查詢幾乎相同。我現在升職了。誰能幫我。?這只是顯示數據庫中所有data(food)
的名稱。我檢查並且沒有拼寫錯誤。 (
TestDatabaseMain.java:
public class TestDatabaseMain extends Activity
{
private ListViewHelperTest dbListFoodHelper = null;
private Cursor OurCursor = null;
private ListFoodAdapter adapter=null;
public void onCreate(Bundle savedInstanceState)
{
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView)findViewById(R.id.listfood);
//create the database helper
dbListFoodHelper=new ListViewHelperTest(this);
//create the database if user runs the first time
dbListFoodHelper.createDataBase();
dbListFoodHelper.openDataBase();
OurCursor=dbListFoodHelper.getCursor();
startManagingCursor(OurCursor);
adapter = new ListFoodAdapter(OurCursor);
myListView.setAdapter(adapter);
}
catch(Exception e)
{
Log.e ("ERROR","ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
class ListFoodAdapter extends CursorAdapter
{
ListFoodAdapter(Cursor c)
{
super(TestDatabaseMain.this, c);
}
@Override
public void bindView(View row, Context ctxt, Cursor c)
{
ListFoodHolder holder = (ListFoodHolder)row.getTag();
holder.populateFrom(c, dbListFoodHelper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent)
{
LayoutInflater inflater =getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
ListFoodHolder holder = new ListFoodHolder(row);
row.setTag(holder);
return(row);
}
}
static class ListFoodHolder
{
private TextView name = null;
ListFoodHolder(View displayfood)
{
name = (TextView)displayfood.findViewById(R.id.listfood);
}
public void populateFrom(Cursor c, ListViewHelperTest r)
{
//to list out the data from the database
name.setText(r.getName(c));
}
}
ListViewHelperTest.java:
public class ListViewHelperTest extends SQLiteOpenHelper
{
//declare constants of the paths
private static String DB_PATH = "/data/data/sg.edu.tp.iit.mns/databases/";
private static String DB_NAME = "ListFoodItem";
private static int SCHEMA_VERSION=1;
private static final String TABLE_NAME = "listfooditem";
private static final String FOOD_TITLE = "FoodItem";
private static final String FOOD_ID = "_id";
private SQLiteDatabase myDataBase;
private final Context myContext;
public ListViewHelperTest(Context context)
{
super(context, DB_NAME, null, SCHEMA_VERSION);
this.myContext = context;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
}
public void createDataBase()
{
createDB();
}
public void createDB()
{
boolean dbExist = DBExists();
if (!dbExist)
{
//overwrite the database with the previous database
this.getReadableDatabase();
//copy the overwrite
copyDBFromRecource();
}
}
private boolean DBExists()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
checkDB.setLocale(Locale.getDefault());
checkDB.setLockingEnabled(true);
checkDB.setVersion(1);
}
catch(SQLiteException e)
{
Log.e("SqlHelper","database not found");
}
if(checkDB != null)
{
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDBFromRecource()
{
InputStream myInput=null;
// Path to the just created empty db
try
{
//Open your local db as the input stream
myInput = myContext.getAssets().open(DB_NAME);
OutputStream myOutput=null;
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
catch (Exception e)
{
throw new Error("Problem coping folders");
}
}
public void openDataBase() throws SQLException
{
String MyPath = DB_PATH + DB_NAME ;
myDataBase = SQLiteDatabase.openDatabase(MyPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close()
{
if (myDataBase != null)
{
myDataBase.close();
}
super.close();
}
//important!
public Cursor getCursor()
{
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
String[] ColumnReturn = new String[] {FOOD_ID, FOOD_TITLE};
//search by string pass(*important*)
Cursor MYCursor = queryBuilder.query(myDataBase, ColumnReturn, null,
null, null, null, "FoodItem ASC");
return MYCursor;
}
public String getName(Cursor c)
{
return(c.getString(1));
}
}
你的意思是我的getCursor();?對不起,但我有點迷惑 – NAJ
是的,只是在你返回MYCursor之前,做一個MYCursor.moveToFirst(); –
它仍然是一樣的。 – NAJ