Custom listView getting wrong position after Filter

609 views Asked by At

I am new on Android, when i filter listview with baseadapter than i got true results but after click on this item,i can't get related values of that item. Like original list : A - B -C -D After filtered list : B - C - D when i click "B" in filtered list, program streaming A's value. I have no idea, how to handle, I tried many solutions, but i could'n make it. I need help.

My adapter class

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();
    }

}}

My viewHolder class :

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

MainActivity part:

            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

There are 1 answers

0
RonTLV On

Change this:

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

    mInflater = LayoutInflater.from(context);
}

To this:

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);
}

Also, once you passed the data to the adapter, getView is the only function you need to implement:

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

Here is a full example of a 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;
    }
}