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

时间:2018-01-15 19:28:32

标签: android listview android-viewholder repeat

感谢所有帮助的人!滚动ListView时遇到问题:当我添加更多项目时,我输入AutoCompleteTextView或EditText字段的值会重复(我从Activity添加)并滚动到底部。所以,看起来这些项目按某种顺序重复。我尝试搜索这个问题,并尝试使用viewHolder和viewInflate,更改inicialization的位置,但问题不会去ot

以下是我的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();
    }
});

1 个答案:

答案 0 :(得分:0)

您似乎在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(); 这些线可能是罪魁祸首。 我认为您应该首先重置视图的内容,并且非常明确地将视图的内容复制回模型(而不是每次调用getView时)。