-2

This is my listview, my problem is why I'm trying to click on my ImageButton(in listview) but the textview(in listview) never change the value that what I want. It's take my whole morning, can someone please help me, Thanks.

 public class ViewAdapter extends BaseAdapter implements Filterable {

        LayoutInflater mInflater;

        public ViewAdapter() {
            mInflater = LayoutInflater.from(getActivity());
        }

        @Override
        public int getCount() {
            return pl.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

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

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listproduct, null);
            }
            final TextView ProductCode = (TextView) convertView.findViewById(R.id.productcode);
            ProductCode.setText("" + pl.get(position).getProductCode());
            final TextView Qty = (TextView) convertView.findViewById(R.id.qty);
            Qty.setText("" + pl.get(position).getQty());
            final TextView Description = (TextView) convertView.findViewById(R.id.description);
            Description.setText("" + pl.get(position).getProductName());
            final String p = Description.getText().toString();
            final TextView Price = (TextView) convertView.findViewById(R.id.price);
            Price.setText("" + pl.get(position).getProductPrice());
            final ImageButton minus = (ImageButton) convertView.findViewById(R.id.minus);

            minus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     Qty.setText("testing")// textview never change to "testing"
                }
            });
            notifyDataSetChanged();
            return convertView;
        }

4 Answers4

0

Basically you should use RecyclerView,but if you still want to use the listView, use the view-holder approach as in this link below:

Optimize list view

And you should never ever call notifyDataSetChanged() in getView method as its called multiple time as your listview scrolls

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42
0

Please try your code with holder just like follows

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

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_entry, null);
        holder = new ViewHolder();
        holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
        holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
        holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    Person person = getItem(position);

    holder.nameTextView.setText(person.getName());
    holder.surnameTextView.setText(person.getSurname());
    //holder.personImageView.setImageBitmap(person.getImage());

    return convertView;
}

Then click as follows

holder.personImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 holder.nameTextView.setText("testing")// textview never change to "testing"
            }
        });
Richardson. M
  • 852
  • 2
  • 17
  • 28
0
Use this code : 





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

                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.listproduct, null);
                    }
                    final TextView ProductCode = (TextView) convertView.findViewById(R.id.productcode);
                    ProductCode.setText("" + pl.get(position).getProductCode());
                    final TextView Qty = (TextView) convertView.findViewById(R.id.qty);

                    final TextView Description = (TextView) convertView.findViewById(R.id.description);
                    Description.setText("" + pl.get(position).getProductName());
                    final String p = Description.getText().toString();
                    final TextView Price = (TextView) convertView.findViewById(R.id.price);
                    Price.setText("" + pl.get(position).getProductPrice());
                    ImageButton minus = (ImageButton) convertView.findViewById(R.id.minus);
                     minus.setTag(position);
                    minus.setOnClickListener(clickButton);

                    return convertView;
                }

          private View.OnClickListener clickButton = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Object tag =v.getTag();
                   if(tag!=null)
                  {
                  int pos = (Integer)tag;
               Qty.setText(pl.get(position).getQty());
              notifyDataSetChanged();


        }
                }
            } ;
0

As far as i understand from your question this cannot be done because once you populate the data in custom listView you have recreate it or call the custom listView again with text display you want to change!

Mirza715
  • 420
  • 5
  • 15