Question

I'm using the following class to so search in listView of countries

package com.androidhive.androidlistviewwithsearch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {

private ListView lv;
SimpleCursorAdapter cursorAdapter;
ArrayAdapter<String> adapter;
EditText inputSearch;
private SQLiteAdapter mySQLiteAdapter;
ArrayList<HashMap<String, String>> productList;
public static final String COUNTRY_NAME = "COUNTRY_NAME";
String[] countryName;

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

    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToWrite();

    int isEmpty = mySQLiteAdapter.isEmpty();
    if (isEmpty == 0) {

        mySQLiteAdapter.insert("Afghanistan", "102", "119", "119");
        mySQLiteAdapter.insert("Albania", "127", "128", "129");
        mySQLiteAdapter.insert("Algeria", "14", "14", "17");            
        mySQLiteAdapter.insert("American Samoa", "911", "911", "911");
        mySQLiteAdapter.insert("Andorra", "118", "118", "110");
        mySQLiteAdapter.insert("Angola", "118", "118", "110");
        mySQLiteAdapter.insert("Panama", "911", "911", "911");
        mySQLiteAdapter.insert("Papua New Guinea /Port Moresby", "", "110", "000");
        mySQLiteAdapter.insert("Yemen", "191",  "191", "194");
        mySQLiteAdapter.insert("Zambia", "991/112", "993/112",  "999/112");
        mySQLiteAdapter.insert("Zimbabwe",  "994/999",  "993/999",  "995/999");

    }

    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToRead();
    Cursor cursor = mySQLiteAdapter.queueAll();
    startManagingCursor(cursor);

    List<String> list = new ArrayList<String>();

    if (cursor.moveToFirst()) {

        while (cursor.isAfterLast() == false) {

            String countries = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.COUNTRIES_CONTENT)).toString().trim();
            System.out.println("countries: " + countries);

            list.add(countries);

            cursor.moveToNext();
        }

    }


    countryName = new String[list.size()];
    countryName = list.toArray(countryName);

    mySQLiteAdapter.close();

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

    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, countryName);

    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {

            Intent intent ;
            Bundle b = new Bundle();                

            b.putString(COUNTRY_NAME, countryName[position]);

            intent = new Intent(getApplicationContext(), CountryNumbers.class);
            intent.putExtras(b);
            startActivity(intent);
            finish();

        }
    });


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

}

}

this code works well and do a search and it also do the setOnItemClickListener that opens the Activity that contains info about the selected country. But when I do search and I wrote "E" for example, I found the list changed and gets countries that starts with "E" but When I press for example the second country that gets from search, it opens the country with second index in "countryName" array. How can I solve this issue and get the info of the selected country from the search?

Hope anyone got my mean. Thanks in advance.

Was it helpful?

Solution

I solved it by replacing this line

 b.putString(COUNTRY_NAME, countryName[position]);

by this line

b.putString(COUNTRY_NAME, adapter.getItem(position));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top