如何在AndroidListViewWithSearch中为Search搜索索引

时间:2015-05-26 07:55:06

标签: android android-listview android-search

我试图用listview创建搜索功能。 listView中的每个项目都有自己的活动要启动。我的问题是我使用索引位置进行搜索。如果我搜索" Mango",索引将自动更改为0(问题所在)并启动activity1。 "芒果"应该开始活动2,而不是1.那么如何在搜索后将其保持在各自的活动中?

package com.try2.androidlistviewwithsearch;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

    // List view
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;
    public static final String products[] = new String [3];

    // ArrayList for Listview
    static  ArrayList<HashMap<String, String>> productList;

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

        // Listview Data

        products[0]="Apple";
        products[1]="Mango";
        products[2]="Orange";



        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);

        //ListView lv = getListView();

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

              if(position == 0) {
                  //code specific to first list item    
                  Intent myIntent = new Intent(view.getContext(), try1.class);
                  startActivityForResult(myIntent, 0);
              }

              if(position == 1) {
                  //code specific to first list item    
                  Intent myIntent = new Intent(view.getContext(), try2.class);
                  startActivityForResult(myIntent, 0);
              } 
          }
        });

        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.this.adapter.getFilter().filter(cs);   
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

尝试更改您的听众

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

    // getting values from selected ListItem vo value je sprava
    String prodname = ((TextView) view.findViewById(R.id.product_name)).getText().toString();

          if( prodname.equals("Apple") {
              //code specific to first list item    
              Intent myIntent = new Intent(view.getContext(), try1.class);
              startActivityForResult(myIntent, 0);
          }

          if( prodname.equals("Mango") {
              //code specific to first list item    
              Intent myIntent = new Intent(view.getContext(), try2.class);
              startActivityForResult(myIntent, 0);
          }
          if( prodname.equals("Orange") {
              //code specific to first list item    
              Intent myIntent = new Intent(view.getContext(), try3.class);
              startActivityForResult(myIntent, 0);
          } 
      }
    });