如何override
动态添加TextView的getView
ArrayAdaptor
方法?
这是我到目前为止所做的:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.numbergenerationitemlayout,
parent, false);
holder.FL = (FlowLayout) view.findViewById(R.id.itemFlowLayout);
holder.FL.setTag(list.get(position));
view.setTag(holder);
GameSet GS = (GameSet) list.get(position);
ArrayList<Element> normal = GS.getNormalNum();
if (normal != null) {
for (int x = 0; x < normal.size(); x++) {
BallTextView BT = new BallTextView(context, "BLUE_BALL");
String text = "" + normal.get(x).getElement();
BT.setText(text);
holder.FL.addView(BT);
}
}
ArrayList<Element> special = GS.getSpecialNum();
if (special != null) {
for (int x = 0; x < special.size(); x++) {
BallTextView BT = new BallTextView(context, "ORANGE_BALL");
String text = "" + special.get(x).getElement();
BT.setText(text);
holder.FL.addView(BT);
}
}
} else {
view = convertView;
// ((ViewHolder) view.getTag()).FL.setTag(list.get(position));
}
ViewHolder currentHolder = (ViewHolder) view.getTag();
currentHolder.position = position;
return view;
}
我现在面临的问题是,当我向数组添加新数据并调用notifyDataSetChanged()
时;在arrayAdaptor
上,新项目未添加。
答案 0 :(得分:0)
我会尝试做
GameSet GS = (GameSet) list.get(position);
以外的
if (convertView == null) {}
例如就在getView()
方法的开头,因为在使用列表一段时间后convertView
可能不再是null
。
答案 1 :(得分:0)
需要更改getView方法,这是解决方案:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.numbergenerationitemlayout,
parent, false);
holder.FL = (FlowLayout) view.findViewById(R.id.itemFlowLayout);
holder.FL.setTag(list.get(position));
view.setTag(holder);
GameSet GS = (GameSet) list.get(position);
ArrayList<Element> normal = GS.getNormalNum();
if (normal != null) {
for (int x = 0; x < normal.size(); x++) {
BallTextView BT = new BallTextView(context, "BLUE_BALL");
holder.FL.addView(BT);
//here only add the view that holds the content.
}
}
ArrayList<Element> special = GS.getSpecialNum();
if (special != null) {
for (int x = 0; x < special.size(); x++) {
BallTextView BT = new BallTextView(context, "ORANGE_BALL");
//here only add the view that holds the content.
holder.FL.addView(BT);
}
}
} else {
view = convertView;
// ((ViewHolder) view.getTag()).FL.setTag(list.get(position));
}
ViewHolder currentHolder = (ViewHolder) view.getTag();
currentHolder.position = position;
//Now do the set text part over here.
return view;
}
基本上只在if(convertView == null)下添加视图内容持有者,然后在条件之外执行setText数据部分。
答案 2 :(得分:0)
@Override
public int getCount() {
if (hasMore) {
return (int) (Math.ceil((mData.length / 4.0))) + 1;
} else {
return (int) (Math.ceil((mData.length / 4.0)));
}
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (position == getCount() - 1 && hasMore) {
convertView = new LinearLayout(context);
ProgressBar progressBar = new ProgressBar(context);
progressBar.setIndeterminateDrawable(context.getResources().getDrawable(R.anim.progress_view_grey_anim));
((LinearLayout) convertView).addView(progressBar);
} else {
}
return convertView;
}
这是我通常添加的进度视图。