I Have a listview
with arrayadapter
.. i need to implement this in my music application ... help me out
So here i got the code corrected and working fine ...
public class SearchingClass extends ListActivity {
EditText et;
ListView lv;
Context context;
static ArrayList AllSongs;
String[] AllSongsArray;
SongsManager songManager;
private ArrayList> songsList = new ArrayList>();
int textlength = 0;
public String SearchData;
private bsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searching_class);
et = (EditText) findViewById(R.id.EditText01);
lv = (ListView) findViewById(android.R.id.list);
context = getBaseContext();
songManager = new SongsManager();
songsList = songManager.getPlayList(context);
AllSongs = new ArrayList();
mAdapter = new bsAdapter(this);
for (int i = 0; i < songsList.size(); i++) {
HashMap obj = songsList.get(i);
AllSongs.add(obj.get("songTitle"));
}
AllSongsArray = new String[AllSongs.size()];
AllSongsArray = AllSongs.toArray(AllSongsArray);
setListAdapter(new bsAdapter(this));
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = et.getText().length();
AllSongs.clear();
for (int i = 0; i < AllSongsArray.length; i++) {
if (textlength <= AllSongsArray[i].length()) {
if (AllSongsArray[i].toLowerCase().contains(
et.getText().toString().toLowerCase().trim())) {
AllSongs.add(AllSongsArray[i]);
}
}
if (textlength == 0) {
mAdapter.setDataSearch(null);
} else {
SearchData = s.toString().toLowerCase();
mAdapter.setDataSearch(SearchData);
}
}
AppendList(AllSongs);
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), AllSongs.get(position),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.searching_class, menu);
return true;
}
public void AppendList(ArrayList str) {
setListAdapter(new bsAdapter(this));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class bsAdapter extends BaseAdapter {
static Activity cntx;
static String search;
String value;
public bsAdapter(Activity context) {
// TODO Auto-generated constructor stub
this.cntx = context;
}
public int getCount() {
// TODO Auto-generated method stub
return AllSongs.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return AllSongs.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return AllSongs.size();
}
public void setDataSearch(String data) {
System.out.println("recieved value" + data);
this.search = data;
}
public String getData() {
return this.search;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = null;
LayoutInflater inflater = cntx.getLayoutInflater();
row = inflater.inflate(R.layout.listview_item, null);
TextView tv = (TextView) row.findViewById(R.id.tv_artist_name);
// tv.setText(AllSongs.get(position));
System.out.println("search data value" + search);
if (search != null) {
tv.setText(highlight(search, AllSongs.get(position)));
} else if (search == null) {
tv.setText(AllSongs.get(position));
}
return row;
}
public CharSequence highlight(String search, String originalText) {
String normalizedText = Normalizer
.normalize(originalText, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
.toLowerCase();
int start = normalizedText.indexOf(search);
Spannable highlighted = new SpannableString(originalText);
if (start < 0) {
// not found, nothing to to
return originalText;
} else {
while (start >= 0) {
int spanStart = Math.min(start, originalText.length());
int spanEnd = Math.min(start + search.length(),
originalText.length());
highlighted.setSpan(new StyleSpan(
android.graphics.Typeface.BOLD), spanStart,
spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
highlighted.setSpan(new ForegroundColorSpan(cntx
.getResources().getColor(R.color.red)), spanStart,
spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = normalizedText.indexOf(search, spanEnd);
}
return highlighted;
}
}
}