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) {
}
});
Change this:
To this:
Also, once you passed the data to the adapter, getView is the only function you need to implement:
Here is a full example of a
ArrayAdapter
: