0

I am developing an app which has a listView having 1 textView which displays the content and one imageView. What i want is, when i click the listView i want to set the imageView with the tick mark image which i have. Its working fine. Once i click the listView, the tick mark image is loaded on that listview item on which i clicked.

The problem arises when i scroll. When i scroll, i could see some other listview item down below has a tick mark loaded. not sure how the image was set on that position.

I have read somewhere that when the listView is scrolled, the view is refreshed. Is that causing the problem?

Can anyone help how can iresolve this? I want the image to be shown(loaded) on the listView item on which i clicked and now other listView item.

Below is the xml stating listView items

method_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white"
        android:text="This is a check box button which reacts upon the check that user clicks. I am testing it with the big string and checking how it looks" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="34dp" />

</RelativeLayout>

Below is the part of class snippet:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.method_list_main);

    final ListView list = (ListView)findViewById(R.id.listView1);

    adapter=new MethodLazyAdapter(this,ARRAY.preparationSteps,ARRAY.timeToPrepare,ARRAY.arrowValue,noOfSteps,list);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
             RelativeLayout ll = (RelativeLayout) view;
              ImageView image = (ImageView)ll.findViewById(R.id.imageView2);

              if(tick[position]){
                  tick[position] = false;
                    image.setImageResource(0);                      
              }
              else{
                  tick[position] = true;
                      image.setImageResource(R.drawable.select_right);  
              }

            System.out.println("Position is: "+position);


        }
    });
}

Initially all tick[i] value is set to false.

Below is the getView function of adapter class

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.method_item, null);

    TextView text = (TextView)vi.findViewById(R.id.textView1);
    text.setText(preparationSteps\[position\]);
    return vi;
}
1
  • Can u please paste the code as i am not getting the desired output even after trying ViewHolder Commented Mar 8, 2013 at 20:14

2 Answers 2

2

Try this to get what you want

in public area add this variable

public SparseBooleanArray checked = new SparseBooleanArray();

then in onItemClick

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                boolean stat = checked.get(position, false);
                checked.put(position, !stat);
                adapter.setChecked();
                System.out.println("Position is: "+position);


            }
        });

and in Adapter Class

If adapter not sub class in activiy add variable in public area

private SparseBooleanArray checked = new SparseBooleanArray();

and add this method to class

public void setChecked(SparseBooleanArray ch){
    checked = ch;
   notifyDataSetChanged();
}

Or if it sub class use our cheked variable we defined it up

then in getView method

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.method_item, null);

    TextView text = (TextView)vi.findViewById(R.id.textView1);
    text.setText(preparationSteps\[position\]);

    ImageView image = (ImageView)vi.findViewById(R.id.imageView2);
    if(checked.get(position, false)){
          image.setImageResource(0);                      
    }
    else{
            image.setImageResource(R.drawable.select_right);  
    }
    return vi;
}

Please let me know if this help you.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked.. :) Thanks a ton. Its really appreciable. I was badly stuck with this. Hats off.
0

Check out this post:

android - populating ListView with data from JSONArray

the reason for that is bacouse your are using your converted view wrong. follow the example code on the post i paste.

if you will inflate your row view every time this problem will disappear. but this implementation is not recommended.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.