自定义listView过滤后出错位置

时间:2017-08-25 10:49:31

标签: android listview android-custom-view listadapter android-filterable

我在Android上是新手,当我使用baseadapter过滤listview而不是获得真实结果但是在点击此项目后,我无法获得该项目的相关值。 像原始列表一样:A - B -C -D 过滤后的清单:B - C - D. 当我点击" B"在筛选列表中,程序流A的值。我不知道,如何处理,我尝试了很多解决方案,但我无法做到。我需要帮助。

我的适配器类

public class MyListAdapter extends BaseAdapter implements Filterable {

MediaPlayer mPlayer=new MediaPlayer() ;
private String[] gelenLink;
private int[] IMAGE_ID = {R.drawable.police};
private List<String>originalData = null;
private List<String>filteredData = null;
private LayoutInflater mInflater;
private ItemFilter mFilter = new ItemFilter();

public MyListAdapter(Context context, List<String> data, String[] linkler) {
    this.filteredData = data ;
    this.originalData = data ;
    this.gelenLink= linkler;

    mInflater = LayoutInflater.from(context);
}

public int getCount() {
    return filteredData.size();
}

public Object getItem(int position) {
    return filteredData.get(position);
}

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


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

    ViewHolder holder;

    convertView=null;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.radio_listview, null);
        holder= new ViewHolder();
        holder.img = (ImageView) convertView.findViewById(R.id.list_item_thumbnail);
        holder.img.setImageResource(IMAGE_ID[0]);
        holder.title = (TextView) convertView.findViewById(R.id.list_item_text);
        holder.button = (Button) convertView.findViewById(R.id.list_item_btn);
        holder.stop_button = (Button) convertView.findViewById(R.id.list_item_stop_btn);


        convertView.setTag(holder);
    } else {

        holder = (ViewHolder) convertView.getTag();
    }

    holder.title.setText(filteredData.get(position));

    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {

                radioLinks(gelenLink[position]);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void radioLinks(String city) throws IOException {

            if(mPlayer.isPlaying()) {
                mPlayer.stop();
                mPlayer.reset();
            }
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.setDataSource(city);
            mPlayer.prepareAsync();
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mPlayer.start();
                }
            });

        }

    });

    holder.stop_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPlayer.stop();
            mPlayer.reset();
        }
    });

    return convertView;
}


public Filter getFilter() {
    return mFilter;
}

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final List<String> list = originalData;

        int count = list.size();
        final ArrayList<String> nlist = new ArrayList<String>(count);

        String filterableString ;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i);
            if (filterableString.toLowerCase().contains(filterString)) {
                nlist.add(filterableString);
            }
        }

        results.values = nlist;
        results.count = nlist.size();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<String>) results.values;
        notifyDataSetChanged();
    }

}}

我的viewHolder类:

public class ViewHolder {
ImageView img;
TextView title;
Button button;
Button stop_button;}

MainActivity部分:

            ArrayList<String> radioname = new ArrayList<String>();
            ArrayList<String> tmp = new ArrayList<String>();
            private String[] radiolinks;            
            MyListAdapter adapter;
            EditText editText;

/////////////////
//onPostExecute
////////////////
     editText = (EditText) findViewById(R.id.search_tv);
                adapter = new MyListAdapter(getApplicationContext(), tmp, radiolinks);
                listView1.setAdapter(adapter);
                listView1.setTextFilterEnabled(true);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

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

                adapter.getFilter().filter(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

1 个答案:

答案 0 :(得分:1)

改变这个:

public MyListAdapter(Context context, List<String> data, String[] linkler) {
    super(context, 0, data);
    this.filteredData = data ;
    this.originalData = data ;
    this.gelenLink= linkler;

    mInflater = LayoutInflater.from(context);
}

对此:

public View getView(int position, View convertView, ViewGroup parent);

此外,一旦将数据传递给适配器,getView就是您需要实现的唯一功能:

ArrayAdapter

以下是public class MyAdapterWithHolder extends ArrayAdapter<FileInfo> { ArrayList<FileInfo> dataSet; LayoutInflater inflater; ListView listView; public MyAdapterWithHolder(Context context, ArrayList<FileInfo> objects, ListView listView) { super(context, 0, objects); dataSet = objects; inflater = LayoutInflater.from(context); this.listView = listView; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder rowHolder = null; if( convertView == null ){//create new row convertView = inflater.inflate( R.layout.row, null ); rowHolder = new Holder(); rowHolder.image = (ImageView) convertView.findViewById( R.id.imageView1 ); rowHolder.name = (TextView) convertView.findViewById( R.id.folderName1 ); rowHolder.count = (TextView) convertView.findViewById( R.id.itemCount1 ); rowHolder.date = (TextView) convertView.findViewById( R.id.date1 ); convertView.setTag(rowHolder); }else{ rowHolder = (Holder) convertView.getTag(); } final FileInfo p = getItem( position ); rowHolder.name.setText(p.fileName); rowHolder.date.setText(p.creationDate); rowHolder.image.setImageResource(p.icon); if( p.isDirectory() ) rowHolder.count.setText( p.size == -1 ? "" : ( "" + p.size + " items" ) ); else rowHolder.count.setText( p.fileSize ); return convertView; } static class Holder{ ImageView image; TextView name; TextView count; TextView date; } }

的完整示例
date