immanual
immanual

Reputation: 19

setText() of EditText not working inside onTextChanged method

From a newbie in android, I have a custom ListView of food items, where each item has item name, quantity and amount. quantity is Edit text and amount is text view. Initially in amount field i'm displaying price for one quantity of item. when the user enters the quantity (Integer) i'm trying to multiply the quantity with amount and display the total amount in the same text view(i.e textView amount) So i'm using onTextChanged of TextWatcher .

When the user enters the quantity i'm able to receive it and multiply with original amount to get the total amount but when i'm trying to set it using textviewAmount.setText(total amount) inside onTextChanged method its not working.

While searching for the solution I came across this this .which says

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

Below is the sample code of what i have done

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;
}}

Adopter

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

Thanks for your help :)

Upvotes: 1

Views: 561

Answers (2)

Anil Atri
Anil Atri

Reputation: 68

Please use String.valueOf(value) If still value does not set than their is some problem in adapter view .

Upvotes: 0

Alireza Ahmadi
Alireza Ahmadi

Reputation: 5338

Well, This is not the right way to handle this, Even if you figure out a way to set the text on textview, After you scroll the page the view is getting destroyed and next time when it gets created it uses the default value and not the one you specified!

I suggest two things, First do not use ListView, Instead use RecylerView. RecyclerView is a replacement for ListView since 2 years ago. Second and more importantly whenever your list changes don't update the view, update the model itself, then ask the adapter to recreate the view. It will create the view with updated values. You just have to call notifyDataSetChanged() on the adapter.

Upvotes: 1

Related Questions