我是android的新手。我只是想为我的应用程序制作一个简单的搜索功能。我的应用程序包含一个ListView和一个SearchView用于搜索。我的ListView内容使用自定义适配器从字符串列表中列出,该适配器是扩展BaseAdapter
现在,我想要做的是,我想从ListView中搜索任何记录例如,如果我有一些像
这样的记录aa abb ...
所以,当我输入一些像
这样的记录名称时列表视图应该列出从...开始的记录。我已经为此引用了一些内容,我得到了addTextChangedListener但是,我不知道该怎么做?
并且,我们可以通过点击按钮
来实现相同的功能有没有人对此有任何想法?提前致谢
MainActivity.java
public class MainActivity extends ActionBarActivity {
ListView list ;
private SearchView mSearchView;
String [] a = {"aa","abb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll"};
String [] b = {"1","2","3","4","5","6","7","8","9","10","11","12"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSearchView = (SearchView) findViewById(R.id.search_view);
mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
list.clearTextFilter();
} else {
Log.i("WSM",newText);
list.setFilterText(newText.toString());
}
return true;
}
});
list = (ListView) findViewById(R.id.lista);
list.setTextFilterEnabled(true);
list.setAdapter(new MyAdapter(this, a, b));
setupSearchView();
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
class SingleRow{
String ssid;
String pass;
public SingleRow (String ssid, String pass){
this.ssid = ssid;
this.pass = pass;
}
}
class MyAdapter extends BaseAdapter {
ArrayList<SingleRow> myList ;
Context c;
public MyAdapter (Context c, String[] ssid, String [] pass){
this.c = c;
myList = new ArrayList<SingleRow>();
for (int i = 0; i<ssid.length; i++){
myList.add(new SingleRow(ssid[i],pass[i]));
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.singlerow, parent ,false);
TextView TV_ssid = (TextView) row.findViewById(R.id.ssid);
TextView TV_pass = (TextView) row.findViewById(R.id.pass);
SingleRow tmp = myList.get(position);
TV_ssid.setText(tmp.ssid);
TV_pass.setText(tmp.pass);
return row;
}
}
}
singlerow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="183dp"
android:layout_height="wrap_content"
android:layout_weight="0.82"
android:orientation="vertical" >
<TextView
android:paddingTop="10sp"
android:id="@+id/ssid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SSID: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:typeface="monospace" />
<TextView
android:id="@+id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingBottom="10sp"
android:typeface="monospace"/>
</LinearLayout>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="56dp"
android:layout_height="49dp"
android:layout_gravity="center"
android:src="@drawable/abc_ic_menu_moreoverflow_normal_holo_light" />
</LinearLayout>
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<SearchView
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lista"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
答案 0 :(得分:0)
对于搜索功能,我建议您使用此组件:
https://github.com/matessoftwaresolutions/AndroidFilterableList
这使您可以非常简单的方式过滤所需的任何内容。动态更改过滤器等。
您必须修改代码才能使其适应这种工作方式......但我认为这值得。
看看样本!!
我希望这会对你有所帮助。