我想在listview中创建搜索功能,我有一个小小的疑问,即使用arraylist我们可以执行搜索功能。这里我已经附上了代码..其他
Totalarraylist是arraylist,从那个arraylist我将数据传递给Question适配器类 代码,,
searchbox.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
If so then what to add here for search functionality...
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
adapterfirst = new Questionadapter(MainActivity.this,Totalarraylist);
listviewfirst = (ListView) findViewById(R.id.listquestion);
listviewfirst.setAdapter(adapterfirst);
}
public class Questionadapter extends BaseAdapter {
private ArrayList<HashMap<String, Object>> data;
public LayoutInflater inflater;
Context context;
public Questionadapter(Context context, ArrayList<HashMap<String,Object>> Totalarraylists) {
// TODO Auto-generated constructor stub
data=Totalarraylists;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
System.out.println("data="+data);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
//
}
}
任何人都可以帮我这么做..
答案 0 :(得分:0)
为ListAdapter
添加过滤器,并在onTextChanged
上将过滤器应用于listadapter
答案 1 :(得分:0)
试试这个,将你的数组列表与你的字符串进行比较,你会得到答案
public class MainActivity extends Activity {
ArrayList<String> list = new ArrayList<String>();
private EditText searchEdt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// making it full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_ugsimply_beta);
list.add("Android");
list.add("Android1");
list.add("blackberry");
list.add("samsung");
list.add("LG");
searchEdt = (EditText) findViewById(R.id.searchEdt);
searchEdt.addTextChangedListener(new TextWatcher() {
private int textlength;
private ArrayList<String> arrayList_sort = new ArrayList<String>();
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = searchEdt.getText().length();
if (arrayList_sort != null)
arrayList_sort.clear();
for (int i = 0; i < list.size(); i++) {
if (textlength <= list.get(i).toString().length()) {
if (searchEdt
.getText()
.toString()
.equalsIgnoreCase(
(String) list.get(i).toString()
.subSequence(0, textlength))) {
arrayList_sort.add(list.get(i));
Log.d("TAG", "log" + arrayList_sort.size());
}
}
}
}
});
}
}