gpt4 book ai didi

java - Android - AutoCompleteTextView 如何将项目附加到始终显示的结果列表?

转载 作者:行者123 更新时间:2023-11-30 11:35:07 25 4
gpt4 key购买 nike

我正在尝试将“无法找到地点单击此处”附加到 AutoCompleteTextView 的结果列表的末尾。我需要它来显示找到的任何结果,以便用户可以手动添加地点

谢谢,

安迪

public class AddVenueActivity extends Activity implements TextWatcher{
AutoCompleteTextView venue_auto_complete;
ArrayList<String> venues = new ArrayList<String>();
ArrayAdapter<String> adapter = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addvenue);

try
{
venue_auto_complete = (AutoCompleteTextView) findViewById(R.id.add_venue_autocomplete);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, venues);
venue_auto_complete.setThreshold(2);
adapter.setNotifyOnChange(true);
venue_auto_complete.addTextChangedListener(this);
venue_auto_complete.setAdapter(adapter);
venue_auto_complete.setCompletionHint("POWPOWPOW");

venue_auto_complete.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
venue_auto_complete.setDropDownHeight(LayoutParams.WRAP_CONTENT);
}
});

venue_auto_complete.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> listView, View arg1, int position, long arg3) {
try
{
String venue = (String)listView.getItemAtPosition(position);
String[] venue_string_array = venue.split(System.getProperty("line.separator"));
venue = venue_string_array[0];
venue_auto_complete.setText(venue);
venue_auto_complete.dismissDropDown();
venue_auto_complete.setDropDownHeight(0);
}
catch(Exception e)
{
Log.v("EROORS", e.toString());
}
}
});
}
catch(Exception e)
{
e.printStackTrace();
Log.v("ERROR", e.toString());
}
}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
if(arg0.length() > 0)
{
search(arg0.toString());
}

}

public void search(String query)
{
try
{
RequestParams params = new RequestParams();
params.put("q", query);
Api.get("/search", params, new JsonHttpResponseHandler()
{
@Override
public void onSuccess(JSONObject json_response)
{
try
{
adapter.clear();

JSONArray venue_searchs = json_response.getJSONArray("response");

for(int i = 0; i < venue_searchs.length(); i++)
{
JSONObject venue_search_result = venue_searchs.getJSONObject(i);
if(venue_search_result.getString("address") != "null")
{
adapter.add(venue_search_result.getString("name") + "\n" + venue_search_result.getString("address"));
}
else
{
adapter.add(venue_search_result.getString("name"));
}
}


// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, venues);
venue_auto_complete.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
catch(Exception e)
{
Log.v("ERROR", e.toString());
}
}
});
}
catch(Exception e)
{
Log.v("error", e.toString());
}
}

}

最佳答案

尝试将 Adapter 包装在 HeaderViewListAdapter 中并尝试用于 AutoCompleteTextView

使用这个构造函数

headeradapter= new HeaderViewListAdapter(ArrayList<ListView.FixedViewInfo> headerViewInfos, ArrayList<ListView.FixedViewInfo> footerViewInfos, adapter);

venue_auto_complete.setAdapter(headeradapter);

关于java - Android - AutoCompleteTextView 如何将项目附加到始终显示的结果列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15277848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com