我有一个arrayadapter的列表视图..我需要在我的音乐应用程序中实现它…帮助我
public class Search_Class extends Activity { EditText searchedit; ListVIEw ListvIEw3; SongsManager songManager; Context context; String[] abc; ArrayList<String> names; ArrayList<String> arrayList; ArrayAdapter<String> ad; private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { // Todo auto-generated method stub super.onCreate(savedInstanceState); setContentVIEw(R.layout.my_search); context = getBaseContext(); searchedit = (EditText) findVIEwByID(R.ID.searchedit); ListvIEw3 = (ListVIEw) findVIEwByID(R.ID.ListvIEw3); songManager = new SongsManager(); songsList = songManager.getPlayList(context); names = new ArrayList<String>(); arrayList = new ArrayList<String>(); for (int i = 0; i < songsList.size(); i++) { HashMap<String, String> obj = songsList.get(i); names.add(obj.get("songTitle")); arrayList.add(obj.get("songTitle")); } abc = new String[names.size()]; abc = names.toArray(abc); ListvIEw3.setTextFilterEnabled(true); // ad = new ArrayAdapter<String>(MySearch.this, // androID.R.layout.simple_List_item_1, abc); // ListvIEw3.setAdapter(ad); ListvIEw3.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { // getting Listitem index String[] abc1 = new String[arrayList.size()]; abc1 = arrayList.toArray(abc); int songIndex; for (int i = 0; i < abc1.length; i++) { if (abc[position].equalsIgnoreCase(abc1[i])) { songIndex = i; Intent in = new Intent(Search_Class.this, MainActivity.class); // Sending songIndex to PlayerActivity SharedPreferences sp; sp = getSharedPreferences("lastplayed", MODE_PRIVATE); Editor edit = sp.edit(); edit.putBoolean("searchSongIndex", true); edit.commit(); in.putExtra("songIndex", songIndex); in.putExtra("FLAG", true); startActivity(in); finish(); break; } } } }); // ad.notifyDataSetChanged(); searchedit.addTextChangedListener(new TextWatcher() { @OverrIDe public voID onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // Todo auto-generated method stub // MySearch.this.ad.getFilter().filter(arg0); String text = arg0.toString().tolowerCase(Locale.getDefault()); filter(text); } @OverrIDe public voID beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // Todo auto-generated method stub } @OverrIDe public voID afterTextChanged(Editable arg0) { // Todo auto-generated method stub } }); } public voID filter(String charText) { System.out.println("Text---" + charText); charText = charText.tolowerCase(Locale.getDefault()); names.clear(); if (charText.length() == 0) { System.out.println("if---" + arrayList.size()); abc = new String[arrayList.size()]; abc = arrayList.toArray(abc); // names.addAll(arrayList); } else { System.out.println("else"); for (int i = 0; i < abc.length; i++) { System.out.println(abc[i] + "----" + charText); if (abc[i].tolowerCase(Locale.getDefault()).contains(charText)) { names.add(abc[i]); } } abc = new String[names.size()]; abc = names.toArray(abc); } ad = new ArrayAdapter<String>(Search_Class.this, androID.R.layout.simple_List_item_1, abc); ListvIEw3.setAdapter(ad); ad.notifyDataSetChanged(); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Todo auto-generated method stub return false; } }Just Created A Custom `ListvIEw` Using `ListActivity` public class SearchingClass extends ListActivity { EditText et; ListVIEw lv; Context context; ArrayList<String> AllSongs; String[] AllSongsArray; SongsManager songManager; private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); SearchClass_Adapter adapter; int textlength = 0; @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<String>(); for (int i = 0; i < songsList.size(); i++) { HashMap<String, String> 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) { // Abstract Method of TextWatcher Interface. } public voID beforeTextChanged(CharSequence s, int start, int count, int after) { // Abstract Method of TextWatcher Interface. } 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 you want to highlight the countrIEs which start * with entered letters then choose this block. And * comment the below If condition Block */ /* * if(et.getText().toString().equalsIgnoreCase( (String) * ListvIEw_names[i].subSequence(0, textlength))) { * array_sort.add(ListvIEw_names[i]); * image_sort.add(ListvIEw_images[i]); } */ /*** * If you choose the below block then it will act like a * like operator in the MysqL */ if (AllSongsArray[i].tolowerCase().contains( et.getText().toString().tolowerCase().trim())) { AllSongs.add(AllSongsArray[i]); } } } 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<String> 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 class bsAdapter extends BaseAdapter { Activity cntx; 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 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)); return row; } }}
解决方法:
首先添加两个函数getData并将Data Search设置为baseadapter
public class bsAdapter extends BaseAdapter { Activity cntx; 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) { this.search = data; } public String getData() { return this.search; }
首先在Search_class中声明searchData
public String searchData;private bsAdapter mAdapter;
并声明
mAdapter = new bsAdapter(this);
然后在文本观察器中添加条件
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); }
然后将此功能放在baseadapter中
public static 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.text_color_white)), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); start = normalizedText.indexOf(search, spanEnd); } return highlighted; } }
并把这个
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); if(search != null){ tv.setText(highlight(search,AllSongs.get(position)); } else if (search == null){ tv.setText(AllSongs.get(position)); } return row;
总结 以上是内存溢出为你收集整理的在Listview中搜索,并在Listview Android中突出显示搜索到的文本颜色全部内容,希望文章能够帮你解决在Listview中搜索,并在Listview Android中突出显示搜索到的文本颜色所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)