使用数据模型创建的ListView的交互行

时间:2012-01-23 10:31:31

标签: android listview model adapter interactive

我一直在关注this教程,一切都很顺利,直到我决定自己实现一些东西。 我有以下适配器:

package alphabet.fast.scroll;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class interactiveArrayAdapter extends ArrayAdapter<model> {

    private final List<model> list;
    private final Activity context;

    public interactiveArrayAdapter(Activity context, List<model> list) {
        super(context, R.layout.custom_row, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected ImageView avatar;
        protected TextView name;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.custom_row, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.avatar = (ImageView) view.findViewById(R.id.avatar);
            viewHolder.name = (TextView) view.findViewById(R.id.name);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox);
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    model element = (model) viewHolder.checkbox.getTag();
                    element.setSelected(buttonView.isChecked());
                }
            });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.avatar.setImageResource(R.drawable.icon2);
        holder.name.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}

这个模型:

package alphabet.fast.scroll;

import android.graphics.drawable.Drawable;

public class model {

    private Drawable avatar;
    private String name;
    private boolean selected;

    public model(String name) {
        this.name = name;
        selected = false;
    }

    public model(String name, Drawable avatar) {
        this.avatar = avatar; 
        this.name = name;
        selected = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

和MainActivity:

package alphabet.fast.scroll;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class tutorial extends ListActivity {

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        ArrayAdapter<model> adapter = new interactiveArrayAdapter(this, getModel());
        setListAdapter(adapter);
    }

        private List<model> getModel() {
            List<model> list = new ArrayList<model>();
            list.add(get("Linux"));
            list.add(get("Windows7"));
            list.add(get("Android"));
            list.add(get("iPhone"));
            list.add(get2("Problem coming up here", getResources().getDrawable(R.drawable.icon)));
            return list;
        }

        private model get(String s) {
            return new model(s);
        }

        private model get2(String s, Drawable av) {
            return new model(s, av);
        }
}

你能否告诉我我的代码有什么问题?为什么我的构造函数有文本和drawable无法正常工作 - 我没有得到ListView中avatar行的更新Problem coming up here

1 个答案:

答案 0 :(得分:1)

而不是使用您正在执行的头像图像更新imageView holder.avatar.setImageResource(R.drawable.icon2);
相反,您应该在Avatar类中创建一个方法
public Drwable getAvatarImage(){
return this.avatar;
}

Then you should do as below in your getView method:
holder.avatar.setImageResource(list.get(position).getAvatarImage);