Reputation: 11
I want get the original position when I select item in filtered ListView
.
This is my simple code.
My problem is to set the old position not the new one in filtered list view:
public class MainActivity extends AppCompatActivity {
EditText sr;
ListView lv_sr;
String[] titre;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sr = (EditText) findViewById(R.id.sr);
lv_sr = (ListView) findViewById(R.id.lv_sr);
titre = getResources().getStringArray(R.array.titre);
adapter = new ArrayAdapter<String>(this, R.layout.row_itme, R.id.row_text, titre);
lv_sr.setAdapter(adapter);
initEvent();
}
private void initEvent() {
sr.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
MainActivity.this.adapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
lv_sr.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent km = new Intent(MainActivity.this, Webhtml.class);
km.putExtra("page",position); -------- this is my problem
startActivity(km);
}
});
}
}
Upvotes: 0
Views: 911
Reputation:
When you filter your ListView then every filtered View position has been also change according to the amount of filtered views. So if you want to get the original position (Previous position or position before filtering) form OnItemClickListener on adapter. You can't get that any more.
You can create a hidden field for holing the original position.
String itemName = (String) parent.getItemAtPosition(position);
int position_fo_item = Arrays.asList(titre).indexOf(itemName);
Intent km = new Intent(MainActivity.this, Webhtml.class);
km.putExtra("page",position_fo_item);
startActivity(km);
If it not work then create another array as 'titre_original' and assign it like 'titre' as well. then use it to get position like
String[] titre_original;
titre_original = getResources().getStringArray(R.array.titre);
int position_fo_item = Arrays.asList(titre_original).indexOf(itemName);
It will definitely work ...
Upvotes: 2