活动和过滤ListView的问题

时间:2017-07-22 14:15:54

标签: java android

我一直在搜索如何在ListView内过滤SearchView。 但是现在我试图找出如何根据最佳结果连接到Activity。

我已经尝试使用此代码,但仍然可以使用相同的Activity但搜索结果不同。

package com.rahmadewi30.android.kamus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

public class Search extends AppCompatActivity implements SearchView.OnQueryTextListener {

    ListView lvItem;
    ArrayAdapter<String> adapter;
    SearchView inputSearch;
    String[] tumbuhan = new String[]{
        "Lumut Tanduk","Lumut Hati","Lumut Daun" ,"Semanggi", "Bakau",
        "Paku Kawat", "Nanas","Pisang","Mangga"};

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

        lvItem = (ListView) findViewById(R.id.listItem);
        inputSearch = (SearchView) findViewById(R.id.editCari);
        adapter = new ArrayAdapter<>(Search.this,
            android.R.layout.simple_list_item_1, android.R.id.text1, tumbuhan);

        inputSearch.setOnQueryTextListener(this);
        lvItem.setAdapter(adapter);
        lvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    Intent l = new Intent(view.getContext(), ActivityLumut.class);
                    startActivityForResult(l, 0);
                } else if (position == 1) {
                    Intent l = new Intent(view.getContext(), ActivityLumutHati.class);
                    startActivityForResult(l, 1);
                } else if (position == 2) {
                    Intent l = new Intent(view.getContext(), ActivityLumutDaun.class);
                    startActivityForResult(l, 2);
                }
            }
        });
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }

}

1 个答案:

答案 0 :(得分:0)

你应该尝试这个:

lvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    if (position == 0) {
        Intent myIntent = new Intent(view.getContext(), YourActivityName.class); 
         startActivityForResult(myIntent, 0); 
    } else if (position == 1) { 
        Intent myIntent = new Intent(view.getContext(), Youractivty2.class); 
       startActivityForResult(myIntent, 0); 
    } else if (position == 2) {
        Intent myIntent = new Intent(view.getContext(), YourActivity3.class); 
         startActivityForResult(myIntent, 0); 
     } else if (position == 3) { 
          Intent myIntent = new Intent(view.getContext(), YourActivity4.class); 
          startActivityForResult(myIntent, 0); 
      } 
  });