Android 列表中的Garabage值
我正在填充列表视图以查看我的记录,我有以下代码Android 列表中的Garabage值,android,sqlite,list,garbage,Android,Sqlite,List,Garbage,我正在填充列表视图以查看我的记录,我有以下代码 super.onCreate(savedInstanceState); setContentView(R.layout.total); ArrayList<Object> results = new ArrayList<Object>(); // -- SQLiteOpenHelper dbHelper = new Databas
super.onCreate(savedInstanceState);
setContentView(R.layout.total);
ArrayList<Object> results = new ArrayList<Object>();
// -- SQLiteOpenHelper dbHelper = new DatabaseHelper(this, SAMPLE_DB_NAME, null, 1);
SQLiteDatabase myDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, SQLiteDatabase.OPEN_READONLY, null);
try {
/* Create the Database (no Errors if it already exists) */
myDB.execSQL("PRAGMA foreign_keys = ON;");
// -- openOrCreateDatabase(name, mode, factory)
// myDB = dbHelper.getReadableDatabase();
Cursor c = myDB.query(DatabaseHelper.SAMPLE_TABLE_NAME, null, null, null, null, null, null);
Cursor d = myDB.query(DatabaseHelper.SAMPLE_TABLE_NAMES, null, null, null, null, null, null);
/* Check if our result was valid. */
if (c != null && d != null) {
c.moveToFirst(); // it's very important to do this action otherwise your Cursor object did not get work
d.moveToFirst();
char cust_name = (char) c.getColumnIndex("cust_name");
char pro_name = (char) d.getColumnIndex("pro_name");
int pro_price = (int) d.getColumnIndex("pro_price");
/* Check if at least one Result was returned. */
if (c.isFirst() && d.isFirst()) {
int i = 0;
/* Loop through all Results */
do {
i++;
String cust_nameColumnIndex = c.getString(cust_name);
String pro_nameColumnIndex = c.getString(pro_name);
int pro_priceColumnIndex = c.getInt(pro_price);
/* Add current Entry to results. */
results.add("" + i + ": " + cust_name + " (" + pro_name + ": " + pro_price + ")");
} while (c.moveToNext()&& d.moveToNext());
}
}
} catch (SQLiteException e) {
} finally {
if (myDB != null)
myDB.close();
}
// -- android.R.layout.simple_list_item_1 is object which belong to ListActivity itself
// -- you only need to add list object in your main layout file
this.setListAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, results));
}
super.onCreate(savedInstanceState);
setContentView(R.layout.total);
ArrayList结果=新建ArrayList();
//--SQLiteOpenHelper dbHelper=newdatabasehelper(这个,SAMPLE_DB_NAME,null,1);
SQLiteDatabase myDB=this.openOrCreateDatabase(SAMPLE\u DB\u NAME,SQLiteDatabase.OPEN\u READONLY,null);
试试{
/*创建数据库(如果数据库已存在,则无错误)*/
myDB.execSQL(“PRAGMA foreign_keys=ON;”;
//--openOrCreateDatabase(名称、模式、工厂)
//myDB=dbHelper.getReadableDatabase();
游标c=myDB.query(DatabaseHelper.SAMPLE_TABLE_NAME,null,null,null,null,null,null);
游标d=myDB.query(DatabaseHelper.SAMPLE_TABLE_name,null,null,null,null,null);
/*检查我们的结果是否有效。*/
如果(c!=null&&d!=null){
c、 moveToFirst();//执行此操作非常重要,否则游标对象无法工作
d、 moveToFirst();
char cust_name=(char)c.getColumnIndex(“cust_name”);
char pro_name=(char)d.getColumnIndex(“pro_name”);
int pro_价格=(int)d.getColumnIndex(“pro_价格”);
/*检查是否至少返回了一个结果。*/
如果(c.isFirst()&&d.isFirst()){
int i=0;
/*循环查看所有结果*/
做{
i++;
String cust_name columnindex=c.getString(cust_name);
String pro_name columnindex=c.getString(pro_name);
int pro_price columnindex=c.getInt(pro_price);
/*将当前条目添加到结果。*/
结果。添加(“+i+”:“+cust_名称+”(“+pro_名称+”:“+pro_价格+”);
}而(c.moveToNext()&d.moveToNext());
}
}
}catch(sqlitee){
}最后{
如果(myDB!=null)
myDB.close();
}
//--android.R.layout.simple_list_item_1是属于ListActivity本身的对象
//--只需在主布局文件中添加列表对象
this.setListAdapter(新的ArrayAdapter(this,android.R.layout.simple_list_item_1,results));
}
total.xml
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="380dp"
android:cacheColorHint="#00000000" >
</ListView>
数据已成功插入sqlite(由adb外壳确认)…它给了我垃圾值…任何人都能解决这个问题吗…提前谢谢
如果不是垃圾值引用(内存)地址,请使用下面的代码
do {
i++;
String cust_nameColumnIndex = c.getString(cust_name);
String pro_nameColumnIndex = c.getString(pro_name);
int pro_priceColumnIndex = c.getInt(pro_price);
/* Add current Entry to results. */
results.add("" + i + ": " + cust_nameColumnIndex + " (" + pro_nameColumnIndex + ": " + pro_priceColumnIndex + ")");
} while (c.moveToNext()&& d.moveToNext());
this.setListAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, (String[]) results.toArray(new String[0])));
do{
i++;
String cust_name columnindex=c.getString(cust_name);
String pro_name columnindex=c.getString(pro_name);
int pro_price columnindex=c.getInt(pro_price);
/*将当前条目添加到结果。*/
结果。添加(“+i+”:“+cust_nameColumnIndex+”(“+pro_nameColumnIndex+”:“+pro_priceColumnIndex+”));
}而(c.moveToNext()&d.moveToNext());
这个.setListAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,(字符串[])results.toArray(新字符串[0]));
如果不是垃圾值引用(内存)地址,请使用下面的代码
do {
i++;
String cust_nameColumnIndex = c.getString(cust_name);
String pro_nameColumnIndex = c.getString(pro_name);
int pro_priceColumnIndex = c.getInt(pro_price);
/* Add current Entry to results. */
results.add("" + i + ": " + cust_nameColumnIndex + " (" + pro_nameColumnIndex + ": " + pro_priceColumnIndex + ")");
} while (c.moveToNext()&& d.moveToNext());
this.setListAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, (String[]) results.toArray(new String[0])));
do{
i++;
String cust_name columnindex=c.getString(cust_name);
String pro_name columnindex=c.getString(pro_name);
int pro_price columnindex=c.getInt(pro_price);
/*将当前条目添加到结果。*/
结果。添加(“+i+”:“+cust_nameColumnIndex+”(“+pro_nameColumnIndex+”:“+pro_priceColumnIndex+”));
}而(c.moveToNext()&d.moveToNext());
这个.setListAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,(字符串[])results.toArray(新字符串[0]));
尝试更改读取光标的方式。这样可能更好:
//Get the indexes
int cust_name = cursor.getColumnIndex("cust_name");
int pro_name = cursor.getColumnIndex("pro_name");
int pro_price = cursor.getColumnIndex("pro_price");
try {
if(cursor.moveToFirst()){
while (!cursor.isAfterLast()) {
//Get the data
String cust_nameColumnIndex = cursor.getString(cust_name);
String pro_nameColumnIndex = cursor.getString(pro_name);
int pro_priceColumnIndex = cursor.getInt(pro_price);
//Move to next cursor item
cursor.moveToNext();
}
}
else {
Log.i(TAG, "Empty cursor");
//Do whatever
}
} catch (Exception e) {
Log.i(TAG, "Exception while reading cursor: " + e.getMessage());
//Do whatever
}
finally {
cursor.close();
}
尝试更改读取光标的方式。这样可能更好:
//Get the indexes
int cust_name = cursor.getColumnIndex("cust_name");
int pro_name = cursor.getColumnIndex("pro_name");
int pro_price = cursor.getColumnIndex("pro_price");
try {
if(cursor.moveToFirst()){
while (!cursor.isAfterLast()) {
//Get the data
String cust_nameColumnIndex = cursor.getString(cust_name);
String pro_nameColumnIndex = cursor.getString(pro_name);
int pro_priceColumnIndex = cursor.getInt(pro_price);
//Move to next cursor item
cursor.moveToNext();
}
}
else {
Log.i(TAG, "Empty cursor");
//Do whatever
}
} catch (Exception e) {
Log.i(TAG, "Exception while reading cursor: " + e.getMessage());
//Do whatever
}
finally {
cursor.close();
}
我想查看多个表中的数据我想查看多个表中的数据您能解释一下吗?我很想知道为什么代码可以观察结果send@BhaskarReddy谢谢你的回复。。实际上,数据来自两个表。。它现在向我显示了第一个(客户名称)而不是垃圾…所以这很好…但是现在在第三列中给出了0。。。价格应该是有价值的,而不是有价值的0@BhaskarReddy我根据我的情况更改ans,从。。。pro_nameColumnIndex=c.getString。。。。和pro_priceColumnIndex=c.getInt。。。到d.getString。。。由于这三个值都来自不同的游标…但将接受您的ans以帮助我:)你能解释一下吗?我很想知道为什么代码可以观察结果send@BhaskarReddy谢谢你的回复。。实际上,数据来自两个表。。它现在向我显示了第一个(客户名称)而不是垃圾…所以这很好…但是现在在第三列中给出了0。。。价格应该是有价值的,而不是有价值的0@BhaskarReddy我根据我的情况更改ans,从。。。pro_nameColumnIndex=c.getString。。。。和pro_priceColumnIndex=c.getInt。。。到d.getString。。。这三种价值观