android. GridView에 12 개의 (3 행, 4 열) 버튼을 만들었고 자습서를 따라 버튼을 누른 후 무언가를 표시하는 축배를 만들었습니다. 버튼이 화면에 올바르게 표시되지만 토스트 메시지는 표시되지 않습니다. 왼쪽 상단 버튼을 누르면 하단 왼쪽 토스트가 나타납니다. 가운데 왼쪽 버튼을 누르면 중간 바로 토스트가됩니다. 왼쪽 아래 버튼을 누르면 위로 올라갑니다. 오른쪽 축배입니다.android : 버튼 어댑터를 사용하여 만든 버튼의 순서가 올바르지 않습니다.
Visually:
button location:
1 2 3 4
5 6 7 8
9 10 11 12
toast message
9 10 11 12
8 7 6 5
4 3 2 1
다음은 어댑터 코드입니다 :
public class KeypadAdapter extends BaseAdapter {
private Context mContext;
// Declare button click listener variable
private OnClickListener mOnButtonClick;
// Method to set button click listener variable
public void setOnButtonClickListener(OnClickListener listener) {
mOnButtonClick = listener;
}
public KeypadAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mButtons.length;
}
public Object getItem(int position) {
return mButtons[position];
}
public long getItemId(int position) {
return 0;
}
private KeypadButtons[] mButtons = { KeypadButtons.ADD,
KeypadButtons.SUBTRACT, KeypadButtons.MULTIPLY,
KeypadButtons.DIVIDE, KeypadButtons.DET, KeypadButtons.INV,
KeypadButtons.POW2, KeypadButtons.POWN, KeypadButtons.TRANSPOSE,
KeypadButtons.NORM1, KeypadButtons.NORM2, KeypadButtons.NORMINF };
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button btn;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
btn = new Button(mContext);
KeypadButtons keypadButton = mButtons[position];
if (keypadButton != KeypadButtons.DUMMY) {
btn.setOnClickListener(mOnButtonClick);
}
// Set CalculatorButton enumeration as tag of the button so that we
// will use this information from our main view to identify what to
// do
btn.setTag(keypadButton);
} else {
btn = (Button) convertView;
}
btn.setText(mButtons[position].getText());
return btn;
}
}
여기가 토스트의 순서가 역전되는 것 같다 처음에는 활동 코드
public class MainActivity extends Activity {
GridView mKeypadGrid;
KeypadAdapter mKeypadAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get reference to the keypad button GridView
mKeypadGrid = (GridView) findViewById(R.id.gridView);
// Create Keypad Adapter
mKeypadAdapter = new KeypadAdapter(this);
// Set adapter of the keypad grid
mKeypadGrid.setAdapter(mKeypadAdapter);
// Set button click listener of the keypad adapter
mKeypadAdapter.setOnButtonClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Button btn = (Button) v;
// Get the KeypadButton value which is used to identify the
// keypad button from the Button's tag
KeypadButtons keypadButton = (KeypadButtons) btn.getTag();
// Process keypad button
ProcessKeypadInput(keypadButton);
}
});
mKeypadGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
}
protected void ProcessKeypadInput(KeypadButtons keypadButton) {
// TODO Auto-generated method stub
Toast.makeText(
MainActivity.this,
keypadButton.getText().toString() + " "
+ keypadButton.toString(), Toast.LENGTH_SHORT).show();
}
}
입니다. 그래서 내가 했어
KeypadButtons keypadButton = mButtons[mButtons.length - 1 - position];
그는 아래 2 개의 열을 고정시켰다. 그러나 그 맨 윗줄은 여전히 바뀐다. 사전에
감사합니다.