当我打开搜索项时,它打开的是 ListView 的第一项而不是搜索项。即使搜索并找到项目成功。
让我举个例子:
- 如果我搜索
Arrow
,我可以得到Arrow
但是当点击Arrow
它时Almost Human
,Almost Human
是我的 ListView 的第一项 - 如果我搜索
American
我正在获取The Americans (first)
并且American Horror Story (second)
(两个结果)搜索过程成功,但如果单击The Americans (second one)
它会转到 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;
}
}
行项目.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 + "\n" + 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();
}
希望你能帮我..