从现有视图创建和新视图

时间:2011-12-11 17:09:40

标签: java android android-view

我一直在开发一个在列表活动中显示供稿的应用。 首先,我获取信息并解析它,并将其放入我创建的“FeedType”数组中,该数组有3个字段:Bitmap pic,String name和View content。 内容代表Feed的内容,并且有不同类型的内容。 此数组正在解析过程中初始化。

我在xml文件中创建了feed布局。 使用addView()创建了一个包含“内容”的ImageView,TextView和LinearLayout。 我创建了一个包装器并创建了我的ListAdapter以重用列表中的对象。

当我尝试使用添加已经是其他LinearLayout的子节点的视图时,我的问题开始了,并且它正在发生,因为当我从数组中检索内容时,我正在退出对现有视图的引用,而不是一种新观点。

有没有办法从现有视图创建新视图?还是有人得到了其他解决方案?

代码:

public class FeedType {

    private Bitmap profilePicSrc;
    private String name;
    private View feedContent;
    private Context context;
    private final String PIC_BASE_SRC = "xxx";
    private final String PIC_END_SRC = "xxx";


    public FeedType(String contactId, String name, View feedContent, Context context){
        try {
            this.profilePicSrc = BitmapFactory.decodeStream((InputStream)new URL(PIC_BASE_SRC + contactId.toString() + PIC_END_SRC).getContent());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.name = name;
        this.context = context;
        this.feedContent = feedContent;
    }

    public String getName(){
        return name;
    }

    public Bitmap getProfilePicture(){
        return profilePicSrc;
    }

    public View getContent(){
        return this.feedContent;
    }
}

包装类:

public class FeedWrapper {

    private View base;
    private ImageView pic = null;
    private TextView name = null;
    private LinearLayout content = null;

    public FeedWrapper(View base) {
        this.base = base;
    }

    public ImageView getProfilePicture() {
        if (pic == null) {
            pic = (ImageView)base.findViewById(R.id.pic);
        }
        return(pic);
    }

    public TextView getName() {
        if (name == null) {
            name = (TextView)base.findViewById(R.id.name);
        }
        return(name);
    }

    public LinearLayout getContent() {
        if (content == null) {
            content = (LinearLayout)base.findViewById(R.id.content);
        }
        else content.removeAllViews();
        return(content);
    }

}

ListAdapter类:

private class FeedListAdapter extends ArrayAdapter<Object> {

        public FeedListAdapter() {
            super(Feeds.this, R.layout.feed, new Object[feedArrLength]);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View feed = convertView;
            FeedWrapper wrapper = null;

            if (feed == null) {

                feed = getLayoutInflater().inflate(R.layout.feed, parent, false);
                wrapper = new FeedWrapper(feed);
                feed.setTag(wrapper);
            }
            else {
                wrapper = (FeedWrapper)feed.getTag();
            }

            wrapper.getName().setText(arr.get(position).getName());
            wrapper.getProfilePicture().setImageBitmap(arr.get(position).getProfilePicture());
            wrapper.getContent().addView(arr.get(position).getContent());
            return(feed);
        }
    }

我知道这有点难以理解,如果您有疑问,请询问。

提前谢谢,Elad!

1 个答案:

答案 0 :(得分:0)

我认为在 FeedType 对象中保留View是一种错误的方法。仅保留FeedType中的数据。

例如,创建新类 FeedContent ,其中包含有关Feed内容(不是任何视图)的已解析数据:

class FeedContent {

... // i.e. private List<String> contents;

}

然后更改 FeedType 类:

public class FeedType {

private Bitmap profilePicSrc;
private String name;
private FeedContent feedContent;
private Context context;
private final String PIC_BASE_SRC = "xxx";
private final String PIC_END_SRC = "xxx";


public FeedType(String contactId, String name, FeedContent feedContent, Context context){
    try {
        this.profilePicSrc = BitmapFactory.decodeStream((InputStream)new URL(PIC_BASE_SRC + contactId.toString() + PIC_END_SRC).getContent());
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    this.name = name;
    this.context = context;
    this.feedContent = feedContent;
}

public String getName(){
    return name;
}

public Bitmap getProfilePicture(){
    return profilePicSrc;
}

public FeedContent getContent(){
    return this.feedContent;
}

}

然后将 FeedListAdapter 设为:

private class FeedListAdapter extends ArrayAdapter<Object> {

    public FeedListAdapter() {
        super(Feeds.this, R.layout.feed, new Object[feedArrLength]);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View feed = convertView;
        FeedWrapper wrapper = null;

        if (feed == null) {

            feed = getLayoutInflater().inflate(R.layout.feed, parent, false);
            wrapper = new FeedWrapper(feed);
            feed.setTag(wrapper);
        }
        else {
            wrapper = (FeedWrapper)feed.getTag();
        }

        wrapper.getName().setText(arr.get(position).getName());
        wrapper.getProfilePicture().setImageBitmap(arr.get(position).getProfilePicture());
        View feedContentView = createContentView(arr.get(position).getContent());
        wrapper.getContent().addView(feedContentView);
        return(feed);
    }

    private View createContentView(FeedContent feedContent) {
        ... // Here you shuold inflate new view and fill it with data from "feedContent"
    }
}