使用 arrayadapter 来 filter 一个 listview

问答小助手 2013-03-14 01:45:43
加精
原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/1679

问题描述:

我使用 arrayadapter 来 filter 一个 listview。Arraydapter 的参数是一个String[][]。但是没有成功,必须要重写 Filter interface吗?要是这样的话,如何重写呢?
filter 数组的每一个位置:

galleryValues[0][0] -> "tiulo" [0][1] -> "desc" [0][2] -> "etc"
lstContinente = (ListView)findViewById(R.id.list);
lstContinente.setTextFilterEnabled(true);
adapter = new PortaitArrayAdapter(cx,galleryValues);
lstContinente.setAdapter(adapter);

ed_busqueda.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
adapter.notifyDataSetChanged();
}
});



The adapter code:

public class PortaitArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[][] values;
private List<Imagen> imagenes = null;
private LayoutInflater mInflater;
public ImageLoader imageLoader;
public PortaitArrayAdapter(Context context, String[][] values) {
super(context, R.layout.gallery_row);
this.context = context;
this.values = values;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imagenes = new ArrayList<Imagen>();
for (int i = 0; i < 20; i++) imagenes.add(new Imagen());
Bitmap def = BitmapFactory.decodeResource(this.context.getResources(),R.drawable.ic_launcher);
imageLoader=new ImageLoader(this.context,def, imagenes);
}
@Override
public int getCount (){
return this.values.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.gallery_row, parent, false);
holder.txtTitulo = (TextView) convertView.findViewById(R.id.txt_gallery_titulo);
holder.txtDesc = (TextView) convertView.findViewById(R.id.txt_gallery_desc);
holder.txtFecha = (TextView) convertView.findViewById(R.id.txt_gallery_fecha);
holder.txtEst = (TextView) convertView.findViewById(R.id.txt_gallery_est);
holder.imageView = (ImageView)convertView.findViewById(R.id.lst_img_gallery);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
/*LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.gallery_row, parent, false);*/

//ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
Bitmap bmp;
Log.v("Position --> ",String.valueOf(position));
try {
byte b[] = imagenes.get(position).getImageData();
if (b != null) {
bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
if (bmp != null) holder.imageView.setImageBitmap(bmp);
} else {
String urlBase = galleryValues[position][0].substring(0, galleryValues[position][0].lastIndexOf("/")+1);
String urlToEncode = galleryValues[position][0].substring(galleryValues[position][0].lastIndexOf("/")+1, galleryValues[position][0].length());
urlToEncode = URLEncoder.encode(urlToEncode,"UTF-8");
String url = urlBase.concat(urlToEncode);
url = url.replace("+", "%20");
Log.v("UrlFinal --> ",url);
imageLoader.DisplayImage(String.valueOf(position),url,act,holder.imageView, position,null);
}
} catch (Exception e) {
Log.e(this.getClass().getName(),"Exception en pos = " + position + " error:" + e.getMessage());
e.printStackTrace();
}
holder.txtTitulo.setText(galleryValues[position][1] + ", " + galleryValues[position][2]);
String[] dates = galleryValues[position][4].split("/");
String date = dates [1] + "/" + dates[0] + "/" + dates[2];
Date d1 = new Date(date);
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
holder.txtDesc.setText(galleryValues[position][3]);
holder.txtFecha.setText(df.format(d1));
holder.txtEst.setText(getText(R.string.num_fotos_gallery) + galleryValues[position][5] + " - " + getText(R.string.num_videos_gallery) + galleryValues[position][6] + " - " + getText(R.string.num_exp_gallery) + galleryValues[position][7]);
return convertView;
}
}
private static class ViewHolder {
TextView txtTitulo;
TextView txtDesc;
TextView txtFecha;
TextView txtEst;
ImageView imageView;
}



...全文
2196 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010003433 2013-03-23
  • 打赏
  • 举报
回复
太高科技了,这个我正在学习中
happyhq 2013-03-22
  • 打赏
  • 举报
回复
楼主厉害 分数也不少啊
jieg3000 2013-03-20
  • 打赏
  • 举报
回复
very good!!!!
zhzhfan 2013-03-19
  • 打赏
  • 举报
回复
哇哇哇,楼主好厉害,有一种不明觉厉的感觉!!!
zh20134004 2013-03-19
  • 打赏
  • 举报
回复
感谢!!!太好啦
gtr_skyline 2013-03-18
  • 打赏
  • 举报
回复
太厉害了佩服
yyydmg 2013-03-18
  • 打赏
  • 举报
回复
nb
dangyangzhimei 2013-03-18
  • 打赏
  • 举报
回复
嗯,支持
gbyeleary 2013-03-18
  • 打赏
  • 举报
回复
vb fsa
star123liu 2013-03-18
  • 打赏
  • 举报
回复
楼上不错哦。
quan1203· 2013-03-18
  • 打赏
  • 举报
回复
学习学习学习
zyp898989 2013-03-17
  • 打赏
  • 举报
回复
yiyufan 2013-03-16
  • 打赏
  • 举报
回复
那你
反过来 2013-03-15
  • 打赏
  • 举报
回复
太牛b了。崇拜
dasen2008 2013-03-15
  • 打赏
  • 举报
回复
感谢楼主分享
ftjavayp 2013-03-15
  • 打赏
  • 举报
回复
nettman 2013-03-15
  • 打赏
  • 举报
回复
E_RIC_Liu 2013-03-15
  • 打赏
  • 举报
回复
silencexh 2013-03-15
  • 打赏
  • 举报
回复
没怎么看懂,加点注释吧。
程序员一灯 2013-03-15
  • 打赏
  • 举报
回复
加载更多回复(13)

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