我对搜索有疑问。搜索有效,但是如果单击列表项,则列表项将保持原样。示例:元素1 = 1,元素2 = 2,元素3 =3。当搜索元素3时,它变成位置1,但是同时,如果我单击它,结果是元素3 = 1,而不是3.这是我的代码。告诉我您需要修复什么,以便一切正常?
private DBHelper mDBHelper;
private SQLiteDatabase mDb;
EditText inputSearch;
private Button btnClear;
SimpleAdapter adapter;
ListView listView;
int textlength = 0;
GenericListFilter mFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adress_of_shops);
btnClear = (Button) findViewById(R.id.btn_clear);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputSearch.setText(null); //очищаем edittext
}
});
inputSearch = findViewById(R.id.inputSearch);
mDBHelper = new DBHelper(this);
try {
mDBHelper.updateDataBase();
} catch (
IOException mIOException) {
throw new Error("UnableToUpdateDatabase");
}
try {
mDb = mDBHelper.getWritableDatabase();
} catch (
SQLException mSQLException) {
throw mSQLException;
}
//Найдем компоненты в XML разметке
final Cursor cursor = mDb.rawQuery("SELECT * FROM shop", null);
cursor.moveToFirst();
final ArrayList<HashMap<String, Object>> name = new ArrayList<>();
//Список параметров конкретного клиента
HashMap<String, Object> info1;
//Пробегаем по всем клиентам
while (!cursor.isAfterLast()) {
info1 = new HashMap<>();
//Заполняем клиента
info1.put("_id", cursor.getString(0));
info1.put("name", cursor.getString(1));
info1.put("Lat", cursor.getString(2));
info1.put("Lng", cursor.getString(3));
info1.put("timework", cursor.getString(4));
info1.put("brand", cursor.getString(5));
info1.put("adress", cursor.getString(6));
info1.put("PC", cursor.getString(7));
info1.put("Image", cursor.getInt(8));
//Закидываем клиента в список клиентов
name.add(info1);
//Переходим к следующему клиенту
cursor.moveToNext();
}
cursor.close();
//Какие параметры клиента мы будем отображать в соответствующих
//элементах из разметки adapter_item.xml
String[] from = {"_id","name", "timework", "brand", "adress", "PC", "Image", "Lat", "Lng"};
//Создаем адаптер
adapter = new SimpleAdapter(this, name, R.layout.listview_adapter,
new String[]{"name".toLowerCase()},
new int[]{R.id.item_list});
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
AdressOfShops.this.adapter.getFilter().filter(s);
if (!inputSearch.getText().toString().equals("")) {
btnClear.setVisibility(View.VISIBLE);
} else {
btnClear.setVisibility(View.GONE);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String lng;
lng = (String) name.get(position).get("Lng");
String lat;
lat = (String) name.get(position).get("Lat");
Uri uri = Uri.parse("yandexnavi://build_route_on_map?lat_to=" + lat + "&lon_to=" + lng);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setPackage("ru.yandex.yandexnavi");
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(AdressOfShops.this, InfoAcivity.class);
String timeWork;
String txName;
String txBrand;
String txAdress;
String txPechka;
timeWork = (String) name.get(position).get("timework");
txBrand = (String) name.get(position).get("brand");
txAdress = (String) name.get(position).get("adress");
txPechka = (String) name.get(position).get("PC");
Object uri = name.get(position).get("Image");
txName = (String) name.get(position).get("name");
intent.putExtra("timework", timeWork);
intent.putExtra("brand", txBrand);
intent.putExtra("adress", txAdress);
intent.putExtra("PC", txPechka);
intent.putExtra("name", txName);
intent.putExtra("Image", uri.toString());
startActivity(intent);
return false;
}
});
}
@Override
public Filter getFilter() {
return AdressOfShops.this.mFilter;
}
}