当我使用编辑文本过滤项目时,列表位置变得混乱,项目不再调用正确的意图。任何帮助表示赞赏
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String mName = filteredValues.get(position).getName().toString();
String mWeb = filteredValues.get(position).getWebsite().toString();
Intent openDetails = new Intent(Test.this, ResourceDetails.class);
Bundle b = new Bundle();
b.putString("name", mName);
b.putString("web", mWeb);
openDetails.putExtras(b);
startActivity(openDetails);
}
});
private TextWatcher filterTextWatcher = new TextWatcher(){
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
}
public void afterTextChanged(Editable s) {
}
};
答案 0 :(得分:2)
flashsearchList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Integer temp=flashSearchNameMap.get(adapter.getItem(position));
navigateSearch(temp);
}
});
(adapter.getItem(position)将返回确切的列表名称,在flashSearchNameMap中,我在应用过滤之前从oncreate开始存储了名称和位置。所以你可以通过这个获得准确的位置
答案 1 :(得分:1)
使用列表时,项目位置不可靠。我建议您在附加内容时使用view.setTag(Object)
为每个项目分配一个标识符。这可以是数字,字符串或任何东西。然后你可以在点击监听器中使用view.getTag()
访问它。
答案 2 :(得分:1)
假设您正在使用自定义bean对象来存储您的姓名&amp;网站价值和ArrayAdapter
在ListView
中显示,就像这样
public class NamedLink {
final String mName;
final String mWebsite;
public NamedLink(String name, String website) {
mName = name;
mWebsite = website;
}
@Override
public String toString() {
return mName;
}
}
使用适配器,定义如下:
mAdapter = new ArrayAdapter<NamedLink>(this, android.R.layout.simple_list_item_2, mLinks) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(WhateverYourActivityIsNamed.this).inflate(android.R.layout.simple_list_item_2, null);
}
NamedLink link = getItem(position);
// This probably deserves a ViewHolder
((TextView) convertView.findViewById(android.R.id.text1)).setText(link.getName());
((TextView) convertView.findViewById(android.R.id.text2)).setText(link.getWebsite());
return convertView;
}
};
当您过滤数组适配器时,它将与bean #toString()
匹配,在这种情况下返回名称。过滤后,数组适配器在内部维护一个正确索引的bean列表副本 - 也就是说,你可以使用你在点击监听器中获得的位置,如下所示:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// getItemAtPosition() will return a NamedLink from the filtered
// list maintained inside the ArrayAdapter
NamedLink link = (NamedLink) parent.getItemAtPosition(position);
Intent openDetails = new Intent(Test.this, ResourceDetails.class);
Bundle b = new Bundle();
b.putString("name", link.getName());
b.putString("web", link.getWebsite());
openDetails.putExtras(b);
startActivity(openDetails);
}
});
答案 3 :(得分:1)
如果您在ViewHolder
中使用Adapter
,只需在holder类中定义一个realPosition变量并在YourAdapter.getView中设置它
并在listClick Listener
中 ContactAdapter.ViewHolder holder = (YourAdapter.ViewHolder) view.getTag();
holder.realPosition