mercredi 15 juin 2016

How to use two adapter in single class

Right now I am doing chat functionalities.I am extending ArrayAdapter.With the help of these ArrayAdapter only, I can achieve many funtionalities in chat.So I can't change this ArrayAdapter.

But I have to use another one Adapter(BaseSwipeListAdapter.java) to achive swipe left(enable or not) functionlity.

ChatActivity.java:(using ChatAdapter inside this class):

 static public class ChatAdapter extends ArrayAdapter<ChatMessage>  {

    private final List<ChatMessage> chatMessages;
    private Activity context;
    String loggedUserId, loggedUserName;

    public ChatAdapter(Activity context, int resource, List<ChatMessage> chatMessages) {
        super(context, resource, chatMessages);
        this.context = context;
        this.chatMessages = chatMessages;
    }

  }

I need to use this below code in ChatActivity.java to achieve my result.But I can't extend BaseSwipeListAdapter.Because I am already using ArrayAdapter.

    @Override
    public boolean getSwipEnableByPosition(int position) {
        if(position % 2 == 0){
            return false;
        }
        return true;
    }

BaseSwipeListAdapter.java:

import android.widget.BaseAdapter;


public abstract class BaseSwipListAdapter extends BaseAdapter {

    public boolean getSwipEnableByPosition(int position){
        return true;
    }



}

SwipeMenuAdapter.java: (Libarary class):

public class SwipeMenuAdapter implements WrapperListAdapter,
        OnSwipeItemClickListener {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SwipeMenuLayout layout = null;

        if (mAdapter instanceof BaseSwipListAdapter) {

            boolean swipEnable = (((BaseSwipListAdapter) mAdapter).getSwipEnableByPosition(position));

            layout.setSwipEnable(swipEnable);   --->This line is used to enable and disable the swipe left in listview.I need to use this method
        }

        return layout;
    }

I think it's not possible to use two adapter in single class.is there any alternate way to solve this issue.

Aucun commentaire:

Enregistrer un commentaire