【问题标题】:Filter text and image in a custom listView with ArrayAdapter使用 ArrayAdapter 在自定义 listView 中过滤文本和图像
【发布时间】:2011-07-06 15:12:38
【问题描述】:

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

当我使用ArrayList <HashMap<String, String>> 时,文本没有被很好地过滤(所有元素都被过滤)。

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

列表活动:

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;

    }

}

【问题讨论】:

    标签: android listview filter android-arrayadapter


    【解决方案1】:

    你的 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);
        }
    

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

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

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 2013-06-18
      • 2012-12-18
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 2015-08-06
      • 1970-01-01
      相关资源
      最近更新 更多