我正在制作自定义适配器。我ListView
的list_item本身就有一个LinearLayout
,我稍后会在其中添加ImageView
。
我正在使用以下代码:
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()
之类的东西,如果它大于图像数量则会返回但是无限循环仍然没有终止。
答案 0 :(得分:1)
public View getView(int position,View convertView,ViewGroup parent) { 查看v = convertView;
TopicHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(R.layout.row_item, null);
holder = new TopicHolder();
holder.topic = (TextView) v.findViewById(R.id.textView);
holder.myLayout = (LinearLayout) v.findViewById(R.id.myLayout);
v.setTag(holder);
} else {
holder = (TopicHolder) v.getTag();
}
Row_Item p = List.get(position);
holder.topic.setText(p.getName());
ImageView imgUsers;
Log.e("some_param",some_parameter+"");
for (int i = 0; i < some_parameter; i++) {
imgUsers = new ImageView(context);
Log.e("some_param",some_image+" "+i);
imgUsers.setImageResource(some_image);
holder.myLayout.addView(imgUsers);
}
return v;
}
如果仍然无法使用getview,请在getview中调试innerloop,以便在linearlayout中添加图片。
初始化some_parameter值默认为0.
答案 1 :(得分:1)
我得到了解决方案。我在适配器本身设置了onClickListener,它工作正常。
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
OnClickListener click= new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do something (which I wasn't able to do earlier)
}
};
v.setOnClickListener(click);
}
return v;
}
private static class TopicHolder {
public TextView topic;
public LinearLayout myLayout;
// public ImageView notifications;
}
}