根据AutoCompleteTextView输入主动更新ImageView src

时间:2019-02-06 15:29:46

标签: java android autocompletetextview

我正在开发一个应用,您可以在其中发布看到的酷车图像。用户必须从AutoCompleteTextView中选择图片的制造商和型号。 AutoCompleteTextViews的ArrayAdapters从带有for循环的Firebase获得。如果从制造商或型号的阵列中找到了所写的字符串,则在两个输入字段旁边都有一个图像,该图像应显示绿色的选中标记。这样,用户必须从“自动完成”建议中选择字符串。默认情况下,图像为红色感叹号。该应用的外观如下:

the red circeled images should change

我对此有一个代码应该可以工作,但是它的行为很奇怪。图像在第一次尝试时不会改变,但是如果我多次输入和删除例如制造商,则图像会改变。它什么都不是一致的。这是我的那部分代码:

//SETTING UP THE MANUFACTURER AUTOCOMPLETE TEXT VIEW FROM DATABASE
    carReference.child("Manufacturers").addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            autoCompleteManufacts.clear();

            for (DataSnapshot manufactSuggestionSnapshot : dataSnapshot.getChildren())
            {
                String manufact = manufactSuggestionSnapshot.child("Manufact").getValue(String.class);

                autoCompleteManufacts.add(manufact);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError)
        {

        }
    });
    manufactAutoET.setAdapter(autoCompleteManufacts);

    monitorManufacts();
}


//CHANGE MANUFACTURER CHECK MARK
private void changeManufactCheckMark()
{
    manufactCheckIW.setVisibility(View.VISIBLE);

    for(int i = 0; i < autoCompleteManufacts.getCount(); i++)
    {
        String carMaker = autoCompleteManufacts.getItem(i).toString();
        if(manufactAutoET.getText().toString().equals(carMaker))
        {
            manufacturerReady = true;

            manufactCheckIW.setImageResource(R.drawable.check);
            manufactCheckIW.setColorFilter(Color.rgb(109, 213, 79));
            setAdapterForModelInput(manufactAutoET.getText().toString());
        }
        else if(!manufactAutoET.getText().toString().equals(carMaker))
        {
            manufacturerReady = false;

            manufactCheckIW.setImageResource(R.drawable.error);
            manufactCheckIW.setColorFilter(Color.rgb(213, 79, 79));
        }
    }
}


//SET THE MODEL EDIT TEXT ADAPTER ACCORDING TO THE MANUFACTURER
private void setAdapterForModelInput(String activeManufact)
{
    Toast.makeText(this, activeManufact, Toast.LENGTH_SHORT).show();

    carReference.child("Manufacturers").child(activeManufact).addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            autoCompleteModels.clear();

            for (DataSnapshot modelSuggestionSnapshot : dataSnapshot.getChildren())
            {
                if(modelSuggestionSnapshot.hasChild("Model"))
                {
                    String model = modelSuggestionSnapshot.child("Model").getValue(String.class);

                    autoCompleteModels.add(model);
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError)
        {

        }
    });
    modelAutoET.setAdapter(autoCompleteModels);

    monitorModels();
}


//CHANGE MODEL CHECK MARK
private void changeModelCheckMark()
{
    modelCheckIW.setVisibility(View.VISIBLE);

    for(int i = 0; i < autoCompleteModels.getCount(); i++)
    {
        String carModel = autoCompleteModels.getItem(i).toString();
        if(modelAutoET.getText().toString().equals(carModel))
        {
            modelReady = true;

            modelCheckIW.setImageResource(R.drawable.check);
            modelCheckIW.setColorFilter(Color.rgb(109, 213, 79));
        }
        else if(!modelAutoET.getText().toString().equals(carModel))
        {
            modelReady = false;

            modelCheckIW.setImageResource(R.drawable.error);
            modelCheckIW.setColorFilter(Color.rgb(213, 79, 79));
        }
    }
}


//MONITOR MANUFACT INPUT
private void monitorManufacts()
{
    manufactAutoET.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            changeManufactCheckMark();

            modelAutoET.setText("");
            modelCheckIW.setVisibility(View.INVISIBLE);
        }

        @Override
        public void afterTextChanged(Editable s)
        {

        }
    });
}


//MONITOR MODEL INPUT
private void monitorModels()
{
    modelAutoET.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            changeModelCheckMark();
        }

        @Override
        public void afterTextChanged(Editable s)
        {

        }
    });
}

有人知道为什么它不起作用吗?非常感谢! :)

0 个答案:

没有答案