2017-07-22 57 views
0

我一直在尋找如何使ListView可以在SearchView內過濾。 但現在我試圖找出如何連接到基於最高結果的活動。問題與活動和過濾ListView

我試過這段代碼,但仍然得到相同的活動但不同的搜索結果。

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

} 
+0

點擊('如果(位置== 0){'...)總會啓動相同的活動,不管是什麼item('view')實際上是/ contains。 – Gooz

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

它只是一樣,你只能改變意圖變量。當我點擊時過濾搜索第三個列表,它只是去第一個未經過濾的列表活動。 –