无法使用自定义适配器从ListView获取ListView的结果

时间:2017-09-21 04:27:52

标签: java android listview android-studio searchview

我有一个带自定义BaseAdapter的Listview。我已经完成了MainActivity.java的代码来应用SearchView。但是,我的Adapter类中的performFiltering方法有问题,因为我有一个将在我的ListView上显示的ImageView。 ImageView最初是String的形式,因为我想从我的Firebase存储中的URL获取图像,然后使用Picasso库将其转换为Image。 TL:DR。我的问题是,每次执行搜索时,Firebase中存储的图像的网址都会显示而不是实际的图像。感谢帮助。

以下是我的适配器类:

public class GridAdapter extends BaseAdapter implements Filterable {

CustomFilter mCustomFilter;
ArrayList<GridItem> filterList;
private Context mContext;
private ArrayList<GridItem> gtem;

//Constructor


public GridAdapter(Context mContext, ArrayList<GridItem> gtem) {
    this.filterList = gtem;
    this.mContext = mContext;
    this.gtem = gtem;
}

@Override
public int getCount() {
    return gtem.size();
}

@Override
public Object getItem(int position) {
    return gtem.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = View.inflate(mContext,R.layout.grid_item, null);
    ImageView putPic = (ImageView)v.findViewById(R.id.imageHere);
    TextView putTitle = (TextView)v.findViewById(R.id.titleHere);
    TextView putPrice = (TextView)v.findViewById(R.id.per_price);
    TextView putCountry = (TextView)v.findViewById(R.id.countryHere);


    //
    putCountry.setText("Country: " + gtem.get(position).getCountry());
    putTitle.setText(gtem.get(position).getmTitleHere());
    putCountry.setText(gtem.get(position).getCountry());
    putPrice.setText("Price: " + "$" + gtem.get(position).getmPriceHere());
    Picasso.with(mContext).load(gtem.get(position).getmImageView()).resize(450 , 500).into(putPic);


    return v;
}



@Override
public Filter getFilter() {
    //To-do Auto-generated method stub
    if (mCustomFilter == null )
    {
        mCustomFilter = new CustomFilter();
    }

    return mCustomFilter;
}

//Inner Class
class CustomFilter extends Filter
{

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        FilterResults results = new FilterResults();
        if (constraint != null && constraint.length()>0)
        {
            //Constraint to Upper
            constraint = constraint.toString().toUpperCase();

            ArrayList<GridItem> filters = new ArrayList<GridItem>();

            //to get specific items
            for (int i = 0; i<filterList.size(); i++)
            {
                if (filterList.get(i).getmTitleHere().toUpperCase().contains(constraint))
                {
                    GridItem g = new GridItem(filterList.get(i).getmTitleHere() , filterList.get(i).getmPriceHere(), filterList.get(i).getmImageView()
                    , filterList.get(i).getCountry());

                    filters.add(g);
                }
            }
            results.count = filters.size();
            results.values = filters;

        }else {
            results.count = filterList.size();
            results.values = filterList;
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        gtem = (ArrayList<GridItem>) results.values;
        notifyDataSetChanged();


    }
}
}

0 个答案:

没有答案