Search code examples
androidandroid-listviewandroid-search

How to constant the index for Search in AndroidListViewWithSearch


I'm trying to make a search function with listview. Each item in listView have it own activity to be start. My problem was I'm using index position for search. If the I search "Mango", the index will automatically change to 0 (which the problem is) and start activity1. "Mango" should start activity2, not 1. So how to keep it to its own respective activity after being search?

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

}

Solution

  • Try to change your listener

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