在Android中搜索ListView项后,总是打开ListView的第一项(问题) [英] After searching for ListView items in Android, always open first item of ListView (Issue)
问题描述
当我打开搜索到的项目时,它打开的是 ListView 的第一个项目,而不是搜索到的项目.即使搜索和找到项目成功.
When I opened the searched item, it is opening the first item of ListView not the searched one. Even if search and find the items successfuly.
让我举个例子:
- 如果我搜索
Arrow
,我可以获得Arrow
,但是当点击Arrow
时,它会Almost Human
code>,Almost Human
是我 ListView 的第一项 - 如果我搜索
American
,我会得到The American (first)
和American Horror Story (second)
(两个结果)搜索过程成功,但如果点击The美国人(第二个)
,它会转到ListView的第二个项目.
- If I search for
Arrow
, I can get theArrow
but when clickedArrow
it is going toAlmost Human
,Almost Human
is my first item of ListView - If I search for
American
I'm gettingThe Americans (first)
andAmerican Horror Story (second)
( two results ) search proccess is successful but if clickThe Americans (second one)
it goes to second item of ListView.
我错过了什么?
我的搜索代码:
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.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
}
});
}
这里是 CustomListViewAdapter.java
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
public CustomListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
// holder.imageView.setImageBitmap(rowItem.getImageId());
ImageView img = (ImageView) convertView.findViewById(R.id.icon);
ImageView gif = (ImageView) convertView.findViewById(R.id.icon);
// UrlImageViewHelper.loadUrlDrawable(context, rowItem.getImageId());
Ion.with(gif).load("http://www.example.com/wp-content/uploads/2014/03/android-icon/loading.gif");
Drawable drawable=gif.getDrawable();
UrlImageViewHelper.setUrlDrawable(img, rowItem.getImageId(),drawable,
600000);
return convertView;
}
}
RowItem.java
public class RowItem<T> {
private String imageId;
private String title;
private String desc;
public RowItem(String imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public String getImageId() {
return imageId;
}
public void setImageId(String imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "
" + desc;
}
}
MainActiviy.java
protected void onCreate(Bundle onSaveInstanceState ) {
super.onCreate(onSaveInstanceState );
setContentView(R.layout.activity_main);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, Open the Next Screen
Intent r = new Intent(MainActivity.this, KategoriActivity.class);
r.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
r.putExtra("cat_img", asd[2][position]);
r.putExtra("cat_id", asd[1][position]);
r.putExtra("cat_id2", asd[1][position]);
r.putExtra("cat_name",asd[0][position]);
// r.putExtra("cat_img", bit[1][position]);
startActivityForResult(r, position);
}
});
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONArray jsonResponse = new JSONArray(result);
asd = new String[3][jsonResponse.length()];
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < jsonResponse.length(); i++) {
JSONObject js = jsonResponse.getJSONObject(i);
asd[0][i] = js.getString("Category_Name");
asd[2][i] = js.getString("Image");
asd[1][i] = js.getString("Term_ID");
RowItem item = new RowItem(asd[2][i], asd[0][i], "");
rowItems.add(item);
}
adapter = new CustomListViewAdapter(MainActivity.this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
希望你能帮助我..
推荐答案
通常,如果您有像 RowItem
这样的自定义类型的 ArrayAdapter,它不会自动为您处理过滤.毕竟,它怎么知道要过滤RowItem
的哪个属性?您应该覆盖 getFilter
方法,并将过滤后的列表与您的适配器中的整体列表分开存储.像这样:
Normally if you have an ArrayAdapter of of a custom type like RowItem
it won't automatically handle filtering for you. After all, how can it know which property of RowItem
to filter? You should override the getFilter
method, and store the filtered list separately from the overall list in your adapter. Something like this:
public class CustomListViewAdapter extends ArrayAdapter<RowItem>
{
private final ArrayList<RowItem> mItems;
private ArrayList<RowItem> mFilteredItems;
private final Comparator<Object> mComparator;
public CustomListViewAdapter(Context context, int textViewResourceId, List<RowItem> items)
{
super(context, textViewResourceId);
mItems = new ArrayList<RowItem>(items);
mFilteredItems = new ArrayList<RowItem>(items);
mComparator = new Comparator<Object>()
{
@Override
public int compare(Object o1, Object o2)
{
String s1 = ((RowItem)o1).getTitle();
String s2 = ((RowItem)o2).getTitle();
return s1.toLowerCase(Locale.getDefault()).compareTo(s2.toLowerCase());
}
};
Collections.sort(mItems, mComparator);
Collections.sort(mFilteredItems, mComparator);
}
@Override
public int getCount()
{
return mFilteredItems.size();
}
@Override
public RowItem getItem(int position)
{
return mFilteredItems.get(position);
}
@Override
public int getPosition(RowItem item)
{
return mFilteredItems.indexOf(item);
}
@Override
public Filter getFilter()
{
return new Filter()
{
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
mFilteredItems = (ArrayList<RowItem>)results.values;
if (results.count > 0)
{
notifyDataSetChanged();
}
else
{
notifyDataSetInvalidated();
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
List<RowItem> filteredResults = new ArrayList<RowItem>();
for (RowItem item : mItems)
{
if (item.getTitle().toLowerCase(Locale.getDefault()).contains(constraint.toString().toLowerCase(Locale.getDefault())))
{
filteredResults.add(item);
}
}
Collections.sort(filteredResults, mComparator);
FilterResults results = new FilterResults();
results.values = filteredResults;
results.count = filteredResults.size();
return results;
}
};
}
}
在 MainActivity 中:
and in MainActivity:
RowItem item = adapter.getItem(position)
r.putExtra("cat_img", item.getImageId());
r.putExtra("cat_id", item.getTermId());
r.putExtra("cat_id2", item.getTermId());
r.putExtra("cat_name", item.getTitle());
您还必须向 RowItem 添加 termid 字段,因为它在您的 JSON 响应中...
You Also have to add a termid field to RowItem, since it's in your JSON response...
这篇关于在Android中搜索ListView项后,总是打开ListView的第一项(问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!