我有一个警告对话框,列出了大约100个城市。有没有办法在其中搜索?或者也许是自动完成?
我想添加一个文本框,根据我输入的内容过滤列表。
编辑:我非常抱歉。我的意思是我想根据我在文本框中输入的内容过滤上面的列表。例如,如果我输入'D',我应该只得到达卡..答案 0 :(得分:7)
好的,我终于设法使用自定义对话框。
citylistview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="200dip"
android:orientation="vertical"
android:gravity="center">
<EditText android:id="@+id/EditBox"
android:maxLines="1"
android:layout_height="wrap_content"
android:layout_width="180dip"
android:gravity="left">
</EditText>
<ListView android:id="@+id/List"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice">
</ListView>
</LinearLayout>
CityListDialog.java
public class CityListDialog extends Dialog implements OnClickListener {
private ListView list;
private EditText filterText = null;
ArrayAdapter<String> adapter = null;
private static final String TAG = "CityList";
public CityListDialog(Context context, String[] cityList) {
super(context);
/** Design the dialog in main.xml file */
setContentView(R.layout.citylistview);
this.setTitle("Select City");
filterText = (EditText) findViewById(R.id.EditBox);
filterText.addTextChangedListener(filterTextWatcher);
list = (ListView) findViewById(R.id.List);
adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, cityList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Log.d(TAG, "Selected Item is = "+list.getItemAtPosition(position));
}
});
}
@Override
public void onClick(View v) {
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
}
};
@Override
public void onStop(){
filterText.removeTextChangedListener(filterTextWatcher);
}}
这是我调用对话框时得到的结果。感谢大家。
答案 1 :(得分:1)
是搜索我们可以使用自动填充
AutoCompleteTextView Auto=new AutoCompleteTextView(this);
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,name);
Auto.setAdapter(arrAdapter);
你确切意思是这个吗?
答案 2 :(得分:1)
如果你假设你有数组中的项目列表需要将它设置为列表,然后使用EditText进行搜索,你可以通过 通过使用EditText addTextChangedListener(this)动作监听器实现三种方法并做你需要的事情
此实现的示例代码如下:
@Override
public void afterTextChanged(Editable s) {
try{
text = textView.getText().toString();
string1 = text.substring(0,1).toUpperCase() + text.substring(1);
}catch(Exception e)
{
Log.v("Text_Hereeeee++++++++++++++++++++++++++++e","NULL"+Integer.toString(a.length));
// wv.setVisibility(View.GONE);
lv.setVisibility(View.GONE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Bru_Maps.this,R.layout.listitem, a);
lv.setAdapter(adapter);
lv.setVisibility(View.VISIBLE);
empty_text=2;
temp_VALUE1=null;
VALUE1=a;
}
if(empty_text==1){
Log.v("Text from ",text);
Set <String> has_set = new TreeSet<String>(search_List);
if(string1!=null)
{
Log.v("string1_for New array",string1);
for(int i=0;i<VALUE1.length;i++)
{
String a = string1;
if(VALUE1[i].startsWith(a))
{
// Log.v("NoDuplicate New array","null");
has_set.add(VALUE1[i]);
}
else{
continue;
}
}
}
Log.v("Text_Her********************e","NULL"+Integer.toString(VALUE1.length));
carArray = new String[has_set.size()];
has_set.toArray(carArray);
temp_VALUE1=carArray;
lv.setVisibility(View.GONE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Bru_Maps.this,R.layout.listitem, temp_VALUE1);
Log.v("Adapter_Get_count",Integer.toString(adapter.getCount()));
lv.setAdapter(adapter);
lv.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
Log.v("beforeTextChanged",s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.v("onTextChanged",s.toString());
empty_text=1;
}
这里我只管理两个数组,一个是原始的,是一个,另一个是在适配器中设置的排序数组temp_VALUE1。
答案 3 :(得分:1)
这些只是想法,但也许你可以使用它们。
穆尔
聚苯乙烯。 如果你的城市在数据库中,你也可以使用游标适配器
<强> UPD:强>
Here is an example也可以在自定义对话框中使用常用布局。因此,您还可以列出一个列表视图。在你的情况下,它会是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/filter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
答案 4 :(得分:1)
@rohith 我以为你很想知道,差不多两年后, 你的答案是帮助人们学习!
我能够让你的解决方案使用光标,虽然它可能不适用于鼻烟。
这里是:
public class SearchableActivity extends Dialog implements OnClickListener
{
CursorAdapter mAdapter;
ListView countryListView;
private EditText filterText = null;
Context context;
public SearchableActivity(Context context)
{
super(context);
setContentView(R.layout.countrysearch);
String[] from;
int[] to;
/*
*dbConnector and cursor
*are close() before entering class
*/
dbConnector.open();
this.setTitle("Pick a Country");
filterText = (EditText) findViewById(R.id.EditBox);
filterText.addTextChangedListener(filterTextWatcher);
filterText.requestFocus();
countryListView = (ListView) findViewById(R.id.List);
cursor = dbConnector.getAllCountries("simpletable");
from = new String[] { "country" };
to = new int[] { android.R.id.text1 };
mAdapter = new SimpleCursorAdapter(context,
android.R.layout.simple_list_item_1, cursor, from, to);
countryListView.setAdapter(mAdapter);
countryListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
cursor.close();
dbConnector.close();
//send the data out
finishSearch(arg3);
}
});
}
@Override
public void onStop(){
filterText.removeTextChangedListener(filterTextWatcher);
super.onStop();
}
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
}
public void doMySearch(String s)
{
// this is a LIKE search on DB
cursor = dbConnector.getSearch(s);
mAdapter.changeCursor(cursor);
}
private TextWatcher filterTextWatcher = new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
}
public void onTextChanged(CharSequence s, int start, int before,
int count)
{
doMySearch(s.toString());
}
};
}
谢谢!