为MainActivity制作ListView持有人 [英] Making a ListView holder for MainActivity

查看:117
本文介绍了为MainActivity制作ListView持有人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的listview正在工作以在MainActivity上显示它,并且该布局设置为具有新Person对象的3个TextView属性.

I have my listview working to display it on the MainActivity and the layout is set to have 3 TextView properties of a new Person object.

我以前的代码可以正常工作,但是滚动很慢,所以我考虑使用Holder模式进行优化.但是,当尝试实现此功能时,我在TextView personID=(TextView) view.findViewById(R.id.person_field1)

My previous code was working but the scrolling was slow, so i looked into using the Holder pattern for optimization. However, when trying to implement this i am getting NullPointerException on lines TextView personID=(TextView) view.findViewById(R.id.person_field1)

listView设置为MainActivity上的ListView,并且我需要人员对象在MainActivity上的此视图上显示.

listView is set to a ListView on the MainActivity and i need the person objects to display on this view within the MainActivity.

MainActivity类:

MainActivity class:

public void onButtonClick() 
{
   //put the persons objects on the listView within MainActivity
   listView.setAdapter(new personAdapter(MainActivity.this, R.layout.list_layout, 
                        personList));
}

class personAdapter extends ArrayAdapter<Person>
{
    private LayoutInflater layoutInflator;

    public personAdapter(Context context, int resource, List<Person> p) {
        super(context, resource, p);
        layoutInflator = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        Holder holder = null;
        Person p = getItem(position);

        if (view == null) {
            layoutInflator.inflate(R.layout.list_layout, null);  

            //getting null pointer exceptions here
            TextView personID = (TextView) view.findViewById(R.id.person_field1);
            TextView personFName = (TextView) view.findViewById(R.id.person_field2);
            TextView personLName = (TextView) view.findViewById(R.id.person_field3);

            holder = new Holder(personID, personFName, personLName);

            view.setTag(holder);
        }
        else {
            holder = (Holder) view.getTag();
        }

        holder.stop.setText(person.getID());
        holder.location.setText(person.getFName());
        holder.time.setText(person.getLName()); 
        return view;
    }
}

static class Holder 
{
    public TextView id;
    public TextView FName;
    public TextView LName;

    public Holder(TextView id, TextView FName, TextView LName) {
        this.id = id;
        this.FName = FName;
        this.LName = LName;
    }
}

推荐答案

因为确实存在null 注意检查view == null,然后view从未初始化 试试这个

because there really is null pay attention to check view == null, and then view never initialized try this

view = layoutInflator.inflate(R.layout.list_layout, null);  

这篇关于为MainActivity制作ListView持有人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