0

Situation:

I have an android app where users do certain actions and those actions should be shown in a feed like Facebook's feed. Actions are 4 types - become friends with, follow somebody, comment and rate.

What I know how to do (see example below*)

I know how to extend BaseAdapter and customize its getView method so I can create a custom list item that I can later populate a listview with. Example - a list of a user's friends having their avatars, their names and a couple of buttons for each.

What I dont know how to do and Im asking your help with

I dont know how to make the feed show different customized layouts for the last 10 actions that happened - this guy became friends with that guy, then this guy rated that guy's work, then this guy commented on that thing...

How do I implement that?

*An example of creating a custom adapterable item and then populating a list view with a bunch of them:

public class MyProfileFriendAdapter extends BaseAdapter {

    private ArrayList<String> userNames =  new ArrayList<String>();
    private ArrayList<String> userAvatarPaths   = new ArrayList<String>();
    private ArrayList<Integer> userIds = new ArrayList<Integer>();
    private Context context;
    private LayoutInflater layoutInflater;

    private ImageButton iMyFriendsFriendAvatarImage;
    private TextView tvMyFriendsListFriendName;
    private Button bMyFriendsMessage;
    private Button bMyFriendsUnfriend;

    public MyProfileFriendAdapter(Context context, ArrayList<String> userNames, ArrayList<String> userAvatarPaths,  ArrayList<Integer> userIds){

        this.context = context;
        this.userNames = userNames;
        this.userAvatarPaths = userAvatarPaths;
        this.userIds = userIds;

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return userIds.size();
    }

    @Override
    public Object getItem(int arg0) {
        return userIds.get(arg0)    ;
    }

    @Override
    public long getItemId(int position) {
        return userIds.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null) {
            convertView = layoutInflater.inflate(R.layout.item_adapterable_my_profile_friend, parent, false);
        }

        iMyFriendsFriendAvatarImage = (ImageButton) convertView.findViewById(R.id.iMyFriendsFriendAvatarImage);
        tvMyFriendsListFriendName = (TextView) convertView.findViewById(R.id.tvMyFriendsListFriendName);

        bMyFriendsMessage = (Button) convertView.findViewById(R.id.bMyFriendsMessage);
        bMyFriendsUnfriend = (Button) convertView.findViewById(R.id.bMyFriendsUnfriend);    

        tvMyFriendsListFriendName.setText(userNames.get(position));

        iMyFriendsFriendAvatarImage.setImageBitmap(BitmapFactory.decodeFile(userAvatarPaths.get(position)));

        return convertView;
    }

}

then in the activity that holds the list view I do as follows:

    listOfFriends = (ListView) findViewById(R.id.lvMyProfileFriendsHolder);

    MyProfileFriendAdapter myProfileFriendAdapter = new MyProfileFriendAdapter(this, userNames, userAvatarPaths,  userIds);
    listOfFriends.setAdapter(myProfileFriendAdapter);

So what do I do when I need to use various different adapterable items in the same list, according to the last actions that users did in my app?

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • 1
    http://stackoverflow.com/questions/18868194/android-xml-layout-for-a-listview-with-different-items – Raghunandan Dec 29 '13 at 13:19
  • 1
    Check my answer in this question http://stackoverflow.com/questions/20757988/chat-application-in-android-so-that-sender-and-receiver-message-should-be-on-dif/20758196#20758196 I think that is wat you want – Abhishek V Dec 29 '13 at 13:19

1 Answers1

1

Have a look at this:

http://www.jiahaoliuliu.com/2012/08/android-list-view-with-different-views.html

It's a siple exmaple. For further information you can look for getViewCount & getItemViewType

Givi
  • 3,283
  • 4
  • 20
  • 23