gpt4 book ai didi

android - EditText 的 setText() 在 onTextChanged 方法中不起作用

转载 作者:行者123 更新时间:2023-11-30 00:06:47 27 4
gpt4 key购买 nike

从一个 android 新手那里,我有一个自定义的食品 ListView ,其中每个项目都有项目名称、数量和金额。数量是编辑文本,数量是 TextView 。最初在金额字段中,我显示了一种商品的价格。当用户输入数量(整数)时,我试图将数量乘以数量并在同一 TextView 中显示总数量(即 textView 数量)所以我正在使用 TextWatcher 的 onTextChanged。

当用户输入我能够接收的数量并乘以原始数量以获得总量但是当我尝试在 onTextChanged 方法中使用 textviewAmount.setText(total amount) 设置它时它不起作用。

在寻找解决方案时,我遇到了这个 this .这说

"The EditText appears to have an issue with resetting text in onCreateView"

下面是 我所做的示例代码

fragment

public class FoodListFragment extends Fragment {
List<FoodListCell> foodListCells ;
ListView listView ;
HotelApp hotelApp;
FragmentTransaction ft;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

hotelApp = (HotelApp)getActivity().getApplicationContext();

View view = inflater.inflate(R.layout.food_listview,container,false);
Log.d("FoodListActivity" ," onCreate");

foodListCells = new ArrayList<>();
listView = (ListView)view.findViewById(R.id.list_view);

foodListCells.add(new FoodListCell("Diet-Vegetable Soup","75.0"));
foodListCells.add(new FoodListCell("Bean-Bacon ","95.0"));
foodListCells.add(new FoodListCell("Chicken Noodle Soup","75.0"));

FoodListAdopter adapter = new FoodListAdopter(hotelApp, R.layout.foodbox_layout, foodListCells);

listView.setAdapter(adapter);

return view;
}}

采纳者

public class MenuAdopter extends ArrayAdapter<FoodListCell>  {
Context context;
int resource;
List<FoodListCell> foodListCells;
HotelApp hotelApp;
EditText quantity ;
TextView foodBoxAmt ;
Double finalAmt = 0.0, amtForOne = 0.0;

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
hotelApp = (HotelApp)this.context;

LayoutInflater layoutInflater = LayoutInflater.from(context);

View view = layoutInflater.inflate(R.layout.foodbox_layout,null);

TextView foodBoxDesc = (TextView) view.findViewById(R.id.foodBoxDesc);
foodBoxAmt = (TextView) view.findViewById(R.id.foodBoxAmt);
Button button = (Button) view.findViewById(R.id.addToCartButton);
quantity = (EditText) view.findViewById(R.id.qnty);

final FoodListCell foodListCell = foodListCells.get(position);

foodBoxDesc.setText(foodListCell.getDesc());
foodBoxAmt.setText("Rs." +foodListCell.getPrice());
amtForOne = Double.parseDouble(foodListCell.getPrice().toString());
quantity.addTextChangedListener(new EditTextListener(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

finalAmt=Double.parseDouble(foodListCell.getPrice().toString())*Double.parseDouble(s.toString());
foodBoxAmt.setText("Rs." +finalAmt.toString());
}}
);
return view;

}}

文本观察器

public class EditTextListener implements 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) {
}
@Override
public void afterTextChanged(Editable s) {
}}

感谢您的帮助:)

最佳答案

好吧,这不是处理这个问题的正确方法,即使你找到了一种在 textview 上设置文本的方法,在你滚动页面后, View 会被销毁,下次创建时它会使用默认值值,而不是您指定的值!

我建议两件事,首先不要使用 ListView,而是使用 RecylerView。 RecyclerView 是 2 年前 ListView 的替代品。其次,更重要的是,每当您的列表更改时,不要更新 View ,更新模型本身,然后要求适配器重新创建 View 。它将创建具有更新值的 View 。您只需在适配器上调用 notifyDataSetChanged()

关于android - EditText 的 setText() 在 onTextChanged 方法中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48833043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com