lawang code

lawang Code


Tanyakan kode disini

Bagaimana mengimplementasikan Algoritma Kmp di pencarian listview...??

ditanyakan oleh Ahmad Darmadi 3 tahun yang lalu

Saya mempunyai listview yang berisi record dari database sqlite dan saya ingin menambahkan percarian diatas listview menggunakan kmp algoritma , bagaimana cara penerapan algoritmanya mohon penjelasan dari suhu2 yang ada di mari. trimakasih sebelumnya.

Public class MainActivity extends Activity implements TextWatcher,       AdapterView.OnItemClickListener {

private DatabaseHelper db;
public AutoCompleteTextView autoTv;
private  ListView listView;
private ArrayAdapter<Kamus> adapter;
private List<Kamus> listKamus;



@Override
public  void  onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView =(ListView) findViewById(R.id.list_istilah);
    autoTv = (AutoCompleteTextView) findViewById(R.id.AutoTV);

    db = DatabaseHelper.getInstance(this);

   setData();

    autoTv.addTextChangedListener(this);
    listView.setOnItemClickListener(this);

}

private  void  setData()
{
    listKamus = db.getAllKamus();

    adapter = new ArrayAdapter<Kamus>(this,
            android.R.layout.simple_expandable_list_item_1, listKamus);
    listView.setAdapter(adapter);


}

@Override
            protected  void onDestroy(){
        super.onDestroy();
        db.close();

}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

    Bundle b = new Bundle();
    b.putString("istilah", adapter.getItem(position).getIstilah());
    b.putString("arti", adapter.getItem(position).getArti());

    Intent i = new Intent(this, ArtiActivity.class);
    i.putExtras(b);
    startActivity(i);

}


@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {



}

@Override
public void afterTextChanged(Editable editable) {

}

}

Menampilkan 1-7 dari 7 hasil
handita okviyanto menjawab 3 tahun yang lalu :

Kalau pake filter bawaan dari android gini nih..

    /**
     * Enabling Search Filter
     * */
    @Override
       public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            // When user changed the Text
            MainActivity.this.adapter.getFilter().filter(cs);   
        }


Tapi kalo mau pake algoritma sendiri. Agan bikin custom adapter yang filteringnya juga pake custom filtering.

Contohnya :

class CustomAdapter extends BaseAdapter implements Filterable {

public View getView(){
... 
} 
public Integer getCount()
{ 
... 
} 

@Override 
public Filter getFilter() { 

    Filter filter = new Filter() { 

        @SuppressWarnings("unchecked") 
        @Override 
        protected void publishResults(CharSequence constraint, FilterResults results) { 

            arrayListNames = (List<String>) results.values; 
            notifyDataSetChanged(); 
        } 

        @Override 
        protected FilterResults performFiltering(CharSequence constraint) { 

            FilterResults results = new FilterResults(); 
            ArrayList<String> FilteredArrayNames = new ArrayList<String>(); 

            // perform your search here using the searchConstraint String. 

            constraint = constraint.toString().toLowerCase(); 
            for (int i = 0; i < mDatabaseOfNames.size(); i++) { 
                String dataNames = mDatabaseOfNames.get(i); 
                if (dataNames.toLowerCase().startsWith(constraint.toString()))  { 
                    FilteredArrayNames.add(dataNames); 
                } 
            } 

            results.count = FilteredArrayNames.size(); 
            results.values = FilteredArrayNames; 
            Log.e("VALUES", results.values.toString()); 

            return results; 
        } 
    }; 

    return filter; 
} 

}


handita okviyanto menjawab 3 tahun yang lalu :

Kalau pake filter bawaan dari android gini nih..

    /**
     * Enabling Search Filter
     * */
    @Override
       public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            // When user changed the Text
            MainActivity.this.adapter.getFilter().filter(cs);   
        }


Tapi kalo mau pake algoritma sendiri. Agan bikin custom adapter yang filteringnya juga pake custom filtering.

Contohnya :

