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

时间:2018-02-16 18:44:08

标签: android listview android-edittext textwatcher

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

当用户输入我能够接收的数量并乘以原始数量以获得总金额时,但是当我尝试使用onTextChanged方法内的textviewAmount.setText(总金额)设置它时,它无法正常工作。

在搜索解决方案时,我遇到了这个问题this

  

“EditText似乎在onCreateView中重置文本时出现问题”

下面是  我所做的示例代码

片段

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;

}}

TextWatcher

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) {
}}

感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

嗯,这不是解决这个问题的正确方法,即使你找到了一种在textview上设置文本的方法,滚动页面后视图也会被破坏,下次创建时它会使用默认值价值,而不是你指定的价值!

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

答案 1 :(得分:0)

请使用String.valueOf(value) 如果仍然没有设置值,那么它们在适配器视图中是一些问题。