本文介绍了Android的ListView控件与自定义适配器搜索不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在ndroid的ListView应用搜索,但它不工作对我来说,这里是code我都试过了。
最后的ArrayList<用户> UDATA =新的ArrayList<用户>();
ListView控件=(ListView控件)findViewById(R.id.listView1);
listView.setAdapter(新CustomListAdapter(这一点,UDATA));searchBox.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){
//当用户更改文本
((可筛选)ViewInfo.this.listView.getAdapter())用getFilter()过滤器(CS)。
} @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
}
});
下面是用户级
公共类用户实现Serializable
{
私人字符串名称;
私人字符串日期;
私人字符串电子邮件;
私人字符串图像;
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
公共字符串GETDATE(){
归期;
}
公共无效的setDate(字符串日期){
this.date =日期;
}
公共字符串getEmail(){
返回的电子邮件;
}
公共无效setEmail(字符串email){
this.email =电子邮件;
}
公共字符串的getImage(){
返回形象;
}
公共无效setimage(字符串图像){
this.image =图像;
}
和这里是CustomListAdapter
公共类CustomListAdapter延伸BaseAdapter { 私人的ArrayList<用户>的ListData;
私人LayoutInflater layoutInflater;
用户ACT =新用户(); 公共CustomListAdapter(上下文的背景下,ArrayList的<用户>的ListData){
this.listData =的ListData;
layoutInflater = LayoutInflater.from(上下文);
} @覆盖
公众诠释的getCount(){
返回listData.size();
} @覆盖
公共对象的getItem(INT位置){
返回listData.get(位置);
} @覆盖
众长getItemId(INT位置){
返回的位置;
} 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewHolder持有人;
如果(convertView == NULL){
convertView = layoutInflater.inflate(R.layout.list_row,NULL);
持有人=新ViewHolder();
holder.picView =(ImageView的)convertView.findViewById(R.id.pic);
holder.dateView =(TextView中)convertView.findViewById(R.id.date);
holder.nameView =(TextView中)convertView.findViewById(R.id.name);
holder.emailView =(TextView中)convertView.findViewById(R.id.email); convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
} 用户行为= listData.get(位置); holder.picView.setImageBitmap(德codeFILE(act.getimage()));
holder.dateView.setText(act.getDate());
holder.nameView.setText(act.getName());
holder.emailView.setText(act.getEmail());
返回convertView;
} 静态类ViewHolder {
ImageView的picView; TextView的dateView;
TextView的emailView;
TextView的nameView; }
}
和这里是LogCat中说的话
15 04-28:55:52.677:E / AndroidRuntime(25953):致命异常:主要
04-28 15:55:52.677:E / AndroidRuntime(25953):java.lang.ClassCastException:com.assignment1.reginfo.CustomListAdapter不能转换为android.widget.Filterable
04-28 15:55:52.677:E / AndroidRuntime(25953):在com.assignment1.reginfo.ViewInfo $ 4.onTextChanged(ViewInfo.java:186)
04-28 15:55:52.677:E / AndroidRuntime(25953):在android.widget.TextView.sendOnTextChanged(TextView.java:7518)
04-28 15:55:52.677:E / AndroidRuntime(25953):在android.widget.TextView.handleTextChanged(TextView.java:7580)
04-28 15:55:52.677:E / AndroidRuntime(25953):在android.widget.TextView $ ChangeWatcher.onTextChanged(TextView.java:9329)
04-28 15:55:52.677:E / AndroidRuntime(25953):在android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
04-28 15:55:52.677:E / AndroidRuntime(25953):在android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
解决方案
您所使用的适配器并不适用于过滤器。它仅适用于ArrayAdapter。因此,无论您使用ArrayAdapter或者自己写code数据的滤网过滤。
I am trying to apply search on ndroid ListView but its not working for me, here is the code i have tried.
final ArrayList<Users> uData = new ArrayList<Users>();
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new CustomListAdapter(this, uData));
searchBox.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
((Filterable) ViewInfo.this.listView.getAdapter()).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
}
});
Here is the Users class
public class Users implements Serializable
{
private String name;
private String date;
private String email;
private String image;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getimage() {
return image;
}
public void setimage(String image) {
this.image = image;
}
and here is the CustomListAdapter
public class CustomListAdapter extends BaseAdapter {
private ArrayList<Users> listData;
private LayoutInflater layoutInflater;
Users Act = new Users();
public CustomListAdapter(Context context, ArrayList<Users> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.picView = (ImageView) convertView.findViewById(R.id.pic);
holder.dateView = (TextView) convertView.findViewById(R.id.date);
holder.nameView = (TextView) convertView.findViewById(R.id.name);
holder.emailView = (TextView) convertView.findViewById(R.id.email);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Users act = listData.get(position);
holder.picView.setImageBitmap(decodeFile(act.getimage()));
holder.dateView.setText(act.getDate());
holder.nameView.setText(act.getName());
holder.emailView.setText(act.getEmail());
return convertView;
}
static class ViewHolder {
ImageView picView;
TextView dateView;
TextView emailView;
TextView nameView;
}
}
and here is what LogCat says
04-28 15:55:52.677: E/AndroidRuntime(25953): FATAL EXCEPTION: main
04-28 15:55:52.677: E/AndroidRuntime(25953): java.lang.ClassCastException: com.assignment1.reginfo.CustomListAdapter cannot be cast to android.widget.Filterable
04-28 15:55:52.677: E/AndroidRuntime(25953): at com.assignment1.reginfo.ViewInfo$4.onTextChanged(ViewInfo.java:186)
04-28 15:55:52.677: E/AndroidRuntime(25953): at android.widget.TextView.sendOnTextChanged(TextView.java:7518)
04-28 15:55:52.677: E/AndroidRuntime(25953): at android.widget.TextView.handleTextChanged(TextView.java:7580)
04-28 15:55:52.677: E/AndroidRuntime(25953): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9329)
04-28 15:55:52.677: E/AndroidRuntime(25953): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
04-28 15:55:52.677: E/AndroidRuntime(25953): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
解决方案
The Adapter you are using not works with Filter. It only works with ArrayAdapter. So either you use ArrayAdapter or write your own code for filteration of data.
这篇关于Android的ListView控件与自定义适配器搜索不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!