我有一个数据库从工作,我放在我的资产文件夹中我使用列表视图来显示一些元素,然后我需要点击一行以使它显示更多的信息,它的工作原理也许我的代码很烂,但我做了我所需要的,但现在他们要求我放置一个搜索框来查找特定项目,并且它不工作,因为我使用了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