2013-06-29 3 views
0

J'utilise la classe suivante pour rechercher si dans listView des paysRechercher dans ListView android

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

} 

} 

ce code fonctionne bien et faire une recherche et faire aussi le setOnItemClickListener qui ouvre l'activité qui contient des informations sur le pays sélectionné. Mais quand je fais une recherche et que j'écris "E" par exemple, je trouve la liste modifiée et obtient les pays qui commencent par "E" mais quand j'appuie par exemple sur le deuxième pays qui obtient de la recherche, dans le tableau "countryName". Comment puis-je résoudre ce problème et obtenir les informations du pays sélectionné à partir de la recherche?

J'espère que quelqu'un a eu mes moyens. Merci d'avance.

+0

salut u peut coller votre classe db, j'ai un doute pour clarifier !! plz – ajey

Répondre

0

Je l'ai résolu en remplaçant cette ligne

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

par cette ligne

b.putString(COUNTRY_NAME, adapter.getItem(position));