Ramz February 2016

addTextChangedListener is not working inside Base Adapter

I have implemented a customized listview with name,price,quantity etc,

By default the quantity is 1 in all the list items.

If user changes one of the list item's quantity from 1 to 2 , then the changed quantity has to updated in the Sqlite database.

Problem

I tried using addTextChangedListener to get the updated data from user input and update it in the sqlite database as well. But addTextChangedListener is not working .

I am unable to update the required field in the database.

Below is the code where I use addTextChangedListener.

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {

    View rowView = convertView;

    if (convertView == null) {

        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        rowView = inflater.inflate(R.layout.list_items_cart, null);


        holder.tv_name = (TextView) rowView.findViewById(R.id.name_cart);
        holder.tv_price = (TextView) rowView.findViewById(R.id.price_cart);
        holder.image = (ImageView) rowView.findViewById(R.id.image_cart);
        holder.tv_model = (TextView) rowView.findViewById(R.id.model_cart);
        holder.tv_product = (TextView) rowView.findViewById(R.id.product_cart);
        holder.delete = (Button) rowView.findViewById(R.id.delete);
        holder.quantity = (EditText) rowView.findViewById(R.id.quantity);
        rowView.setTag(holder);



    }
    else

    holder = (Holder) rowView.getTag();

    holder.tv_name.setText(list_name.get(position));
    name = holder.tv_name.getText().toString();
    holder.tv_price.setText(list_price.get(position));
    price = holder.tv_price.getText().toString();
    holder.tv_model.setText(list_model.get(position));
    model = holder.tv_model.getText().toString();
    holder.tv_product.setText(        

Answers


tynn February 2016

Your biggest issue is, that you're using fields instead of local variables.

quant = holder.quantity.getText().toString();

updates quant with every call to getView() so

@Override
public void afterTextChanged(Editable s) {
    value = s.toString().trim();
    quant.replace(holder.quantity.getText().toString(), value);
    updateTable();
}

will use the text field last bound to data and not the the one you added the listener to.

You need to take care of the scope of the variables you're using and introduce consistency to your model.

I would recommend a complete rewrite of the adapter without using any fields.

Post Status

Asked in February 2016
Viewed 2,670 times
Voted 13
Answered 1 times

Search




Leave an answer