Фильтровать текст и изображение в пользовательском списке с помощью ArrayAdapter - PullRequest
2 голосов
/ 06 июля 2011

Когда я использую ArrayList <ArrayList<String>>, текст фильтруется, но изображения отображаются некорректно (порядок изображений остается прежним).

Когда я использую ArrayList <HashMap<String, String>>, текст плохо фильтруется (фильтруются все элементы).

Что не так с моим кодом? (Пожалуйста, смотрите код для ArrayList <HashMap<String, String>> ниже.)

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;

    }

}

1 Ответ

0 голосов
/ 22 сентября 2011

Ваша функция getview странная ... почему вы сделали другой код в if (view == null) {....

Весь этот раздел используется для перезаписи представления, когда оно входитвам не нужно создавать новое представление.

Но вы используете его для помещения в него пользовательского кода?

Вот как выглядит одна из функций моего адаптера 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);
    }

Затем вы можете добавить свой собственный код.

Это, вероятно, где я бы начал расследование Себастьян.

...