2012-07-18 279 views
0

我有一个按钮(SET按钮),并立即在按下按钮,我希望我的修改AutoCompleteTextView显示在下拉菜单中AutoCompleteTextView不显示下拉

我有两个类

AutoCompleteTextViewTest1Activity.class

package com.autocompletetextviewtest1; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 


public class AutoCompleteTextViewTest1Activity extends Activity { 
ArrayAdapter<String> adapter1; 
ArrayAdapter<String> adapter2; 
private InstantAutoComplete actv; 
private String[] countries2 ={"Taiwan", "China", "S. Korea", "USA", "Japan", "Russia"}; 
private String[] countries={}; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countries); 
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countries2); 
actv = (InstantAutoComplete)findViewById(R.id.actv); 
actv.setAdapter(adapter1); 

Button setButton = (Button)findViewById(R.id.setButton); 
Button clearButton = (Button)findViewById(R.id.clearButton); 

setButton.setOnClickListener(new OnClickListener(){ 
public void onClick(View v){ 
actv.setAdapter(adapter2); 
actv.requestFocus(); 
} 
}); 

clearButton.setOnClickListener(new OnClickListener(){ 
public void onClick(View v){ 
actv.setAdapter(adapter1); 
     } 
}); 
} 
} 

而且

InstantAutoComplete.class

package com.autocompletetextviewtest1; 

import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.widget.AutoCompleteTextView; 

public class InstantAutoComplete extends AutoCompleteTextView { 

public InstantAutoComplete(Context context) { 
super(context); 
} 

public InstantAutoComplete(Context arg0, AttributeSet arg1) { 
super(arg0, arg1); 
} 

public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) { 
super(arg0, arg1, arg2); 
} 

@Override 
public boolean enoughToFilter() { 
return true; 
} 

@Override 
protected void onFocusChanged(boolean focused, int direction, 
Rect previouslyFocusedRect) { 
super.onFocusChanged(focused, direction, previouslyFocusedRect); 
if (focused) { 
performFiltering(getText(), 0); 
showDropDown(); 

} 
} 

} 

但我的AutoCompleteTextView不显示按下setButton的下拉菜单。我能做什么?谢谢!

+0

我做类似的东西在这里选择一个项目! http://stackoverflow.com/questions/12854336/autocompletetextview-backed-by-cursorloader – toobsco42 2012-10-29 20:03:02

回答

0

尝试调用setButton中的actv.invalidate()onClick以刷新具有新更新的适配器的视图。

0

这是为我工作。请注意,我在适当的地方有日志来检查我的webservice调用和它的返回值。这对自动完成的工作至关重要。在你的情况下,你可能需要删除web服务调用,并用硬编码值自己填充适配器。

初始化的onCreate

fromAutoComplete = new AutoComplete(this, R.layout.autocomplete_list_item); 
fromAutoComplete.setNotifyOnChange(true); 
fromAddress = (AutoCompleteTextView) findViewById(R.id.fromAddress); 
fromAddress.setAdapter(fromAutoComplete); 
fromAddress.setOnItemClickListener(this); 

自动完成适配器,检查你的角色,并一遍又一遍地调用Web服务,在下拉菜单中显示了下来。

请注意,

  1. 这个API需要一个谷歌的API密钥,您需要为您自己。另外不要忘记为您的密钥启用位置API。没有这个,不会工作。
  2. 将变量放置在web服务调用中(出于某种疯狂的原因)。最后的传感器变量为我工作。随意在你的logcat的fiddler或chrome上测试它。
public AutoComplete(Context context, int textViewResourceId) { 
    super(context, textViewResourceId); 
} 

@Override 
public int getCount() { 
    return resultList.size(); 
} 

@Override 
public String getItem(int index) { 
    return resultList.get(index); 
} 

@Override 
public Filter getFilter() { 
    Filter filter = new Filter() { 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults filterResults = new FilterResults(); 
      if (constraint != null) { 
       // Retrieve the autocomplete results. 
       resultList = autocomplete(constraint.toString()); 

       // Assign the data to the FilterResults 
       filterResults.values = resultList; 
       filterResults.count = resultList.size(); 
      } 
      return filterResults; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      if (results != null && results.count > 0) { 
       notifyDataSetChanged(); 
      } else { 
       notifyDataSetInvalidated(); 
      } 
     } 
    }; 
    return filter; 
} 

