海我正在學習接取從斷言文件夾 這裏的數據庫文件附上我的外部數據庫圖像如何避免重複ListView中的Android
我成功地在列表視圖中成功地添加標題字段,但標題中都分貝反覆和我的輸出,但我只需要一個早餐,晚餐,午餐 我們該怎麼做?
MainActivity
public class MainActivity extends ActionBarActivity {
ListView listView;
SqlLiteDbHelper dbHelper;
FoodSupply foodSupply;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
TitleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listView);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
//foodSupply= new FoodSupply();
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.gettitles(sqLiteDatabase);
adapter= new TitleAdapter(getApplicationContext(),R.layout.list_item);
listView.setAdapter(adapter);
if (cursor.moveToFirst())
{
do {
String title;
title=cursor.getString(0);
foodSupply= new FoodSupply(title);
adapter.add(foodSupply);
}while (cursor.moveToNext());
}
}
我的數據庫類
public Cursor gettitles(SQLiteDatabase db)
{
db = this.getReadableDatabase();
Cursor cursor;
cursor=db.rawQuery("SELECT Title FROM food_details",null);
return cursor;
}
謝謝:)但我們把真正的?? db.query(true)是什麼原因? – Karthick
我想你會發現它,但對於以後的用戶:.query的第一個參數是不同的。這告訴查詢從表中只獲取一個獨特的值。 – alltooconfusingthereforesleep