I’ve been working on an app that displays feeds in a list activity.
At first, I’m getting the information and parsing it, and I put it in a array of “FeedType” that I’ve created, that has 3 fields: Bitmap pic, String name and View content.
The content represent the feed’s content, and there’s different type of contents.
This array is being initialized in the parsing process.
I created the feed layout in a xml file.
Created a ImageView, TextView and LinearLayout that will contain the “content” by using addView().
I’ve created a wrapper and create my ListAdapter to reuse the object in the list.
My problem start when I’m trying to use add a view who is already a child of other LinearLayout, and it’s happening because when I retrieve the content from the array I’m retireving the reference to the existing view, and not a new view.
Is there a way to create a new view from existing view? Or somebody got other solution?
Code:
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;
}
}
Wrapper class:
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 class:
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);
}
}
I know that’s a bit difficult to understand, if you have questions please ask.
Thanks in advance, Elad!
I think it’s a wrong way to keep View in FeedType object. Keep only data in FeedType.
For example create new class FeedContent which contains parsed data about feed content (not any views):
Then change your FeedType class:
}
And then make your FeedListAdapter like: