我尝试对我的应用进行自动完成搜索,但我的代码过滤了书籍标题并在我的ListView中显示了错误的项目。它没有感官。例如,当我在EditText中键入“M”时,它显示了其他列表,这些列表不是以书籍的标题名称中的“M”或“M”开头的字母。请帮帮我?
这是我扩展ArrayAdapter的适配器。
public class CustomList extends ArrayAdapter<String> {
private String[] titles;
private String[] authors;
private String[] prices;
private String[] pricebycovers;
private String[] percentdiscounts;
private String[] urls;
private Bitmap[] bitmaps;
private Activity context;
public CustomList(Activity context, String[] titles, String[] authors, String[] prices, String[] pricebycovers, String[] percentdiscounts, String[] urls, Bitmap[] bitmaps) {
//super(context, R.layout.list_view, urls);
//super(context, R.layout.list_view, titles);
super(context, R.layout.list_view, R.id.editTextTitle, titles);
this.context = context;
this.titles = titles;
this.authors = authors;
this.prices = prices;
this.pricebycovers = pricebycovers;
this.percentdiscounts = percentdiscounts;
this.urls= urls;
this.bitmaps= bitmaps;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_view, null, true);
TextView tvTitle = (TextView) listViewItem.findViewById(R.id.tvTitle);
TextView tvAuthor = (TextView) listViewItem.findViewById(R.id.tvAuthor);
TextView tvPrice = (TextView) listViewItem.findViewById(R.id.tvPrice);
TextView tvPriceByCover = (TextView) listViewItem.findViewById(R.id.tvPriceByCover);
TextView tvPercentageDiscount = (TextView) listViewItem.findViewById(R.id.tvPercentageDiscount);
ImageView imvBookPic = (ImageView) listViewItem.findViewById(R.id.imvBookPic);
tvTitle.setText(titles[position]);
tvAuthor.setText(authors[position]);
tvPrice.setText(prices[position]);
tvPriceByCover.setText(pricebycovers[position]);
tvPercentageDiscount.setText(percentdiscounts[position]);
imvBookPic.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position],80,80,false));
return listViewItem;
}
public String[] getTitle(){
return titles;
}
}
这是MainActivity的一部分,我称之为TextWatcher
private ListView listView;
private CustomList customList;
private CustomList customList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
/**
* Enabling Search Filter
* */
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.customList.getFilter().filter(cs);
//Log.w("d",this.customList.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
}
});
}
我这样打电话和setAdapter
customList = new CustomList(MainActivity.this, GetAlIBooks.titles, GetAlIBooks.authors, GetAlIBooks.prices, GetAlIBooks.pricebycovers, GetAlIBooks.percentdiscounts, GetAlIBooks.imageURLs, GetAlIBooks.bitmaps);
listView.setAdapter(customList);
这是我在XML中的ListView布局
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imvBookPic"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_column="0"
android:layout_marginRight="10dp"
android:layout_row="0"
android:layout_rowSpan="4"
android:background="@drawable/book_default" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:layout_row="0"
android:text="Title"
android:textStyle="bold" />
<TextView
android:id="@+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_weight="1"
android:text="Author"
android:textSize="12sp" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_marginRight="5dp"
android:layout_row="2"
android:text="Price"
android:textColor="@color/red" />
<TextView
android:id="@+id/tvPriceByCover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_row="2"
android:text="Price by cover" />
<TextView
android:id="@+id/tvPercentageDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:layout_row="3"
android:text="Percentage Discount"
android:textColor="@color/green" />
</GridLayout>