Swapping two rows in ArrayAdapter
<p>I've created a ListView that in each row has a button with UP and DOWN arrow. Pressing these buttons makes the row to be shifted one position up or down. </p>

<p>I've achieved it by implementing OnClickListener for both buttons in a the override method getView. It works as it should however I fill bad with that cuz it seems to be highly memory consuming and lots of code is doubled.</p>

<pre><code>@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = inflater.inflate(R.layout.row_layout, parent, false);

    TextView textView = (TextView) rowView.findViewById(R.id.label);
    CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.checkbox);
    checkBoxes.add(position, checkBox);

    String address = this.getItem(position).getAddress();
    String tokenizedAddress = tokenizeAddress(address);

    textView.setText(tokenizedAddress);

    ImageButton buttonUp = (ImageButton)rowView.findViewById(R.id.button_up);
    ImageButton buttonDown = (ImageButton)rowView.findViewById(R.id.button_down);

    buttonUp.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            ListAdapter adapter = ListAdapter.this;

            if(position != 0 ){
                GameTask current = adapter.getItem(position);

                ArrayList&lt;GameTask&gt; list = new ArrayList&lt;GameTask&gt;();
                for( int i = 0; i &lt; adapter.getCount(); i++ )
                    list.add(adapter.getItem(i));
                list.remove(position);
                list.add(position-1, current);
                adapter.clear();
                for( int i = 0; i &lt; list.size(); i++ ){
                    adapter.add(list.get(i));
                }
                adapter.notifyDataSetChanged();
            }

        }
    });

    buttonDown.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            ListAdapter adapter = ListAdapter.this;

            if(position != adapter.getCount()-1 ){
                GameTask current = adapter.getItem(position);

                ArrayList&lt;GameTask&gt; list = new ArrayList&lt;GameTask&gt;();
                for( int i = 0; i &lt; adapter.getCount(); i++ )
                    list.add(adapter.getItem(i));
                list.remove(position);
                list.add(position+1, current);
                adapter.clear();
                for( int i = 0; i &lt; list.size(); i++ ){
                    adapter.add(list.get(i));
                }
                adapter.notifyDataSetChanged();
            }

        }
    });


    return rowView;
}
</code></pre>

<p>Both listeners do almost the same, the only difference is the condition and the value of shifting +1/-1. I was wondering about creating the inner class implementing OnClickListener in the extended ArrayAdapter class however, I have no idea, how I could then pass the position of the row clicked to this inner class.</p>
