我有一个工作中的数据库,我放在我的资产文件夹中我使用listview显示一些元素,然后我需要点击一行,使其显示更多信息,它的工作原理可能我的代码很糟糕,但我做了什么我需要,但现在他们让我放一个搜索框来查找特定项目并且它不起作用,因为我使用列表视图上的位置知道点击了什么项目,现在当我使用搜索框时位置和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 insted的位置,但它给了我相同的结果,他们告诉我,这是因为我没有在我的数据库中包含_id,但我做了。
请帮助我,我正在实习,我需要让这个工作,这份工作将帮助我获得很多准备去大学的经验。
这是sqlite连接器
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;
}
}
答案 0 :(得分:0)
也许你可以使用setTag()并给你想要一个唯一标签的View / position / id。我用一些购物车的东西 - 用项目的目录号或sku标记一个视图,然后我不必记住用户点击的位置/ id - 我只是获取视图的标签()
编辑:
如果您想设置标签,那很简单:
someView.setTag(someValue);
在我的自定义列表视图中,我在getView()
中设置了每个项目的标记someView.setTag(value_dereived_from_position);
然后,在我的代码的其他部分,我可能会响应按下或按下按钮。我可以询问视图并检查标签:
Object index = some_other_view.getTag();
if (index == expected_value) {
// do something
}
答案 1 :(得分:0)
我在SQLiteOpenHelper
中更改了getFraccionInfo中的查询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 fraccion = '" + id + "'";
//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;
}
}
在我的主要活动中,我得到了带有
的视图中显示的文本String fraccionValues = ((TextView)view.findViewById(android.R.id.text1)).getText().toString();
现在查询使用视图文本“fraccion”从我的数据库中查找其他信息。
我有照片,但由于我的声誉,我无法上传它们。
我有一个博客我将上传图片