Elpedia
Elpedia

Reputation: 9

Android Listview Search function

Am having problem with my code.. am trying to add a search function to my list view in android.. it is somehow working but when i search it gave me other activity not the real result.. Like in my listview if i search for sale i.e the 5th activity it shows the result but when i click to open the activity it open Sani activity i.e the first activity on the list...

Here is the Activity Java Code

package com.example.bati;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

public class MainActivity extends Activity {
    ListView lv;
    SearchView sv;
    String[] teams = {"Sani", "Bali", "Sati", "Umam", "sale"};
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, teams);
        lv.setAdapter(adapter);
        sv.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    Intent myIntent = new Intent(view.getContext(), Sani.class);
                    startActivityForResult(myIntent, 0);
                }
                if (position == 1) {
                    Intent myIntent = new Intent(view.getContext(), Bali.class);
                    startActivityForResult(myIntent, 1);
                }
                if (position == 2) {
                    Intent myIntent = new Intent(view.getContext(), Sati.class);
                    startActivityForResult(myIntent, 2);
                }
            }
        });
    }
}

Upvotes: 1

Views: 133

Answers (2)

savepopulation
savepopulation

Reputation: 11921

When you filter your adapter and notify with the result your item's positions change you you cannot use position as an identifier to open activity. Your search data changes but you're always clicking the position 0 so always Sani activity gets opened.

Here i modified your code a bit:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final String item = (String)parent.getItemAtPosition(position);
                if ("Sani".equals(item)) {
                    Intent myIntent = new Intent(view.getContext(), Sani.class);
                    startActivityForResult(myIntent, 0);
                }
                if ("Bali".eqauls(item)) {
                    Intent myIntent = new Intent(view.getContext(), Bali.class);
                    startActivityForResult(myIntent, 1);
                }
                if ("Sati".equals(item)) {
                    Intent myIntent = new Intent(view.getContext(), Sati.class);
                    startActivityForResult(myIntent, 2);
                }
            }
        });

I think this'll help you.

Upvotes: 1

Lahiru Chandima
Lahiru Chandima

Reputation: 24138

The position the ListView gives you in onItemClick is the position of the clicked item from the top of the currently visible items in the ListView. Since the list view is filtered, the position may not be equal to the item position in teams array.

You can get the item at the position from the adapter itself. Following code will work for you.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String item = adapter.getItem(position);
        switch (item) {
            case "Sani":
                startActivityForResult(new Intent(view.getContext(), Sani.class), 0);
                return;
            case "Bali":
                startActivityForResult(new Intent(view.getContext(), Bali.class), 1);
                return;
            case "Sati":
                startActivityForResult(new Intent(view.getContext(), Sati.class), 2);
                return;
        }
    }
});

Upvotes: 0

Related Questions