3

I have a List view that I fill the rows via mysql database.

This is a view of what I have done:

enter image description here

This names that you saw are coming from my database and the PLAY ImageView is from my layout list that appears in each row(when I add a name into my database).

Now my problem is when I set a click listener to that play button, (purpose is switching it into pause ImageView and etc.) It changes another play buttons from another rows too,but not all of them. Hence I think I'm not getting the position, right? Can you help me with this please!

This is my Fragment :

public class XXXUpdatesFragment extends Fragment implements AsyncResponse
,AdapterView.OnItemClickListener{

    View view ;
    private ArrayList<Product> productList;
    private ListView lvProduct;
    private FunDapter<Product> adapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.xxx_updates_layout, container, false);
        ImageLoader.getInstance().init(UILConfig.config(getActivity()));

        lvProduct = (ListView)view.findViewById(R.id.lvProduct);

        PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this);

        taskRead.execute("http://symphonyrecords.6te.net/product.php");

        return view;
    } //onCreate


    @Override
    public void  processFinish(String s) {
        productList = new JsonConverter<Product>().toArrayList(s, Product.class);

        BindDictionary<Product> dict = new BindDictionary<Product>();
        dict.addStringField(R.id.tvName, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product product, int position) {
                return product.name;
            }
        });

        dict.addStringField(R.id.tvDescription, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product product, int position) {
                return product.description;   
            }
        });

        dict.addStringField(R.id.tvQty, new StringExtractor<Product>() {
            @Override
            public String getStringValue(Product product, int position) {
                return "" + product.qty;
            }
        });


        adapter = new FunDapter<>(getActivity(), productList, R.layout.d_layout_list_d, dict);


        lvProduct.setAdapter(adapter);
        lvProduct.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Product selectedProduct = productList.get(position);
        Toast.makeText(getActivity(),selectedProduct.name+"", Toast.LENGTH_SHORT).show();
       }


    }

This is my Product class :

public class Product implements Serializable {
@SerializedName("pid")
public int pid;

@SerializedName("name")
public String name;

@SerializedName("qty")
public int qty;

@SerializedName("price")
public String description;
}

And this is my Adapter :

public class FunDapter<T> extends BaseAdapter implements Filterable {



private boolean paused = true;


protected List<T> mDataItems;
protected List<T> mOrigDataItems;
protected LongExtractor<T> idExtractor;
protected final Context mContext;
private final int mLayoutResource;
private final BindDictionary<T> mBindDictionary;
private int oddColorRes;
private int evenColorRes;
private FunDapterFilter<T> funDapterFilter;
private Filter mFilter;


public FunDapter(Context context, List<T> dataItems, int layoutResource,
                 BindDictionary<T> dictionary) {
  this(context, dataItems, layoutResource, null, dictionary);
}

public FunDapter(Context context, List<T> dataItems, int layoutResource,
                 LongExtractor<T> idExtractor, BindDictionary<T> dictionary) {
    this.mContext = context;
    this.mDataItems = dataItems;
    this.mOrigDataItems = dataItems;
    this.mLayoutResource = layoutResource;
    this.idExtractor = idExtractor;
    this.mBindDictionary = dictionary;

}

public void updateData(List<T> dataItems) {
    this.mDataItems = dataItems;
    this.mOrigDataItems = dataItems;
    notifyDataSetChanged();
}

@Override
public int getCount() {
    if (mDataItems == null || mBindDictionary == null) return 0;

    return mDataItems.size();
}

@Override
public T getItem(int position) {
    return mDataItems.get(position);
}

@Override
public boolean hasStableIds() {
    if(idExtractor == null) return super.hasStableIds();
    else return true;
}

@Override
public long getItemId(int position) {
    if(idExtractor == null) return position;
    else return idExtractor.getLongValue(getItem(position), position);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    final GenericViewHolder holder;
    if (null == v) {
        LayoutInflater vi =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(mLayoutResource, null);
        holder = new GenericViewHolder();
        holder.root = v;


        holder.playPause=(ImageView)v.findViewById(R.id.playPause); 
        holder.playPause.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                        if (paused) {
                            holder.playPause.setImageResource(R.drawable.ic_pause);
                            paused=false;
                        }
                        else {
                            paused = true;
                            holder.playPause.setImageResource(R.drawable.ic_play);
                        }

            }});



        FunDapterUtils.initViews(v, holder, mBindDictionary);

        v.setTag(holder);


    } else {
        holder = (GenericViewHolder) v.getTag();
    }

    final T item = getItem(position);
    showData(item, holder, position);

    return v;
}


private void showData(T item, GenericViewHolder holder, int position) {

    if (oddColorRes > 0 && evenColorRes > 0) {
        if (position % 2 == 0) {
            holder.root.setBackgroundColor(mContext.getResources().getColor(evenColorRes));
        } else {
            holder.root.setBackgroundColor(mContext.getResources().getColor(oddColorRes));
        }
    }

    FunDapterUtils.showData(item, holder, position, mBindDictionary);
}

public FunDapter<T> setAlternatingBackground(int oddColorRes, int evenColorRes) {

    if (oddColorRes <= 0 || evenColorRes <= 0) {
        throw new IllegalArgumentException("Color parameters are illegal");
    }

    this.oddColorRes = oddColorRes;
    this.evenColorRes = evenColorRes;

    return this;
}

public void setIdExtractor(LongExtractor<T> idExtractor) {
    this.idExtractor = idExtractor;
}

@Override
public Filter getFilter() {
    return mFilter;
}

public void initFilter(FunDapterFilter<T> filter) {

    if (filter == null)
        throw new IllegalArgumentException("Cannot pass a null filter to FunDapter");

    this.funDapterFilter = filter;

    mFilter = new Filter() {

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            List<T> list = (List<T>) results.values;

            if (results.count == 0) {
                resetData();
            } else {
                mDataItems = list;
            }

            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {

                results.values = mOrigDataItems;
                results.count = mOrigDataItems.size();
            } else {

                List<T> filter =
                        funDapterFilter.filter(constraint.toString(), mOrigDataItems);

                results.count = filter.size();
                results.values = filter;

            }

            return results;
          }
      };
  }

  public void resetData() {
      mDataItems = mOrigDataItems;
     }

  }

It has been a week that I'm struggling with this, I would be so grateful if you help me with this.

Mehran
  • 481
  • 1
  • 9
  • 26
  • 1
    check this http://stackoverflow.com/questions/20611123/listview-subobject-clickable-confilct if it helps – Raghunandan Feb 11 '17 at 17:59
  • 1
    @rabhis answer will likely work (and may be the fastest way to solve your problem) but the answer in Raghunandan's link would be a better way to do it. – Gary99 Feb 11 '17 at 19:49

1 Answers1

0
    holder.playPause=(ImageView)v.findViewById(R.id.playPause); 
    holder.playPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

                    if (paused) {
                        holder.playPause.setImageResource(R.drawable.ic_pause);
                        paused=false;
                    }
                    else {
                        paused = true;
                        holder.playPause.setImageResource(R.drawable.ic_play);
                    }

        }});

This block must appear in both if and else condition. Now the listeners are correctly attached only when view is created and code fails to attach the listeners when view is reused.

    if (v==null) {
        //that code
    } else {
        //that code 
    }
rabhis
  • 440
  • 2
  • 5