Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make a List of HashMap type in recycler view adapter

I am using this liabray for swipe gestures. https://github.com/nikhilpanju/RecyclerViewEnhanced. In the MainAdapter.java class recyclerView contains List but i want to replace this to List> type. When i do this, so many errors appear. Kindly guide me how can i do this or suggest me any tutorial to do that.

MainAdapter.java

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {
    LayoutInflater inflater;
    List<RowModel> modelList;

    public MainAdapter(Context context, List<RowModel> list) {
        inflater = LayoutInflater.from(context);
        modelList = new ArrayList<>(list);
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.recycler_row, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        holder.bindData(modelList.get(position));
    }

    @Override
    public int getItemCount() {
        return modelList.size();
    }

    class MainViewHolder extends RecyclerView.ViewHolder {

        TextView mainText, subText;

        public MainViewHolder(View itemView) {
            super(itemView);
            mainText = (TextView) itemView.findViewById(R.id.mainText);
            subText = (TextView) itemView.findViewById(R.id.subText);
        }

        public void bindData(RowModel rowModel) {
            mainText.setText(rowModel.getMainText());
            subText.setText(rowModel.getSubText());
        }
    }
}

Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_panel);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        unclickableRows = new ArrayList<>();
        unswipeableRows = new ArrayList<>();
        dialogItems = new String[25];
        for (int i = 0; i < 25; i++) {
            dialogItems[i] = String.valueOf(i + 1);
        }

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mAdapter = new MainAdapter(this, getData());
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        onTouchListener = new RecyclerTouchListener(this, mRecyclerView);
        onTouchListener
                .setIndependentViews(R.id.rowButton)
                .setViewsToFade(R.id.rowButton)
                .setClickable(new RecyclerTouchListener.OnRowClickListener() {
                    @Override
                    public void onRowClicked(int position) {
                        util.shortToast(getApplicationContext(), "Row " + (position + 1) + " clicked!");
                    }

                    @Override
                    public void onIndependentViewClicked(int independentViewID, int position) {
                        util.shortToast(getApplicationContext(), "Button in row " + (position + 1) + " clicked!");
                    }
                })
                .setSwipeOptionViews(R.id.add, R.id.edit, R.id.change)
                .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        String message = "";
                        if (viewID == R.id.add) {
                            message += "Add";
                        } else if (viewID == R.id.edit) {
                            message += "Edit";
                        } else if (viewID == R.id.change) {
                            message += "Change";
                        }
                        message += " clicked for row " + (position + 1);
                        util.shortToast(getApplicationContext(), message);
                    }
                });
        mRecyclerView.addOnItemTouchListener(onTouchListener);





        // Detect touched area
        detector = new Swipe(this,this);

        db = new DB(getApplicationContext());

        Bundle bundle = getIntent().getExtras();
        if(bundle != null) {
            String id = bundle.getString("id");
            Toast.makeText(this, "Welcome " + db.getUsernameById(id), Toast.LENGTH_LONG).show();
        }

        //get references of layout
//        adapter = new AdapterUsers(this, R.layout.textview_users, arrayList);
//        listView = (ListView) findViewById(R.id.users);
//        listView.setAdapter(adapter);
//
//        showAllUsers();
    }

    public void showAllUsers() {
        try {
            Cursor cursor = db.selectAllUsers();
            cursor.moveToFirst();
            adapter.clear();
            if (cursor.getCount() == 0) {
                Toast.makeText(this, "Empty user list", Toast.LENGTH_SHORT).show();
            } else {
                for (int i = 0; i < cursor.getCount(); i++) {
                    HashMap<String, Object> hm = new HashMap<String, Object>();
                    hm.put(ID, cursor.getString(cursor.getColumnIndex(db.ID_USER)));
                    hm.put(USERNAME, cursor.getString(cursor.getColumnIndex(db.NAME_USER)));
                    arrayList.add(hm);
                    cursor.moveToNext();
                }
            }
        } catch (Exception e) {
            Log.d("Show users", " failed due to " + e.getMessage());
        }
    }


    // Swipe interface
    @Override
    public void onSwipe(int direction) {
        String str = "";

        switch (direction) {

            case Swipe.SWIPE_RIGHT :
                listView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                        //deleteUser(id);
                        Toast.makeText(AdminPanel.this, "user deleted", Toast.LENGTH_LONG).show();
                    }
                });
                str = "Swipe Right";
                break;
            case Swipe.SWIPE_LEFT :  str = "Swipe Left";
                break;
            case Swipe.SWIPE_DOWN :  str = "Swipe Down";
                break;
            case Swipe.SWIPE_UP :    str = "Swipe Up";
                break;

        }
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDoubleTap() {
        Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (touchListener != null) touchListener.getTouchCoordinates(ev);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void setOnActivityTouchListener(OnActivityTouchListener listener) {
        this.touchListener = listener;
    }

    private List<RowModel> getData() {
        List<RowModel> list = new ArrayList<>(25);
        for (int i = 0; i < 25; i++) {
            list.add(new RowModel("Row " + (i + 1), "Some Text... "));
        }
        return list;
    }

}
like image 323
bc120201817 Zohaib Siddiq Avatar asked Jul 01 '16 10:07

bc120201817 Zohaib Siddiq


1 Answers

you can change your getData() method and make it return HashMap.

 private HashMap <String,String> getData() {
        HashMap <String,String> list = new HashMap <String,String>();
        for (int i = 0; i < 25; i++) {
            list.put(i,"Text");
        }
        return list;
    }

now in your Adapter you can handle it like...

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {
LayoutInflater inflater;
<HashMap<String,String> modelList;

public MainAdapter(Context context, <HashMap<String,String> modelList) {
    this.inflater = LayoutInflater.from(context);
    this.modelList = modelList;
}

@Override
public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.recycler_row, parent, false);
    return new MainViewHolder(view);
}

@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
    //holder.bindData();
    holder.mainText.setText(modelList.get(position)); // value for the given key
}

@Override
public int getItemCount() {
    return modelList.size();
}

class MainViewHolder extends RecyclerView.ViewHolder {

    TextView mainText, subText;

    public MainViewHolder(View itemView) {
        super(itemView);
        mainText = (TextView) itemView.findViewById(R.id.mainText);
        subText = (TextView) itemView.findViewById(R.id.subText);
    }

}
}
like image 174
Newbiee Avatar answered Nov 10 '22 07:11

Newbiee