1 Replies - 1206 Views - Last Post: 26 July 2012 - 04:15 PM

#1 mavidchar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-November 11

GridView: Re-arrange items

Posted 26 July 2012 - 03:20 PM

I am trying to build a simple calculator and I am using a GridView in the xml layout.
The buttons are created in a custom adapter.

Is there a way to interchange the position of two grid items or change the size of a couple of them?

public class Calculator extends Activity{
	
	public GridView grv;
	
	public String[] numbers = new String[] {"1","2","3", "4", "5","6","7","8","9","0",".", "+","-","/","C","CE","+/-","<--","="};
	
	public List<Button> calcButtons;
	
	
	/*
	 * ------------------------------------------
	 * |       |
	 * |  +/-  | n1 ... 
	 * |       |
	 * ------------------------------------------
	 */
	public StringBuffer calcView;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calculator);
		
		grv = (GridView) findViewById(R.id.grdButtons);
		grv.setAdapter(new ButtonAdapter(this));
		
		calcView = new StringBuffer("");

	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onpause()
	 */
	@Override
	protected void onpause() {
		// TODO Auto-generated method stub
		super.onpause();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onresume()
	 */
	@Override
	protected void onresume() {
		// TODO Auto-generated method stub
		super.onresume();
	}
	
	public class ButtonAdapter extends BaseAdapter implements onclickListener {
		
		private Context mContext;
		
		public ButtonAdapter(Context c) {
			this.mContext = c;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return numbers.length;
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Button btn;
			if (convertView == null) {
				btn = new Button(mContext);
				btn.setLayoutParams(new GridView.LayoutParams(100,55));
				btn.setPadding(8, 8, 8, 8);
			} else {
				btn = (Button) convertView;
			}
			
			btn.setText(numbers[position]);
			btn.setId(position);
			btn.setTag(numbers[position]);
			btn.setonclickListener(this);
			return btn;
		}
		
		/* INTERFACE : onclickListener */
		public void onclick(View v) {
			/*
			 * Button types: numeric, operation
			 * 
			 * {0-9} -> 
			 * {+,-,*,/, C,CE, +/-} ->
			 */
			
			
			
		}
		
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: GridView: Re-arrange items

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: GridView: Re-arrange items

Posted 26 July 2012 - 04:15 PM

Well there is no way to control the size of individual cells. To interchange the position of two items in the gridview just change their position in the array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1