如何在EditText的基础上更新自定义ArrayAdapter的TextView?

时间:2017-07-02 21:04:11

标签: android android-edittext android-arrayadapter

我有一个自定义的ArrayAdapter,它是一个包含02 EditText字段和01 TextView的水平LinearLayout .....所有都设置为/显示numberDecimals。

我想要的是,只要在任何EditText字段中输入数字,两个数字(EditText 1& EditText 2)的乘积就会显示在TextView中。

我已尝试过OnFocusChangedListener和TextChangedListener,但TextView在实时输入期间没有更新。它没有任何显示。

我是编程新手,所以无法找到任何其他解决方案。 提前谢谢!

以下是适配器代码:

TextView labelView;
EditText ratioView;
EditText rateView;
TextView avgView;


public FibresAdapter(Activity context, ArrayList<Fibres> fibresList) {
    super(context, 0, fibresList);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }
    final Fibres currentFibre = getItem(position);

    labelView = (TextView) convertView.findViewById(R.id.label);
    labelView.setText(currentFibre.fibreLabel);

    ratioView = (EditText) convertView.findViewById(ratio);
    rateView = (EditText) convertView.findViewById(R.id.rate);
    avgView = (TextView) convertView.findViewById(R.id.avg);

    ratioView.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) {
            if (s.toString().length() > 0) {
                currentFibre.fibreRatio = Double.parseDouble(s.toString());
                avgView.setText("Testing");
            } else {
                currentFibre.fibreRatio = 0;
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    rateView.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) {
            if (s.toString().length() > 0) {
                currentFibre.fibreRate = Double.parseDouble(s.toString());
                avgView.setText("Testing");
            } else {
                currentFibre.fibreRate = 0;
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    return convertView;
}

以下是活动代码:

double totalAvg;
double totalRatio;

TextView totalAvgView;
TextView totalRatioView;

static ArrayList<Fibres> fibres;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fibres_list);

    fibres = new ArrayList<>();
    int x = 0;

    while (x < Fibres.fibCatArray.length) {
        fibres.add(new Fibres(Fibres.fibCatArray[x]));
        x++;
    }

    ListView listView = (ListView) findViewById(R.id.list);
    FibresAdapter adapter = new FibresAdapter(this, fibres);

    listView.setAdapter(adapter);

    totalRatioView = (TextView) findViewById(R.id.total_ratio);
    totalAvgView = (TextView) findViewById(R.id.total_avg);

}

public void displayTotals() {
    totalRatio = 0;
    totalAvg = 0;

    for (Fibres materials : fibres) {
        totalRatio += materials.fibreRatio;
        totalAvg += (materials.fibreRatio / 100 * materials.fibreRate);
    }
    totalRatioView.setText(totalRatio + "");
    totalAvgView.setText(totalAvg + "");

}

public boolean validateData() {
    if (totalRatio != 100) {
        Toast.makeText(this, "Total should be 100%!", Toast.LENGTH_SHORT).show();
        return false;
    }

    for (Fibres materials : fibres) {
        if (materials.fibreRatio > 1 && materials.fibreRate <= 0) {
            Toast.makeText(this, "Enter both Ratio & Rate!", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

在你的适配器中,将addTextChangedListener添加到EditText并在其中使用textView.setText()。这样的事情。

editText1.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() != 0)
        textView.setText("");
   }
  });