Answer the question
In order to leave comments, you need to log in
How to properly use ViewHolder + LisView?
Good afternoon,
I used the ViewHolder pattern:
public View getView(int position, View convertView, ViewGroup parent) {
switch (type) {
case 0: return item_view_new(data, position, convertView, parent);
case 1: return item_view_new(data, position, convertView, parent);
case 2: return item_view_new(data, position, convertView, parent);
case 3: return item_view_video(data, position);
case 4: return item_view_video(data, position);
case 5: return item_view_video(data, position);
case 6: return item_view_meeting(data, position);
case 10: return item_view_new_error(data, position);
case 99: return item_view_new(data, position, convertView, parent);
}
return null;
}
public View item_view_new(ArrayList<HashMap<Integer, Object>> data, int position,final View convertView, View parent) {
View v = convertView;
ViewHolder viewHolder;
if (v == null) {
viewHolder = new ViewHolder();
v = LayoutInflater.from(context).inflate(R.layout.new_layout_item, (ViewGroup) parent, false);
viewHolder.text_group = (TextView) v.findViewById(R.id.group);
viewHolder.text_date = (TextView) v.findViewById(R.id.date);
viewHolder.title = (TextView) v.findViewById(R.id.title);
viewHolder.text = (TextView) v.findViewById(R.id.content);
viewHolder.author = (TextView) v.findViewById(R.id.author);
viewHolder.addition = (TextView) v.findViewById(R.id.addition);
viewHolder.img = (ImageView) v.findViewById(R.id.image);
viewHolder.ico_group = (ImageView) v.findViewById(R.id.ico_group);
viewHolder.text_group.setTypeface(Typeface.createFromAsset
(context.getAssets(), "Ubuntu-M.ttf"));
viewHolder.text_date.setTypeface(Typeface.createFromAsset
(context.getAssets(), "Ubuntu-N.ttf"));
viewHolder.title.setTypeface(Typeface.createFromAsset
(context.getAssets(), "Ubuntu-M.ttf"));
viewHolder.text.setTypeface(Typeface.createFromAsset
(context.getAssets(), "Ubuntu-L.ttf"));
viewHolder.author.setTypeface(Typeface.createFromAsset
(context.getAssets(), "Ubuntu-N.ttf"));
viewHolder.addition.setTypeface(Typeface.createFromAsset
(context.getAssets(), "Ubuntu-N.ttf"));
viewHolder.text.setTextSize(Integer.parseInt(sharedPref.getString("size_preview", "15")));
viewHolder.pos = position;
v.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) v.getTag();
}
viewHolder.text_group.setText(themetext(data.get(position).get(NAMEGROUP).toString()));
viewHolder.text_date.setText(data.get(position).get(DATE).toString());
viewHolder.title.setText(data.get(position).get(HEADER).toString());
viewHolder.text.setText(ExtraClass.text_clear(data.get(position).get(CONTENT).toString()));
viewHolder.author.setText(data.get(position).get(AUTHOR).toString());
ExtraClass.setIco(data.get(position).get(NAMEGROUP).toString(), viewHolder.ico_group, context);
return v;
}
class ViewHolder {
static TextView text_group;
static TextView text_date;
static TextView title;
static TextView text;
static TextView author;
static TextView addition;
static ImageView img;
static ImageView ico_group;
static int pos;
}
Answer the question
In order to leave comments, you need to log in
v = LayoutInflater.from(context).inflate(R.layout.new_layout_item, (ViewGroup) parent, false);
- тут не нужно parent
кастовать в ViewGroup
class ViewHolder {
static TextView text_group;
static TextView text_date;
static TextView title;
static TextView text;
static TextView author;
static TextView addition;
static ImageView img;
static ImageView ico_group;
static int pos;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question