我一直在开发一个在列表活动中显示提要的应用程序。 首先,我要获取信息并进行解析,然后将其放入我创建的“ FeedType”数组中,该数组具有3个字段:位图图片,字符串名称和查看内容。 内容代表提要的内容,并且有不同类型的内容。 该数组正在解析过程中初始化。

我在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);
        }
    }

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

预先感谢,埃拉德!

===============>>#1 票数:0 已采纳

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

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

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"
    }
}

  ask by Elad92 translate from so

未解决问题?本站智能推荐: