SQLite データベースからデータを取得し、それを arrayList に格納して、listView を介して連絡先に関する情報を表示しています。次に、ユーザーが EditText を入力すると、TextWatcher が listView をフィルター処理します。ただし、これが発生すると、リストビュー項目の位置が onClickListener で更新されません。
CRUD アクティビティに送信する ID は常に、最初にリストされたときの位置ではなく、フィルター処理された後の位置です。ListView アイテムがフィルター処理された後、元の位置を保持するにはどうすればよいですか? または、位置を介さずに CRUD アクティビティに正しい ID を与えるより良い方法です。
public class Search extends AppCompatActivity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<String> arrayList = new ArrayList<>();
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
db = new DBHelper(this);
arrayList = db.getAllContacts();
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
lv.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Search.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
adapter.notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
int id_To_Search = pos + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt(EXTRA_FLAG, id_To_Search);
// The flag value > 0 is used to signal that the upcoming operation
// is not adding, but updating or deleting
Intent intent = new Intent(getApplicationContext(), CRUDActivity.class);
intent.putExtras(dataBundle); //Intent.putExtras(Bundle extras
startActivity(intent);
}
});
}