Android 通过编辑文本查看可搜索列表
大家好,我想开发一个应用程序,该应用程序在数组列表中有许多项,在列表视图中有一个用于快速使用搜索项的编辑文本,我显示u图像如下:Android 通过编辑文本查看可搜索列表,android,listview,android-edittext,Android,Listview,Android Edittext,大家好,我想开发一个应用程序,该应用程序在数组列表中有许多项,在列表视图中有一个用于快速使用搜索项的编辑文本,我显示u图像如下: 那么,在第页中,您是否检查了Arrayadapter的筛选方法?您需要设置textwatcher,在textchange方法中,您需要调用adapter.filter(“word entered in edittext”)并进行了详细检查?您尝试执行的操作可能通过AutoCompleteTextView实现,因此,您必须通过它更改您的EditText 此外,您需要扩
那么,在第页中,您是否检查了Arrayadapter的筛选方法?您需要设置textwatcher,在textchange方法中,您需要调用adapter.filter(“word entered in edittext”)并进行了详细检查?您尝试执行的操作可能通过
AutoCompleteTextView实现,因此,您必须通过它更改您的EditText
此外,您需要扩展自己的ArrayAdapter
,并在其中扩展Filter
类,并声明扩展Filter类的实例。在过滤器
扩展中,您需要覆盖以下方法:
FilterResults performFiltering(CharSequence)
:在这里您实现了要过滤的方式
void publishResults(CharSequence,final FilterResults)
:您将在此处收到从FilterResults
返回的结果,作为第二个参数。如果有多个项目,只需调用notifyDataSetChanged()
这样你就可以按你想要的方式过滤。可能会找到一个很好的例子。尝试Android中提供的自动完成文本视图。它的编辑文本和Android中的搜索功能 您不需要编写使用自动完成文本视图进行过滤的代码 有关源代码,请参阅示例
您可以询问是否有任何疑问。快乐编码:)您需要将addTextChangedListener添加到EditText。当用户在EditText中输入新数据时,从中获取文本并将其传递给数组适配器筛选器。 您可以这样添加:
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
}
}))
完整示例是尝试以下简单代码:
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SOF_AutoList" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" >
<requestFocus />
</EditText>
</LinearLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Java活动类:
public class SOF_AutoList extends Activity
{
private ListView listview;
private EditText edttxt;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sof__auto_list);
ArrayList<String> data = new ArrayList<String>();
for(int i=0;i<10;i++)
{
if(i==0 || i==1 || i==3)
{
data.add("Apple "+(i+1));
}
else if(i==4 || i==5)
{
data.add("Banana "+(i+1));
}
else
{
data.add("CArrot "+(i+1));
}
}
listview = (ListView)findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,data);
listview.setAdapter(adapter);
edttxt = (EditText)findViewById(R.id.editText1);
edttxt.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
}
公共类软件自动列表扩展活动
{
私有列表视图列表视图;
私有编辑文本;
专用阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u软件\u自动\u列表);
ArrayList数据=新的ArrayList();
对于(int i=0;i我使用此代码通过使用简单的Edittext来生成自动完成的Edittext
公共类MainActivity扩展活动实现OnKeyListener{
EditText e;
String[] st={"apple","abhay","anurag","boll","banana","basic","cat","calm"};
String[] temp={"","","","","",""};
ArrayAdapter<String> ad;
ListView lv;
ViewGroup vg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vg=(ViewGroup)findViewById(R.id.LinearLayout1);
e=(EditText)findViewById(R.id.editText1);
e.setOnKeyListener(this);
ad=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1, temp);
lv=new ListView(getBaseContext());
lv.setAdapter(ad);
lv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
Log.d("", "onKey Entered");
if(KeyEvent.ACTION_UP==arg2.getAction())
{
vg.removeView(lv);
String s=e.getText().toString();
Log.d("", "onKey if Entered"+s);
for(int i=0,j=0;i<st.length;i++,j++)
{
if(st[i].startsWith(s))
temp[j]=st[i];
else
--j;
}
vg.addView(lv);
}
return false;
edite;
字符串[]st={“苹果”、“阿比”、“阿努拉格”、“波尔”、“香蕉”、“基本”、“猫”、“平静”};
字符串[]temp={“”、“”、“”、“”、“”、“”、“”};
ArrayAdapter广告;
ListView lv;
视图组vg;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vg=(视图组)findViewById(R.id.LinearLayout1);
e=(EditText)findViewById(R.id.editText1);
e、 setOnKeyListener(此);
ad=新的ArrayAdapter(getBaseContext(),android.R.layout.simple\u list\u item\u 1,temp);
lv=新的ListView(getBaseContext());
低压设置适配器(ad);
lv.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
}
@凌驾
公共布尔onKey(视图arg0、int arg1、KeyEvent arg2){
//TODO自动生成的方法存根
Log.d(“,”输入onKey“);
if(KeyEvent.ACTION\u UP==arg2.getAction())
{
vg.移除视图(lv);
字符串s=e.getText().toString();
Log.d(“,”如果输入“+s”,则为“ON键”);
对于(inti=0,j=0;i我的代码在这里
ArrayList<Integer> ids=new ArrayList<Integer>();
ArrayList<String> ar=new ArrayList<String>();
ArrayAdapter adpter;
private ListView list;
private EditText search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search=(EditText)findViewById(R.id.tvsearch);
ActionBar ab=getActionBar();
list =(ListView)findViewById(R.id.list);
handler h=new handler(MainActivity.this);
ArrayList<property> p = h.display();
StringBuffer sb;
for(property p1 : p)
{
sb=new StringBuffer();
int id=p1.getId();
String State_Name = p1.getState_name();
String State_Web= p1.getState_web();
ar.add(State_Name);
ids.add(id);
}
h.close();
adpter=new ArrayAdapter(MainActivity.this, android.R.layout.simple_selectable_list_item,ar);
list.setAdapter(adpter);
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
MainActivity.this.adpter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
// list.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_selectable_list_item,ar));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
int did=ids.get(pos);
Intent i=new Intent(MainActivity.this,website.class);
i.putExtra("did",did);
startActivity(i);
}
});
}
ArrayList id=new ArrayList();
ArrayList ar=新的ArrayList();
阵列适配器;
私有列表视图列表;
私人编辑文本搜索;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search=(EditText)findViewById(R.id.tvsearch);
ActionBar ab=getActionBar();
list=(ListView)findViewById(R.id.list);
handler h=新的处理程序(MainActivity.this);
ArrayList p=h.display();
对某人施加压力;
对于(属性p1:p)
{
sb=新的StringBuffer();
int id=p1.getId();
字符串State_Name=p1.getState_Name();
字符串State_Web=p1.getState_Web();
ar.add(州名称);
添加(id);
}
h、 close();
adpter=newArrayAdapter(MainActivity.this,android.R.layout.simple\u可选\u列表\u项,ar);
列表.设置适配器(adpter);
search.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
MainActivity.this.adpter.getFilter().filter;
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
}
});
//setAdapter(新的ArrayAdapter(MainActivity.this,android.R.layout.simple_可选_列表_项,ar));
list.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、int pos、,
长id)
{
int did=id.get(pos);
意向i=新意向(MainActivity.this,website.class);
i、 putExtra(“did”,did);
星触觉(i);
}
});
}
您是否尝试实现它。请发布您的代码??我有一个列表视图和一个编辑文本列表视图从数组列表中获取值,数组中有许多项,我想让自己成为G类型