使用 ArrayAdapter 过滤自定义 listView 中的文本和图像

发布于 2024-11-18 21:17:11 字数 3792 浏览 1 评论 0 原文

当我使用 ArrayList > 时,文本会被过滤,但图像无法正确显示(图像的顺序保持不变)。

当我使用 ArrayList > 时,文本没有得到很好的过滤(所有元素都被过滤)。

我的代码有什么问题吗? (请参阅下面的 ArrayList > 代码。)

ListActivity :

public class myListActivity extends ListActivity{

private ImageInDiscountCardListAdapter adapter;
private EditText filterText;
private ArrayList<HashMap<String, String>> retailerList;
private HashMap<String, String> retailerInfo;
private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s){
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after){
    }

    public void onTextChanged(CharSequence s, int start, int before, int count){
        if (adapter!=null){
            adapter.getFilter().filter(s);
        }
    }

};

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    addImageUrl();

    this.filterText = (EditText) findViewById(R.id.name);
    this.filterText.addTextChangedListener(filterTextWatcher);  

    this.adapter = new ImageInDiscountCardListAdapter(this, retailerList);
    setListAdapter(adapter);

}

@Override
public void onDestroy(){

    adapter.imageLoader.stopThread();
    setListAdapter(null);
    filterText.removeTextChangedListener(filterTextWatcher);
    super.onDestroy();

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id){

    //this.filterText.setText(this.item.get(position));

}

public void addImageUrl(){

    this.retailerList = new ArrayList<HashMap<String,String>>();

    this.retailerInfo = new HashMap<String, String>();
    retailerInfo.put("name", "Fnac");
    retailerInfo.put("imageUrl", "http://android-france.fr/wp-content/uploads/2010/09/FNAC.jpg");
    this.retailerList.add(retailerInfo);

    // etc...

}

适配器 :

public class ImageInDiscountCardListAdapter extends ArrayAdapter<HashMap<String, String>> {

    private Activity activity;
    private ArrayList<HashMap<String, String>> retailerList;
    private static LayoutInflater inflater;
    public ImageLoader imageLoader; 

    public ImageInDiscountCardListAdapter(Activity activity, ArrayList<HashMap<String, String>> retailerList) {

        super(activity, android.R.layout.simple_list_item_1, retailerList);
        this.activity = activity;
        this.retailerList = retailerList;
        inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.imageLoader = new ImageLoader(this.activity.getApplicationContext());

    }

    public static class ViewHolder{
        public TextView text;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent){

        View view = convertView;
        ViewHolder holder;

        if(view == null){
            view = inflater.inflate(R.layout.retailer_row, null);
            holder = new ViewHolder();
            holder.text=(TextView)view.findViewById(R.id.retailer_name);;
            holder.image=(ImageView)view.findViewById(R.id.retailer_image);
            view.setTag(holder);
        }

        else{
            holder=(ViewHolder)view.getTag();
        }

        holder.text.setText(getItem(position).get("name"));
        holder.image.setTag(this.retailerList.get(position).get("imageUrl"));
        imageLoader.DisplayImage(this.retailerList.get(position).get("imageUrl"), activity, holder.image);

        return view;

    }

}

When I use an ArrayList <ArrayList<String>>, the text is filtered, but the images don't appear correctly (the order of the images stays the same).

When I use an ArrayList <HashMap<String, String>>, the text is not well filtered (all elements are filtered).

What is wrong with my code? (Please see code for the ArrayList <HashMap<String, String>> below.)

ListActivity :

public class myListActivity extends ListActivity{

private ImageInDiscountCardListAdapter adapter;
private EditText filterText;
private ArrayList<HashMap<String, String>> retailerList;
private HashMap<String, String> retailerInfo;
private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s){
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after){
    }

    public void onTextChanged(CharSequence s, int start, int before, int count){
        if (adapter!=null){
            adapter.getFilter().filter(s);
        }
    }

};

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    addImageUrl();

    this.filterText = (EditText) findViewById(R.id.name);
    this.filterText.addTextChangedListener(filterTextWatcher);  

    this.adapter = new ImageInDiscountCardListAdapter(this, retailerList);
    setListAdapter(adapter);

}

@Override
public void onDestroy(){

    adapter.imageLoader.stopThread();
    setListAdapter(null);
    filterText.removeTextChangedListener(filterTextWatcher);
    super.onDestroy();

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id){

    //this.filterText.setText(this.item.get(position));

}

public void addImageUrl(){

    this.retailerList = new ArrayList<HashMap<String,String>>();

    this.retailerInfo = new HashMap<String, String>();
    retailerInfo.put("name", "Fnac");
    retailerInfo.put("imageUrl", "http://android-france.fr/wp-content/uploads/2010/09/FNAC.jpg");
    this.retailerList.add(retailerInfo);

    // etc...

}

Adapter :

public class ImageInDiscountCardListAdapter extends ArrayAdapter<HashMap<String, String>> {

    private Activity activity;
    private ArrayList<HashMap<String, String>> retailerList;
    private static LayoutInflater inflater;
    public ImageLoader imageLoader; 

    public ImageInDiscountCardListAdapter(Activity activity, ArrayList<HashMap<String, String>> retailerList) {

        super(activity, android.R.layout.simple_list_item_1, retailerList);
        this.activity = activity;
        this.retailerList = retailerList;
        inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.imageLoader = new ImageLoader(this.activity.getApplicationContext());

    }

    public static class ViewHolder{
        public TextView text;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent){

        View view = convertView;
        ViewHolder holder;

        if(view == null){
            view = inflater.inflate(R.layout.retailer_row, null);
            holder = new ViewHolder();
            holder.text=(TextView)view.findViewById(R.id.retailer_name);;
            holder.image=(ImageView)view.findViewById(R.id.retailer_image);
            view.setTag(holder);
        }

        else{
            holder=(ViewHolder)view.getTag();
        }

        holder.text.setText(getItem(position).get("name"));
        holder.image.setTag(this.retailerList.get(position).get("imageUrl"));
        imageLoader.DisplayImage(this.retailerList.get(position).get("imageUrl"), activity, holder.image);

        return view;

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2024-11-25 21:17:11

你的 getview 函数很奇怪......为什么你在中做了不同的代码
如果(视图==空){
....

整个部分用于在视图进入时回收视图,因此您不需要实例化新视图。

但你用它来放入自定义代码吗?

这就是我的适配器 getview() 函数之一的样子

public View getView(int position, View convertView, ViewGroup parent)
{
    View v = convertView;
    ViewGroup p = parent;
    LayoutInflater inflater;

    // Create a new view only if we cannot recycle the one passed to this function.
    if (v == null)
    {
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.artifact_list_item, p, false);
    }

然后您可以添加自定义代码。

这可能就是我开始调查塞巴斯蒂安的地方。

Your getview function is strange... why did you do different code in
if(view == null){
....

That whole section is used to recycle the view when it comes in so you don't need to instanciate a new view.

But you use it to put custom code in it?

This is what one of my adapter getview() function looks like

public View getView(int position, View convertView, ViewGroup parent)
{
    View v = convertView;
    ViewGroup p = parent;
    LayoutInflater inflater;

    // Create a new view only if we cannot recycle the one passed to this function.
    if (v == null)
    {
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.artifact_list_item, p, false);
    }

Then you can add your custom code.

That is probably where I would start investigating Sebastien.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文