1
votes

I have the problem that when i scrolling down the listview the hidden images shown...how this problem solved...?? Thanks in advance... enter image description here and this is my code:- @Override public View getView(final int position, View convertView, ViewGroup parent) {

            View v = convertView;

            if(v==null)
            {
                LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=li.inflate(R.layout.custom_row,parent,false);

            }

            TextView Title = (TextView) v.findViewById(R.id.custom_text);
            final RelativeLayout content = (RelativeLayout) v.findViewById(R.id.main);
            final ImageView img1 =(ImageView) v.findViewById(R.id.cust_img);

final BinForAll listitem = mList.get(position);


            Title.setText(listitem.getTxt());

            //Desc.setText(listitem.getDesc());
            content.setOnClickListener(new OnClickListener()
            {

                public void onClick(View arg0) 
                {
                    if(img1.getVisibility()==View.VISIBLE && count>=0)
                    {
                        count++;
                        img1.setVisibility(View.GONE);
                        m1.remove(mList.get(position).getTxt());
                        Log.d("remove", mList.get(position).getTxt());
                    }
                    else if(img1.getVisibility()==View.GONE && count>0)
                    {
                        count--;
                        img1.setVisibility(View.VISIBLE);
                        m1.add(mList.get(position).getTxt());
                        Log.d("add", mList.get(position).getTxt());
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "You can not add more than 10 values..", 1).show();
                    }

            }
        });
2

2 Answers

1
votes

ListView always reuses views. So you should check that the view is in appropriate state each time you prepare the view for the ListView via Adapter.getView(final int position, View convertView, ViewGroup parent).

In order to solve your problem you can persist visibility state in your final BinForAll listitem = mList.get(position); object (just add boolean feald to the BinForAll class) and rewrite your getView method as following:

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

        View v = convertView;

        if(v==null)
        {
            LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v=li.inflate(R.layout.custom_row,parent,false);

        }

        TextView Title = (TextView) v.findViewById(R.id.custom_text);
        final RelativeLayout content = (RelativeLayout) v.findViewById(R.id.main);
        final ImageView img1 =(ImageView) v.findViewById(R.id.cust_img);

        final BinForAll listitem = mList.get(position);


        Title.setText(listitem.getTxt());
        // each time set visibility for your image view
        img1.setVisibility(listitem.isVisible() ? View.VISIBLE : View.GONE);

        //Desc.setText(listitem.getDesc());
        content.setOnClickListener(new OnClickListener()
        {

            public void onClick(View arg0) 
            {
                if(img1.getVisibility()==View.VISIBLE && count>=0)
                {
                    count++;
                    img1.setVisibility(View.GONE);
                    // save visibility state
                    listitem.setVisible(false);
                    m1.remove(mList.get(position).getTxt());
                    Log.d("remove", mList.get(position).getTxt());
                }
                else if(img1.getVisibility()==View.GONE && count>0)
                {
                    count--;
                    img1.setVisibility(View.VISIBLE);
                    // save visibility state
                    listitem.setVisible(true);
                    m1.add(mList.get(position).getTxt());
                    Log.d("add", mList.get(position).getTxt());
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "You can not add more than 10 values..", 1).show();
                }

        }
    });
0
votes

Add this attributes to your listView

    android:scrollingCache="false"
    android:persistentDrawingCache="scrolling"
    android:fastScrollEnabled="true"
    android:cacheColorHint="@android:color/transparent"