0
我有一个自定义的ArrayAdapter,它是一个水平的LinearLayout,包含02 EditText字段和01 TextView .....所有设置为/显示numberDecimals。如何基于EditText更新自定义ArrayAdapter的TextView?
我只需要在任何EditText字段中输入数字时,TextView中将显示两个数字(EditText 1 & EditText 2)的乘积。
我已经尝试了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;
}
感谢您的回复!我已经做到了,但textView中仍然没有显示任何内容。 –
@ShahoodulHassan用你的适配器和活动/片段代码更新你的问题 – Googler
我已经用相关代码更新了这个问题。我注意到,文本现在出现在textview中,但出现在错误的地方。 PL建议一个解决方案。 –