我有一個警報對話框,列出大約100個城市。有沒有辦法在其中搜索?或者也許自動完成?在AlertDialog中搜索
我想添加一個文本框,將根據什麼I型過濾列表。
編輯:我很抱歉。我的意思是我想根據我在文本框中輸入的內容來篩選上面的列表。例如,如果我輸入「D」,我應該只有達卡..
我有一個警報對話框,列出大約100個城市。有沒有辦法在其中搜索?或者也許自動完成?在AlertDialog中搜索
我想添加一個文本框,將根據什麼I型過濾列表。
編輯:我很抱歉。我的意思是我想根據我在文本框中輸入的內容來篩選上面的列表。例如,如果我輸入「D」,我應該只有達卡..
好吧,我最終設法使用自定義對話框。
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);
}}
這是結果我得到當我打電話的對話框。感謝大家。
警報對話框列出大約100個城市?這到底意味着什麼?搜索
是的,我們可以使用自動完成
AutoCompleteTextView Auto=new AutoCompleteTextView(this);
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,name);
Auto.setAdapter(arrAdapter);
您是否嚴格意義呢?
用「列出100個城市的警報對話框」,我的意思是上面的截圖。你如何得到這個想法。當我輸入文本框時,我想相應地過濾列表。 – 2010-10-28 09:46:28
s如果你使用自動完成文本框你將能夠過濾項目...顯示結果在自己的列表視圖。但如果你想你自己的listview動態更改,我認爲MGS的答案將支持你 – Ads 2010-10-28 10:54:59
如果假設u有你需要將其設置成列表,然後使用EditText上進行搜索,您可以通過 使用的EditText addTextChangedListener(這)動作監聽器實現它通過實施三項方法和做陣列的產品清單是什麼u需要
這裏這個implementaion的示例代碼具有如下
@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集。
我不知道,但我不知道這正是我想要的..我不認爲我說得夠清楚:( – 2010-10-28 09:52:12
這些只是想法,但可能是你可以使用它們。
穆爾
詩篇。 如果在DB你的城市,你也可以使用光標適配器
UPD:
Here is an example使用通常的佈局也是自定義對話框。因此你也可以放一個listview。在你的情況下,它會是這樣的:
<?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>
是的,我在想這樣做,但我相對較新,並試圖找出如何把自定義視圖中的列表:)任何想法? – 2010-10-28 10:31:52
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog – Tima 2010-10-28 11:44:20
非常感謝。我得到了:) – 2010-11-01 04:35:42
@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());
}
};
}
謝謝!
很高興聽到這個:)謝謝你的解決方案..總是很好的知道替代品:D – 2012-04-01 15:38:26
@RohithNandkumar這隻適用於你的對象是String的情況。對於我們編寫的其他自定義類對象,還有其他解決方法嗎? – johntheripp3r 2013-11-28 11:32:13