class CustomAdapter extends BaseAdapter implements Filterable {

public View getView(){
... 
} 
public Integer getCount()
{ 
... 
} 

@Override 
public Filter getFilter() { 

    Filter filter = new Filter() { 

        @SuppressWarnings("unchecked") 
        @Override 
        protected void publishResults(CharSequence constraint, FilterResults results) { 

            arrayListNames = (List<String>) results.values; 
            notifyDataSetChanged(); 
        } 

        @Override 
        protected FilterResults performFiltering(CharSequence constraint) { 

            FilterResults results = new FilterResults(); 
            ArrayList<String> FilteredArrayNames = new ArrayList<String>(); 

            // perform your search here using the searchConstraint String. 

            constraint = constraint.toString().toLowerCase(); 
            for (int i = 0; i < mDatabaseOfNames.size(); i++) { 
                String dataNames = mDatabaseOfNames.get(i); 
                if (dataNames.toLowerCase().startsWith(constraint.toString()))  { 
                    FilteredArrayNames.add(dataNames); 
                } 
            } 

            results.count = FilteredArrayNames.size(); 
            results.values = FilteredArrayNames; 
            Log.e("VALUES", results.values.toString()); 

            return results; 
        } 
    }; 

    return filter; 
    } 
} 

Nah di dalam publishResults agan bikin tuh algoritma KMPnya disana tinggal baca algoritma diatas . Good luck


handita okviyanto menjawab 3 tahun yang lalu :

algoritma KMP yang mana ya?

yang ini kah mksdnya?

algorithm kmp_search:
input:
    an array of characters, S (the text to be searched)
    an array of characters, W (the word sought)
output:
    an integer (the zero-based position in S at which W is found)

define variables:
    an integer, m ? 0 (the beginning of the current match in S)
    an integer, i ? 0 (the position of the current character in W)
    an array of integers, T (the table, computed elsewhere)

while m + i < length(S) do
    if W[i] = S[m + i] then
        if i = length(W) - 1 then
            return m
        let i ? i + 1
    else
        if T[i] > -1 then
            let m ? m + i - T[i], i ? T[i]
        else
            let i ? 0, m ? m + 1

(if we reach here, we have searched all of S unsuccessfully)
return the length of S


Di dalamKamus ada apa ya?


Ahmad Darmadi menjawab 3 tahun yang lalu :

Iya gan untuk pencarian string .

Di dlm kamus ada array istilah dan arti dari database sqlite yang di tampilin di listview.

Jadi saat di ketik di autoCompleteTextView langsung muncul kata yang dicari gan.


handita okviyanto menjawab 3 tahun yang lalu :
Kalau pake filter bawaan dari android gini nih..

    /**
     * Enabling Search Filter
     * */
    @Override
       public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            // When user changed the Text
            MainActivity.this.adapter.getFilter().filter(cs);   
        }


Tapi kalo mau pake algoritma sendiri. Agan bikin custom adapter yang filteringnya juga pake custom filtering.

Contohnya :

class CustomAdapter extends BaseAdapter implements Filterable {

public View getView(){
... 
} 
public Integer getCount()
{ 
... 
} 

@Override 
public Filter getFilter() { 

    Filter filter = new Filter() { 

        @SuppressWarnings("unchecked") 
        @Override 
        protected void publishResults(CharSequence constraint, FilterResults results) { 

            arrayListNames = (List<String>) results.values; 
            notifyDataSetChanged(); 
        } 

        @Override 
        protected FilterResults performFiltering(CharSequence constraint) { 

            FilterResults results = new FilterResults(); 
            ArrayList<String> FilteredArrayNames = new ArrayList<String>(); 

            // perform your search here using the searchConstraint String. 

            constraint = constraint.toString().toLowerCase(); 
            for (int i = 0; i < mDatabaseOfNames.size(); i++) { 
                String dataNames = mDatabaseOfNames.get(i); 
                if (dataNames.toLowerCase().startsWith(constraint.toString()))  { 
                    FilteredArrayNames.add(dataNames); 
                } 
            } 

            results.count = FilteredArrayNames.size(); 
            results.values = FilteredArrayNames; 
            Log.e("VALUES", results.values.toString()); 

            return results; 
        } 
    }; 

    return filter; 
    } 
} 

Nah di dalam publishResults agan bikin tuh algoritma KMPnya disana tinggal baca algoritma diatas . Good luck


Ahmad Darmadi menjawab 3 tahun yang lalu :

Terima kasih banya masnya.. Saya doain rejekinya ditambah sma yg di atas. Amin


handita okviyanto menjawab 3 tahun yang lalu :

hehe iya sama2. :D


Anda harus login terlebih dahulu untuk memberikan komentar

Recent Tag

X 6
X 48
X 14
X 10
X 1
X 5
X 4
X 13
X 10

Bantuan

Help

Feedback

Help