1

I tried to make AutoComplete Search for my app, but my code filtered titles of books and showed wrong items in my ListView. It doesn't make senses. For example, when I typed 'M' in EditText, it showed others list these aren't start alphabet with 'M' or without 'M' in title names of books. Help me please?

This is my adapter that I extended 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;
    }
}

And this is a part of MainActivity that I called 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

        }
    });
}

I called and setAdapter like this

customList = new CustomList(MainActivity.this, GetAlIBooks.titles, GetAlIBooks.authors, GetAlIBooks.prices, GetAlIBooks.pricebycovers, GetAlIBooks.percentdiscounts, GetAlIBooks.imageURLs, GetAlIBooks.bitmaps);
listView.setAdapter(customList);

This is my ListView Layout in XML

<?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>
  • In every TextWatcher example, I see the `toString()` method being used. Try `customList.getFilter().filter(cs.toString());` ? – CzarMatt Jan 15 '16 at 01:24
  • CzarMatt, Yes, I've tried it, but it still not working. It seem I got the same result. I have only one book that the title name start with 'M' that the name is "Marketing is everything!". Others are title names are in my language. All of alphabets in English and Numbers (0-9), it could be showed only 'm' , M'', 'e', 'E' , 't' and 'T'. By the way, it's not correct result that should to be. I don't know why. – Bomb Staunchman Jan 15 '16 at 01:52
  • I've finished to solve this problem already. Follow this link http://stackoverflow.com/a/14119383/1844047 – Bomb Staunchman Jan 18 '16 at 21:51

0 Answers0