当在 ListView 中调用 2 个自定义布局时,Listview 项目在滚动时随机播放

Listview items are shuffling on scroll when 2 custom layouts called in ListView

我在 1 个列表视图中调用 2 个布局

一个是用于 textView 和 editText 的线性,另一个只是一个微调器

但是,当我尝试通过程序(下面给出的代码)调用线性布局中的项目时, 每次滚动列表时,项目都会被打乱顺序或从序列中重新定位。

没有显示错误,但不知何故发生了这种情况。

我希望列表被调用一次并保持顺序。

非常感谢任何帮助。下面是我的代码

Activity:

Fillup_Details.java

public class Fillup_Details extends AppCompatActivity {
        private ListView listView;
        Spinner spinner;
        ArrayList<ListViemItems> listViemItemses; 

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fillup__details);
            listView = (ListView) findViewById(R.id.listFillupDetails);
            spinner = (Spinner) findViewById(R.id.spnrFillupDetails);
            setTitle("Fill-Up Details");
            loadFillupDetails();

        }

        public void loadFillupDetails() {
            listViemItemses = new ArrayList<ListViemItems>();
            ListViemItems items1 = new ListViemItems();
            items1.setListItems("ListItem1");
            items1.setOddoreven(0);
            listViemItemses.add(items1);

            ListViemItems items2 = new ListViemItems();
            items2.setListItems("ListItem2");
            items2.setOddoreven(0);
            listViemItemses.add(items2);

            //.....many more objects


            ListViemItems itemspnr = new ListViemItems();
            itemspnr.setSpinnerItems("Spinner1");
            itemspnr.setOddoreven(1);
            listViemItemses.add(itemspnr);

                    FillupDetailsList fillupDetailsList = new FillupDetailsList(Fillup_Details.this, listViemItemses);
                    listView.setAdapter(fillupDetailsList);


        } 
    }

适配器

FillupDetailsList.java

class FillupDetailsList extends BaseAdapter {
    private static final String TAGGGG = "FillupDetailsList";
    Context context;
    String[] spnrItemasas =new String[]{"a","b"} ;
    LayoutInflater layoutInflater =null;
    View view;
    ViewHolder viewHolder;
    ArrayList<ListViemItems> listViemItems;
    int type;
    private static final int TYPE_LIST_ITEMS = 0;
    private static final int TYPE_SPINNER_ITEMS= 1;
    public FillupDetailsList(Context context, ArrayList<ListViemItems> listViemItems) {
        super();
        this.context = context;
        this.listViemItems=listViemItems;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {

        return listViemItems.size();
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }
    class ViewHolder{
        Spinner spinner;
        TextView txtItem;
        EditText edtTextItem;
        int ref;
    }

    @Override
    public int getItemViewType(int position) {
        if(listViemItems.get(position).getOddoreven()==0){

            type=TYPE_LIST_ITEMS;
        }else if(listViemItems.get(position).getOddoreven()==1){
            type=TYPE_SPINNER_ITEMS;
        }
        return type;
    }
    @Override
    public int getViewTypeCount() {
        return 5;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        if(convertView==null){
            int type = getItemViewType(position);
            Log.d(TAGGGG, "getView " + position + " " + convertView + " type = " + type);
            viewHolder = new ViewHolder();
            switch (type){
                case TYPE_LIST_ITEMS:

                    convertView = layoutInflater.inflate(R.layout.fillup_details_layout,null);
                    viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtFillupDetails);
                    viewHolder.edtTextItem = (EditText) convertView.findViewById(R.id.edtFillupDetails);
                    viewHolder.txtItem.setText(listViemItems.get(position).getListItems());

                    break;
                case TYPE_SPINNER_ITEMS:
                    ArrayList<String> as = new ArrayList<String>();
                    ArrayAdapter<String> adptrSpnrItem = new ArrayAdapter<String>(context, R.layout.spinner_item, spnrItemasas);
                    adptrSpnrItem.setDropDownViewResource(R.layout.spinner_item);
                    convertView = layoutInflater.inflate(R.layout.fillup_details_layout_spinner,viewGroup,false);
                    viewHolder.spinner = (Spinner)convertView.findViewById(R.id.spnrFillupDetails);
                    viewHolder.spinner.setAdapter(adptrSpnrItem);
                    break;
                default:
                    break;
            }

            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

}

我得到了答案,我所要做的就是在开关盒中进行以下更改

 @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int type = getItemViewType(position);
        viewHolder = new ViewHolder();
        switch (type) {
            case TYPE_LIST_ITEMS:
                if (convertView == null) {
                    convertView = layoutInflater.inflate(R.layout.fillup_details_layout, null);
                }
                viewHolder.txtItem = convertView.findViewById(R.id.txtFillupDetails);
                ListViemItems detail_position = listViemItems.get(position);
                viewHolder.txtItem.setText(detail_position.getListItems());
                break;
            case TYPE_SPINNER_ITEMS:
                if (convertView == null) {
                    convertView = layoutInflater.inflate(R.layout.fillup_details_layout_spinner, null);
                }
                ListViemItems detail_position2 = listViemItems.get(position);
                ArrayAdapter<String> adptrSpnrItem =
                        new ArrayAdapter<String>(context, R.layout.spinner_item, detail_position2.getSpinnerItems());
                adptrSpnrItem.setDropDownViewResource(R.layout.spinner_item);

                viewHolder.spinner = (Spinner) convertView.findViewById(R.id.spnrFillupDetails);
                viewHolder.spinner.setAdapter(adptrSpnrItem);
                break;
        }
        return convertView;
    }