问题描述:

I have an arraylist that needs to be updated using the adpater. I dont have any textviews for it. Here is my code -

public class MyAdapter extends BaseAdapter {

private LayoutInflater inflater = null;

private ArrayList<MyClass> favouriteMessageList;

private MyAdapter(Activity activity, ArrayList list) {

favouriteMessageList = list;

inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override

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

ViewHolder viewHolder;

MyClass favouriteMessage = favouriteMessageList.get(position);

if (convertView == null) {

convertView = inflater.inflate(R.layout.messaging_favorites_layout, null);

viewHolder = new ViewHolder();

convertView.setTag(viewHolder);

}else{

viewHolder = (ViewHolder) convertView.getTag();

}

return convertView;

}

public void setData(ArrayList<MyClass> data) {

favouriteMessageList = data;

notifyDataSetChanged();

}

}

The setData method will receive an arraylist and I need to populate the view with my Arraylist. I am using a viewHolder. How do I achieve this?

网友答案:

Try like this

   public class MyAdapter extends BaseAdapter {

            private LayoutInflater inflater = null;
            private ArrayList<MyClass> favouriteMessageList;

            private MyAdapter(Activity activity, ArrayList list) {
                favouriteMessageList = list;
                inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
                MyClass favouriteMessage =  favouriteMessageList.get(position);

                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.messaging_favorites_layout, null);
                    viewHolder = new ViewHolder();

                    convertView.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                //Update Your Views Here
                return convertView;
            }


           @Override
           public int getCount() {
                if(favouriteMessageList !=null)
                   return favouriteMessageList.size();
                return 0;
            }

           public void setData(ArrayList<MyClass> data) {
                favouriteMessageList = data;
                notifyDataSetChanged();
            }
        }
相关阅读:
Top