dynamic form in a listview

寵の児 提交于 2019-11-29 16:49:05

If you are inflating the different item into the listview than u need to use this method getItemViewType of the BaseAdapter class.

Have a look into my solution i have distinguished the view and managed accordingly

public class YOUR_ADAPTER_CLASS extends BaseAdapter {

    private int ITEM_BOOLEAN = 1;
    private int ITEM_STRING = 2;
    private int ITEM_INT = 3;

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

    @Override
    public Object getItem(int position) {
        return questions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return questions.indexOf(getItem(position));
    }


    @Override
    public int getItemViewType(int position) {


        if (questions.get(position).getQuestionType().equalsIgnoreCase("BOOLEAN")) {
            return ITEM_BOOLEAN;
        } else if (questions.get(position).getQuestionType().equalsIgnoreCase("STRING")) {
            return ITEM_STRING;
        } else {
            return ITEM_INT;
        }

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        int listViewItemType = getItemViewType(position);


        if (convertView == null) {

            if (listViewItemType == ITEM_BOOLEAN) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.boolean_layout, null);

            } else if (listViewItemType == ITEM_STRING) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.string_question, null);
            } else {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.number_question, null);
            }
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        if (listViewItemType == ITEM_BOOLEAN) {

            /**
             * GET THE BOOLEAN ITEM AND INFALTE DATA INTO IT
             */

        } else if (listViewItemType == ITEM_STRING) {
            /**
             * GET THE STRING ITEM AND INFALTE DATA INTO IT
             */

        } else {
            /**
             * GET THE INT ITEM AND INFALTE DATA INTO IT
             */
        }

        return convertView;
    }

}

Have a look on my another solution which is in the Recyclview Recyclview with different item inflation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!