private ArrayList<String> autocomplete(String input) { 
    ArrayList<String> resultList = null; 

    HttpURLConnection conn = null; 
    StringBuilder jsonResults = new StringBuilder(); 
    try { 
     StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON); 
     sb.append("?key=" + API_KEY+"&sensor=false"); 
     sb.append("&components=country:in"); 
     sb.append("&input=" + URLEncoder.encode(input, "utf8")); 
     Log.d(LOG_TAG, "Calling "+sb.toString()) ; 

     URL url = new URL(sb.toString()); 
     conn = (HttpURLConnection) url.openConnection(); 
     InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

     // Load the results into a StringBuilder 
     int read; 
     char[] buff = new char[1024]; 
     while ((read = in.read(buff)) != -1) { 
      jsonResults.append(buff, 0, read); 
     } 
    } catch (MalformedURLException e) { 
     Log.e(LOG_TAG, "Error processing Places API URL", e); 
     return resultList; 
    } catch (IOException e) { 
     Log.e(LOG_TAG, "Error connecting to Places API", e); 
     return resultList; 
    } finally { 
     if (conn != null) { 
      conn.disconnect(); 
     } 
    } 

    try { 
     // Create a JSON object hierarchy from the results 
     JSONObject jsonObj = new JSONObject(jsonResults.toString()); 
     JSONArray predsJsonArray = jsonObj.getJSONArray("predictions"); 

     // Extract the Place descriptions from the results 
     resultList = new ArrayList<String>(predsJsonArray.length()); 
     for (int i = 0; i < predsJsonArray.length(); i++) { 
      resultList.add(predsJsonArray.getJSONObject(i).getString("description")); 
     } 
    } catch (JSONException e) { 
     Log.e(LOG_TAG, "Cannot process JSON results", e); 
    } 

    Log.d(LOG_TAG, resultList.size() + " in total returned.") ; 

    return resultList; 
} 

在从下拉文本

@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
    Double latitude, longitude; 
    ArrayList<String> resultList = null; 
    HttpURLConnection conn = null; 
    StringBuilder jsonResults = new StringBuilder(); 
    String str = (String) adapterView.getItemAtPosition(position); 
    try { 
     StringBuilder sb = new StringBuilder(
       "http://maps.googleapis.com/maps/api/geocode/json?" + "address=" 
         + URLEncoder.encode(str, "utf-8") + "&sensor=true"); 

     URL url = new URL(sb.toString()); 
     Log.d("Taxeeta", url.toString() + " Called"); 
     conn = (HttpURLConnection) url.openConnection(); 
     InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

     // Load the results into a StringBuilder 
     int read; 
     char[] buff = new char[1024]; 
     while ((read = in.read(buff)) != -1) { 
      jsonResults.append(buff, 0, read); 
     } 
    } catch (MalformedURLException e) { 
     Log.e("Taxeeta", "Error processing Places API URL", e); 
    } catch (IOException e) { 
     Log.e("Taxeeta", "Error connecting to Places API", e); 
    } finally { 
     if (conn != null) { 
      conn.disconnect(); 
     } 
    } 

    try { 
     // Create a JSON object hierarchy from the results 
     JSONObject jsonObj = new JSONObject(jsonResults.toString()); 
     JSONObject location = ((JSONObject) jsonObj.getJSONArray("results").get(0)) 
       .getJSONObject("geometry").getJSONObject("location"); 
     latitude = location.optDouble("lat"); 
     longitude = location.optDouble("lng"); 
     Log.d("Taxeeta", "Received Latitude " + latitude + ": Longitude" + longitude); 
     if (view.getId() == fromAddress.getId()) { 
      source = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6)); 
      Log.d("Taxeeta", "Source Done"); 
     } else { 
      destination = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6)); 
      Log.d("Taxeeta", "Destination Done"); 
     } 
    } catch (JSONException e) { 
     Log.e("Taxeeta", "Cannot process JSON results", e); 
    } 
}