Android Listview搜索功能

时间:2017-04-24 10:42:07

标签: android listview search

我的代码有问题..我试图在我的列表视图中添加一个搜索功能在android ..它在某种程度上工作但是当我搜索它给了我其他活动而不是真正的结果..就像在我的listview中我搜索销售,即第五个活动,它显示结果,但当我点击打开活动时,它打开Sani活动,即列表上的第一个活动......

这是活动Java代码

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);
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

当您过滤适配器并通知结果时,您的项目位置会发生变化,您无法使用位置作为标识符来打开活动。您的搜索数据发生了变化,但您始终点击位置0,因此Sani活动始终会被打开。

这里我修改了你的代码:

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);
                }
            }
        });

我认为这会帮助你。

答案 1 :(得分:0)

ListViewonItemClick中为您提供的位置是ListView中当前可见项顶部点击项的位置。由于列表视图已过滤,因此位置可能不等于teams数组中的项目位置。

您可以从适配器本身获取该项目。以下代码适合您。

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;
        }
    }
});