0

我正在尝试制作一个应用程序,它将使用具有 autocompleteTextView 的 listView.app 显示带有频道编号和频道徽标的频道名称,如果我输入 S 它将显示每个建议 4 次,然后如果我按“Z” “然后它什么也没显示。

    public class MyArrayAdapter extends ArrayAdapter<RowItem> implements Filterable {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.d(TAG, "getview");
    RowItem rowitem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) ctx
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    View rowView = mInflater.inflate(R.layout.list_item, null);

    ImageView ChannelImage = (ImageView) rowView.findViewById(R.id.icon);

    TextView txtChannelName = (TextView) rowView.findViewById(R.id.title);
    txtChannelName.setText(rowitem.getChannelName());

    TextView txtChannelNumber = (TextView) rowView
            .findViewById(R.id.ChannelNumber);
    txtChannelNumber.setText(rowitem.getChannelNumber());

    ChannelImage.setImageDrawable(ctx.getResources().getDrawable(R.drawable.apple));
    return rowView;

}

@Override
public Filter getFilter() {
    Filter channelFilter = new Filter() {

        private FilterResults fr = new FilterResults();


        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            Log.d(TAG,"performFiltering");
            FilterResults results = new FilterResults();
            if (constraint != null) {
                ArrayList<RowItem> res = new ArrayList<RowItem>();
                for (int x = 0; x < channelItems.size(); x++) {
                    if (channelItems
                            .get(x)
                            .getChannelName()
                            .toLowerCase()
                            .startsWith(constraint.toString().toLowerCase())) {
                        res.add(0,channelItems.get(x));
                    }
                }
                results.values = res;
                results.count = res.size();
            } else {
                ArrayList<RowItem> FullList = new ArrayList<RowItem>(
                        channelItems);
                results.values = FullList;
                results.count = FullList.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            Log.d(TAG,"PublishResults");
            if (results.count > 0) {
                clear();
                ArrayList<RowItem> printchannelList = (ArrayList<RowItem>) results.values;
                if (printchannelList != null) {
                    for (int i = 0; i < printchannelList.size(); i++) {
                        add(printchannelList.get(i));
                    }
                }
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    };
    return channelFilter;
}

private final static String TAG = MyArrayAdapter.class.getName();

private Context ctx;
private List<RowItem> channelItems;

public MyArrayAdapter(Context context, int ResourceId, int channelNumber,
        List<RowItem> items) {
    super(context, ResourceId, channelNumber, items);
    this.ctx = context;

    channelItems = items;

}

@Override
public int getCount() {
    Log.d(TAG, "getcount");
    return channelItems.size();
}

@Override
public RowItem getItem(int index) {
    Log.d(TAG, "getItem");
    return channelItems.get(index);
}

} 在此处输入代码

4

0 回答 0