2013-06-29 36 views
0

我使用下面的类来在国家搜索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       
     } 
    }); 

} 

} 

此代码工作良好的ListView所以搜索和做搜索,它也做setOnItemClickListener打开包含有关信息的活动选定的国家。 但是,当我做搜索,我写了“E”例如,我发现列表更改并获得以“E”开头的国家,但当我按例如从搜索获得的第二个国家,它打开国家与第二个索引在“countryName”数组中。 如何解决此问题并从搜索中获取所选国家/地区的信息?

希望任何人都明白我的意思。 在此先感谢。

+0

喜u能粘贴您的DB类替换该行

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

,我有一个疑问,澄清!PLZ – ajey

回答

0

我解决它通过这条线

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