使用baseAdapter实现搜索小部件的问题

时间:2019-02-15 13:13:48

标签: java android listview

我正在使用https://github.com/hellb0y8577/Kiohome/blob/master/src/com/csform/android/uiapptemplate/SearchBarShopActivity.java的源代码

我正确执行了所有操作,但发现我无法在列表视图中正确填充项目。

这是主要的活动代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_order);

        Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/MaterialDesignIcons.ttf");

        mSearchField = (EditText) findViewById(R.id.search_field);
        mXMark = (TextView) findViewById(R.id.search_x);

        mListView = (ListView) findViewById(R.id.list_view);

        mXMark.setTypeface(custom_font);
        searchicon = (TextView)findViewById(R.id.searchicon);

        searchicon.setTypeface(custom_font);

        mListView.setVisibility(mListView.GONE);

        searchableArrayList = DummyContent.getDummyModelList();

        mXMark.setOnClickListener(this);


        mSearchField.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @SuppressLint("DefaultLocale")
            @Override
            public void afterTextChanged(Editable editable) {
                mListView.setVisibility(mListView.VISIBLE);
                String searchText = editable.toString().trim();
                ArrayList<String> searchedArray = new ArrayList<String>();
                for (DummyModel dm : searchableArrayList) {
                    //if (dm.getScheme().toLowerCase().contains(searchText.toLowerCase())) { searchedArray.add(dm.getScheme());}




                    searchedArray.add(dm.getScheme());








                }
                if (searchText.isEmpty()) {
                    mListView.setAdapter(null);
                    mXMark.setText(R.string.fontello_x_mark);
                } else {
                    mListView.setVisibility(ListView.VISIBLE);
                    mListView.setAdapter(new SearchAdapter(
                            NewOrderActivity.this, searchedArray));
                    mXMark.setText(R.string.fontello_x_mark_masked);
                }
            }

这是SearchAdapter

public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_view_item_search, parent, false);
            holder = new ViewHolder();
            holder.icon = (TextView) convertView.findViewById(R.id.productname);
            holder.text = (TextView) convertView.findViewById(R.id.packsize);
            holder.stocks = (TextView) convertView.findViewById(R.id.stockistname);
            holder.pricev = (TextView) convertView.findViewById(R.id.pricevalue);
            holder.schemev = (TextView) convertView.findViewById(R.id.schemename);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //Final SearchModel searchModel = searchlist.get(position);
        String subcategory = mSubcategories.get(position);




        holder.icon.setText(subcategory);
        holder.text.setText(subcategory);
        holder.stocks.setText(subcategory);
        holder.pricev.setText(subcategory);
        holder.schemev.setText(subcategory);


        //Log.i(TAG, subcategory);



        return convertView;

    }

主要问题是我用相同的商品名称填充了所有5件商品

我认为这2个代码块是问题区域

String subcategory = mSubcategories.get(position);




        holder.icon.setText(subcategory);
        holder.text.setText(subcategory);
        holder.stocks.setText(subcategory);
        holder.pricev.setText(subcategory);
        holder.schemev.setText(subcategory);




        return convertView;

    }

public void afterTextChanged(Editable editable) {
                mListView.setVisibility(mListView.VISIBLE);
                String searchText = editable.toString().trim();
                ArrayList<String> searchedArray = new ArrayList<String>();
                for (DummyModel dm : searchableArrayList) {
                    //if (dm.getScheme().toLowerCase().contains(searchText.toLowerCase())) { searchedArray.add(dm.getScheme());}

                    searchedArray.add(dm.getScheme());


                }
                if (searchText.isEmpty()) {
                    mListView.setAdapter(null);
                    mXMark.setText(R.string.fontello_x_mark);
                } else {
                    mListView.setVisibility(ListView.VISIBLE);
                    mListView.setAdapter(new SearchAdapter(
                            NewOrderActivity.this, searchedArray));
                    mXMark.setText(R.string.fontello_x_mark_masked);
                }
            }

希望您能提供所有帮助!

0 个答案:

没有答案