如何修改此代码,以便突出显示listView中的搜索结果?
这是我项目的完整代码。
public class MainActivity extends Activity implements OnItemClickListener, TextWatcher {
private ListView daftarLagu;
private String[] tampungLaguLirik = null;
protected static String strSearchLyric = null;
// Test Highlight
private String mSearchText;
//deklarasi keperluan untuk database
private SQLiteDatabase db = null;
private Cursor LaguCursor = null;
private DatabaseSong datalagu = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
datalagu = new DatabaseSong(this);
db = datalagu.openDataBase();
db = datalagu.getReadableDatabase();
datalagu.onCreate(db);
//buat object dari dataLagu
setContentView(R.layout.activity_search);
daftarLagu = (ListView)findViewById(R.id.listIndexLagu);
//buat listener dari list view pada aplikasi pencarian tanaman obat
daftarLagu.setOnItemClickListener(this);
//Tombol Ganti Jenis Pencarian
TextView text = (TextView) findViewById(R.id.pencarian);
text.setText("PENCARIAN LIRIK LAGU");
Button nomorBtn = (Button) findViewById(R.id.btnSwitchPencarian);
nomorBtn.setText("CARI NOMOR, JUDUL, PENGARANG");
nomorBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
db.close();
//startActivity(new Intent(MainActivity.this, SearchActivity.class));
}
});
//Tombol Cari Lagu
EditText textCari = (EditText)findViewById(R.id.editTextcari);
textCari.setHint("Masukan Potongan Lirik Lagu");
textCari.addTextChangedListener(this);
}
//buat overread method dari set on item click listener
public void onItemClick(AdapterView<?> arg0, View arg1, int posisi, long arg3) {
Toast.makeText(MainActivity.this, "ini pas saya click", Toast.LENGTH_SHORT).show();
//Toast.makeText(SearchLyricActivity.this, ""+tampungLaguLirik[posisi], Toast.LENGTH_SHORT).show();
//panggil method yang dibuat untuk handler tampilkan gambar dari data tanaman
cariTampilView(""+tampungLaguLirik[posisi]);
posisi = 0;
}
//fungsi
public void cariTampilView(String cari){
LaguCursor = null;
//memilih lagu dari daftar
LaguCursor = db.rawQuery("SELECT lirik, nomor||'. '||judul "
+ "FROM tbl_Lagusion WHERE (UPPER('\n\n___ '||nomor||'. '||judul)||'\n\n'||lirik) = '"+ cari +"' ", null);
if (LaguCursor.moveToFirst()) {
strSearchLyric = null;
strSearchLyric = LaguCursor.getString(0);
Toast.makeText(MainActivity.this, LaguCursor.getString(1).toString(), Toast.LENGTH_SHORT).show();
//for (; !LaguCursor.isAfterLast(); LaguCursor.moveToNext()) {
// strSearchLyric = LaguCursor.getString(0);
// }
}
if (strSearchLyric.equals("")) {
strSearchLyric = "Terjemahan Not Found";
Toast.makeText(MainActivity.this, "Pencarian tidak ditemukan........!", Toast.LENGTH_SHORT).show();
}else{
//hasil yang didapat dan di tampilkan ke frame_view
Toast.makeText(MainActivity.this, "Sadisssssss\n"+LaguCursor.getString(0).toString(), Toast.LENGTH_SHORT).show();
//Intent x = new Intent(MainActivity.this, LyricViewer.class);
//startActivity(x);
}
}
void tampilLagu(){
strSearchLyric = null;
EditText text=(EditText)findViewById(R.id.editTextcari);
text.setTextColor(Color.parseColor("#0066FF"));
String value = text.getText().toString();
int length = text.length();
value = value.trim();
value = value.replaceAll("'","’");
//value = value.replaceAll(",","");
if (length == 0){
value = null;
}
int Hitung =0;
//Untuk menampilkan daftar lagu pada Index Lagu
LaguCursor = db.rawQuery("SELECT (UPPER('\n\n___ '||nomor||'. '||judul)||'\n\n'||lirik) FROM tbl_Lagusion WHERE lirik LIKE '%"+value+"%' ", null);
tampungLaguLirik = new String[LaguCursor.getCount()];
LaguCursor.moveToFirst();
Toast.makeText(MainActivity.this, "klot "+LaguCursor.getCount(), Toast.LENGTH_SHORT).show();
for (int cc=0; cc < LaguCursor.getCount(); cc++)
{
Hitung = Hitung +1;
LaguCursor.moveToPosition(cc);
tampungLaguLirik[cc] = LaguCursor.getString(0).toString();
}
if(Hitung == 0){
daftarLagu = (ListView)findViewById(R.id.listIndexLagu);
daftarLagu.setFastScrollEnabled(true);
daftarLagu.setBackgroundColor(Color.TRANSPARENT);
daftarLagu.setAnimationCacheEnabled(true);
daftarLagu.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, tampungLaguLirik));
daftarLagu.setSelected(true);
//daftarLagu..setTypeface(null, Typeface.BOLD);
//pesanPengguna("Lagu Tidak Ditemukan!");
}else{
daftarLagu = (ListView)findViewById(R.id.listIndexLagu);
daftarLagu.setFastScrollEnabled(true);
daftarLagu.setBackgroundColor(Color.TRANSPARENT);
daftarLagu.setAnimationCacheEnabled(true);
daftarLagu.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, tampungLaguLirik));
daftarLagu.setSelected(true);
}
}
private String getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public void onBackPressed() {
strSearchLyric = null;
db.close();
finish();//memanggil method exit()
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
tampilLagu();
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
// Toast.makeText(MainActivity.this, "klot", Toast.LENGTH_SHORT).show();
// Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]);
if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
// Toast.makeText(MainActivity.this, "klot klot", Toast.LENGTH_SHORT).show();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
}
我真的不知道,我已经搜索了与此相关的答案,但我不知道如何将所有逻辑或代码实现到我的代码中。我还在使用Eclipse。这是我在官方IDE更改为Android Studio之前制作的项目。
请根据我的代码给我一个示例。
我已经尝试过使用spannable但是这里的代码不起作用 我添加了一个方法,但仍然无效,我错过了什么?
private Spannable highlightSearchKey(String title) {
Spannable highlight;
Pattern pattern;
Matcher matcher;
int word_index;
String title_str;
word_index = tampungLaguLirik.length;
title_str = Html.fromHtml(title).toString();
highlight = (Spannable) Html.fromHtml(title);
Toast.makeText(MainActivity.this, "Word Index "+word_index+" terus "+title_str+" Span "+highlight, Toast.LENGTH_SHORT).show();
for (int index = 0; index < word_index; index++) {
pattern = Pattern.compile("(?i)" + tampungLaguLirik[index]);
matcher = pattern.matcher(title_str);
while (matcher.find()) {
highlight.setSpan(
new BackgroundColorSpan(0x44444444),
matcher.start(),
matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return highlight;
}
并在此处调用此方法:
//Untuk menampilkan daftar lagu pada Index Lagu
LaguCursor = db.rawQuery("SELECT (UPPER('\n\n___ '||nomor||'. '||judul)||'\n\n'||lirik) FROM tbl_Lagusion WHERE lirik LIKE '%"+value+"%' ", null);
tampungLaguLirik = new String[LaguCursor.getCount()];
LaguCursor.moveToFirst();
Toast.makeText(MainActivity.this, "klot "+LaguCursor.getCount(), Toast.LENGTH_SHORT).show();
for (int cc=0; cc < LaguCursor.getCount(); cc++)
{
Hitung = Hitung +1;
LaguCursor.moveToPosition(cc);
tampungLaguLirik[cc] = LaguCursor.getString(0).toString();
highlightSearchKey(value);
}
但我仍然无法突出显示我在editText字段中输入的相同字符。