我正在制作一个自定义适配器。我的 list_item本身ListView
有一个LinearLayout
,我ImageView
稍后会在其中添加 s。我正在使用以下代码:
public class Adapter extends ArrayAdapter<Row_Item> {
private java.util.List<Row_Item> List;
private Context context;
public Adapter(List<Row_Item> list, Context ctx) {
super(ctx, R.layout.row_item, list);
this.List = list;
this.context = ctx;
}
public int getCount() {
return List.size();
}
public Row_Item getItem(int position) {
return List.get(position);
}
public long getItemId(int position) {
return List.get(position).hashCode();
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TopicHolder holder = new TopicHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row_item, null);
TextView tv = (TextView) v.findViewById(R.id.textView);
*LinearLayout* l = (LinearLayout) v.findViewById(R.id.myLayout);
holder.topic = tv;
holder.myLayout = l;
v.setTag(holder);
} else
holder = (TopicHolder) v.getTag();
Row_Item p = List.get(position);
holder.topic.setText(p.getName());
ImageView imgUsers;
for (int i = 0; i < some_parameter; i++) { imgUsers = new ImageView(context);
imgUsers.setImageResource(some_image);
holder.myLayout.addView(imgUsers); //add that image to my linearlayout
}
return v;
}
private static class TopicHolder {
public TextView topic;
public LinearLayout myLayout;
// public ImageView notifications;
}
}
所以这里发生的getView()
是被无限调用。另外,我已经onItemClickListener
在调用此适配器的类中进行了设置。由于适配器类没有终止,监听器不工作,应用程序保持静止。
另外,我尝试过类似的操作myLayout.getChildCount()
,如果它大于图像数量,则返回,但无限循环仍然没有终止。