Adding image to listview after listview item click
<p>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. </p>

<p>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.</p>

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

<p>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.</p>

<p>Below is the xml stating listView items</p>

<p>method_item.xml</p>

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

    &lt;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" /&gt;

    &lt;ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true" /&gt;

    &lt;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" /&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p>Below is the part of class snippet:</p>

<pre><code>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&lt;?&gt; 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);


        }
    });
}
</code></pre>

<p>Initially all tick[i] value is set to false.</p>

<p>Below is the getView function of adapter class</p>

<pre><code>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;
}
</code></pre>
