6 jun
2017

Search filter on listview returns wrong results

i create an app when i click on listview show result correct(work normaly) but when i try to using search filer, the result is wrong. e.g : normal list >

  • AA
  • BA
  • CC
  • DA
  • EE

when i search "A" show

  • AA
  • BA
  • DA

but when i click "DA", result is "CC". this wrong is possition but i dont understand about this. code of mainActivity :

public class MainActivity extends AppCompatActivity {

private EditText filterText;
private ArrayAdapter<String> listAdapter;

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

    filterText = (EditText) findViewById(R.id.editText);
    ListView itemList = (ListView) findViewById(R.id.listView);
    DbBackend dbBackend = new DbBackend(MainActivity.this);
    String[] terms = dbBackend.kamusKosakata();

    listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, terms);

    itemList.setAdapter(listAdapter);
    itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // make Toast when click
            Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(MainActivity.this, KamusActivity.class);
            intent.putExtra("KAMUS_ID", position);
            startActivity(intent);
        }
    });

    filterText.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) {
            MainActivity.this.listAdapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}}

and next activity is kamusActivity

public class KamusActivity extends AppCompatActivity {

private TextView artiKata;
private TextToSpeech convertToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kamus);

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    int kamusId = bundle.getInt("KAMUS_ID");
    int id = kamusId + 1;

    TextView kata = (TextView)findViewById(R.id.kata);
    artiKata = (TextView)findViewById(R.id.kamus);
    Button textToSpeech = (Button)findViewById(R.id.button);

    DbBackend dbBackend = new DbBackend(KamusActivity.this);
    KamusObject allKamusQuestions = dbBackend.getKamusById(id);

    kata.setText(allKamusQuestions.getKata());
    artiKata.setText(allKamusQuestions.getDefinisi());

    textToSpeech.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String convertTextToSpeech = artiKata.getText().toString();
            convertToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onInit(int status) {
                    if(status != TextToSpeech.ERROR){
                        convertToSpeech.setLanguage(Locale.US);
                        convertToSpeech.speak(convertTextToSpeech, TextToSpeech.QUEUE_FLUSH, null, null);
                    }
                }
            });
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_dictionary, menu);
    return true;
}

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onPause() {
    if(convertToSpeech != null){
        convertToSpeech.stop();
        convertToSpeech.shutdown();
    }
    super.onPause();
}}

and here screenshot my app when I click first item, show result next activity and work normally but when I search searchOnMainActivity.png and click first result of search and show wrong result(show original position before using search)wrong result.png. note : first item is "abdomen"

COMENTARIOS

luis

could you solve it? I have the same problem and I dont understand some solutions like this one https://stackoverflow.com/questions/14663725/list-view-filter-android Can you help me? Thanks...

luis

I am sorry wrong link! I have nothing!

DEJA TU COMENTARIO

© 2017 website by Rubit Corporation