繁体   English   中英

滚动时项目在列表视图中重复

[英]Items are repeating in listview when scrolling

谢谢大家的帮助! 滚动ListView时遇到问题:当我添加更多项操作(从Activity中添加)并滚动到底部时,输入AutoCompleteTextView或EditText字段的值会重复。 因此,看起来项目以一定顺序重复。 我尝试搜索此问题,并使用viewHolder和viewInflate进行很多尝试,更改了初始化的位置,但是问题没有解决

以下是我的OperationAdapter.java ---------------------------------

class OperationAdapter extends BaseAdapter {
    public ArrayList<Operation> operations;
    private Context context;
    private ArrayList<String> operationns = new ArrayList<>();

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");        

    public OperationAdapter(Context context, ArrayList<Operation> operations) {
        this.operations = operations;
        this.context = context;
        operationns.add("Maltcev");
        operationns.add("Loskut");
    }

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

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

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = lInflater.inflate(R.layout.operation_item, null);

            holder = new ViewHolder();

            holder.timeEndOperation = (EditText) convertView.findViewById(R.id.timeEndOperation);
            holder.etc = (EditText) convertView.findViewById(R.id.etc);
            holder.deleteOperationBut = (Button) convertView.findViewById(R.id.deleteOperation);
            holder.saveOperationBut = (Button) convertView.findViewById(R.id.saveOperation);
            holder.startTimeBut = (Button) convertView.findViewById(R.id.startTimeBut);
            holder.finishTimeBut = (Button) convertView.findViewById(R.id.finishTimeBut);
            holder.timeBeginOperation = (EditText) convertView.findViewById(R.id.timeBeginningOperation);
            holder.operationsAutocomplete = (AutoCompleteTextView) convertView.findViewById(R.id.operationAutocomplete);

            convertView.setTag(holder);



        } else {
            holder = (ViewHolder)convertView.getTag();
        }



        ArrayAdapter<String> adapterOperations = new ArrayAdapter<String>(context, R.layout.support_simple_spinner_dropdown_item, operationns);
        holder.operationsAutocomplete.setAdapter(adapterOperations);


        operations.get(position).timeOfStart = holder.timeBeginOperation.getText().toString();
        operations.get(position).timeOfFinish = holder.timeEndOperation.getText().toString();
        operations.get(position).etc = String.valueOf(position);
        operations.get(position).nameOfOperation = holder.operationsAutocomplete.getText().toString();

        final ViewHolder viewHolderFinal = holder;


        holder.deleteOperationBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolderFinal.timeBeginOperation.clearComposingText();
                viewHolderFinal.timeEndOperation.clearComposingText();
                viewHolderFinal.etc.clearComposingText();
                viewHolderFinal.operationsAutocomplete.clearComposingText();


                operations.remove(position); 
                notifyDataSetChanged();     
            }
        });

        holder.startTimeBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String currentStartTime = sdf.format(new Date());
                viewHolderFinal.timeBeginOperation.setText(currentStartTime);
            }
        });

        holder.finishTimeBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String currentEndTime = sdf.format(new Date());
                viewHolderFinal.timeEndOperation.setText(currentEndTime);
            }
        });
        return convertView;
    }

    public void addNewOperationCard() {
        Operation operation = new Operation("", "", "", "");
        operations.add(operation);
    }

    static class ViewHolder {
        public EditText timeBeginOperation;
        public EditText timeEndOperation;
        public EditText etc;
        public AutoCompleteTextView operationsAutocomplete;
        public Button deleteOperationBut;
        public Button saveOperationBut;
        public Button startTimeBut;
        public Button finishTimeBut;
    }
}

和来自Activity的代码块-----------------------------

final ListView operationListView = (ListView) findViewById(R.id.operationListView);

final OperationAdapter operationAdapter = new OperationAdapter(this, operations);
operationListView.setAdapter(operationAdapter);

Button addOperationButton = (Button) findViewById(R.id.addOperationButton);
addOperationButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        operationAdapter.addNewOperationCard();
        operationAdapter.notifyDataSetChanged();
        Toast.makeText(OperationAndWorkerActivity.this, operations.size() + " " + operationAdapter.operations.size(), Toast.LENGTH_LONG).show();
    }
});

似乎您正在getView实现中复制数据。 您从视图中在Operation对象上设置字段的那些行: operations.get(position).timeOfStart = holder.timeBeginOperation.getText().toString(); operations.get(position).timeOfFinish = holder.timeEndOperation.getText().toString(); operations.get(position).etc = String.valueOf(position); operations.get(position).nameOfOperation = holder.operationsAutocomplete.getText().toString(); operations.get(position).timeOfStart = holder.timeBeginOperation.getText().toString(); operations.get(position).timeOfFinish = holder.timeEndOperation.getText().toString(); operations.get(position).etc = String.valueOf(position); operations.get(position).nameOfOperation = holder.operationsAutocomplete.getText().toString(); 这些台词可能是罪魁祸首。 我认为您应该首先重置视图的内容,并非常明确地说明何时将视图的内容复制回模型(而不是每次调用getView )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM