1

我正在制作一个自定义适配器。我的 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(),如果它大于图​​像数量,则返回,但无限循环仍然没有终止。

4

2 回答 2

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;
    }
}
于 2013-09-09T13:20:53.233 回答
1

public View getView(int position, View convertView, ViewGroup parent) { View 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 中调试内部循环,以便在线性布局中添加图像。

初始化 some_parameter 值默认为 0。

于 2013-09-09T11:36:11.947 回答