问题
After filter a listview,how can I obtain the position of the first listview? I use simpleadapter to fill the listview. Each item in the datasource has its own id,and I use "case" to redirect them. After I filter the listview ,I don't know how to associate the latter items with the first listview.The postion and id have changed. Thank you.
I use the afterTextChanged of EditTextView to filter the listview and notify it. @Override
public void afterTextChanged(Editable paramEditable) {
listItemsCopy.clear();
int count=simpleAdapter.getCount();
if ((count>0 )&¶mEditable.length()>0) {
for (int i = 0; i < simpleAdapter.getCount(); i++) {
Map<String,Object> tempMap=(Map<String,Object>)simpleAdapter.getItem(i);
String itemName=tempMap.get("name").toString();
HashMap<String, Object> tempHashMap=new HashMap<String, Object>();
int copyCount=0;
if(itemName.toLowerCase().contains(paramEditable.toString().toLowerCase())){
tempHashMap=(HashMap<String, Object>)simpleAdapter.getItem(i);
listItemsCopy.add(tempHashMap);
copyCount++;
}
}
if(listItemsCopy!=null){
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
simpleAdaptercopy = new SimpleAdapter(getActivity(),
listItemsCopy, R.layout.right_menu_list_view, new String[] {
"name", "id", "image" }, new int[] {
R.id.rightMenuListViewTextView1,
R.id.rightMenuListViewTextView2,
R.id.rightMenuListViewImageView1 });
simpleAdapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if ((view instanceof ImageView && data instanceof Bitmap)) {
ImageView iv = (ImageView) view;
iv.setImageBitmap((Bitmap) data);
return true;
} else {
return false;
}
}
});
listView.setAdapter(simpleAdaptercopy);
simpleAdapter.notifyDataSetChanged();
}
}, 1000);
}
}
else {
listView.setAdapter(simpleAdapter);
simpleAdapter.notifyDataSetChanged();
}
}
Then I want to use the method onItemClick of listview to redirect different Intent.As the datasource has changed,both position and id are different from which I want
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = null;
switch (position) {}`
回答1:
I slove it with three arrays. Only test code:
String positionString[]=new String[22];
int latterPosition[]=new int[22];
for (int i = 0; i < latterPosition.length; i++) {
positionString[i]="";
latterPosition[i]=0;
}
int latterCount=0;
for (int i = 0; i < positions.length; i++) {
if (positions[i]==10) {
positionString[latterCount]=i+"";
latterPosition[latterCount]=10;
latterCount++;
}
}
回答2:
this happens becouse Android gives you only IDs for the itmes you currently see + maybe two hidden on button and two hidden on top.
In this tutorial you can see how to create a ListView with a model, this helps me, when i got this problem before. Add this to you code and change it (I think you don't need the checkboxes).
来源:https://stackoverflow.com/questions/16393538/after-filter-a-listview-how-can-i-obtain-the-position-of-the-first-listview