我有一個數據庫從工作,我放在我的資產文件夾中我使用列表視圖來顯示一些元素,然後我需要點擊一行以使它顯示更多的信息,它的工作原理也許我的代碼很爛,但我做了我所需要的,但現在他們要求我放置一個搜索框來查找特定項目,並且它不工作,因爲我使用了listview上的位置來知道哪些項目被點擊了,現在當我使用搜索框的位置和id改變。Android sqlite數據庫從列表視圖中獲取ID
public class MainActivity extends Activity {
ListView datos;
ListAdapter adapter;
SQLiteConnector sqlConnect;
EditText search;
TextView _id;
ListAdapter intento;
SQLiteHelper dbTools = new SQLiteHelper (this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datos = (ListView) findViewById(R.id.listView1);
sqlConnect = new SQLiteConnector(this);
search = (EditText) findViewById(R.id.editTextbuscar);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sqlConnect.getAllRecord());
datos.setAdapter(adapter);
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
((Filterable) MainActivity.this.adapter).getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
datos.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long posicion = (id + 1);
String fraccionValues = String.valueOf(posicion);
Intent detailsint = new Intent(getApplication(), Details.class);
detailsint.putExtra("_id", fraccionValues);
Toast.makeText(getApplicationContext(), fraccionValues + " selected", Toast.LENGTH_LONG).show();
startActivity(detailsint);
}
});
}
}
我需要用不同的方式來識別列表上的項目,這是該活動的代碼被點擊
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
Fraccion = (TextView) findViewById(R.id.viewfraccion);
Descripcion = (TextView) findViewById(R.id.viewdescripcion);
ADV = (TextView) findViewById(R.id.viewadv);
Intent theIntent = getIntent();
String _id = theIntent.getStringExtra("_id");
HashMap<String, String> fraccionMap = dbtools.getFraccionInfo(_id);
if(fraccionMap.size()!=0) {
Fraccion.setText(fraccionMap.get("fraccion"));
Descripcion.setText(fraccionMap.get("descripcion"));
ADV.setText(fraccionMap.get("adv"));
}
}
}
之後調用有人告訴我使用ID的位置,但它給了我相同的結果,他們說,這是因爲我沒有在我的數據庫中包括_id,但我做到了。
請幫助我,我正在實習,我需要把這個工作,這項工作將幫助我獲得大量的準備上大學的經驗。
這是源碼連接器
public SQLiteConnector (Context context) {
sqlHelper = new SQLiteHelper (context) ;
}
public List<String> getAllRecord() {
List<String> fraccionesList = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_RECORD + " ORDER BY _id"; //+ " WHERE COLUMN = Fraccion";
database = sqlHelper.getReadableDatabase();
cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
fraccionesList.add(cursor.getString(1));
}
while (cursor.moveToNext());
}
database.close();
return fraccionesList;
}
}
和sqliteopenhelper
公共類SQLiteHelper延伸SQLiteOpenHelper {
private static String DB_PATH="/data/data/com.as.sqliteviewer/databases/";
private static String DB_NAME="Tarifa.s3db";
private static int VERSION = 1;
private SQLiteDatabase myDataBase;
private final Context myContext;
public SQLiteHelper (Context context) {
super(context, DB_NAME, null, VERSION);
myContext = context;
try {
createDatabase();
}
catch (IOException ioe) {
throw new Error ("Unable to create database");
}
}
public void createDatabase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
System.out.println ("DB EXIST");
}
else {
this.getReadableDatabase();
this.close();
copyDataBase();
}
}
public void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte [1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e) {
System.out.println("Database doesn't exist yet.");
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public HashMap<String, String> getFraccionInfo(String id) {
HashMap<String, String> fraccionMap = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM fracciones WHERE _id ='" + id + "'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
fraccionMap.put("_id", cursor.getString(0));
fraccionMap.put("fraccion", cursor.getString(1));
fraccionMap.put("descripcion", cursor.getString(2));
fraccionMap.put("adv", cursor.getString(3));
} while (cursor.moveToNext());
}
return fraccionMap;
}
}
你可以給使用settag一個例子嗎? – ASCA120
我需要自動爲數據庫中的所有項目分配一個標記(數千個) – ASCA120