선택 큐를 원한다면 초점을 맞출 개체가 필요하다고 생각합니다. 그러나 포커스가있는 개체 (예 : Button)를 사용하면 GridView에 OnItemClickListener를 연결해도 작동하지 않습니다 (올바르게 기억하는 경우). 오히려 어댑터의 getView()에서 각 항목에 OnClickListener를 개별적으로 첨부해야합니다.
어댑터 :
활동 측면에서
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
Button button;
if (convertView == null) { // if it's not recycled, initialize some attributes
button = new Button(mContext);
// set layout params (make sure its GridView.layoutParams)
// and other stuff
}
else {
button = (Button) convertView;
}
button.setBackgroundResource(mThumbIds[position]); // mThumbIds hold Resource Ids
button.setOnClickListener(new OnClickListener() {
onClick(View v) {
// store directly to database here, or send it with the activity with sharedPreferences (below)
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences("MY_PREFERENCE", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("button_id", mThumbIds[position]);
// Commit the edits!
editor.commit();
}
});
return button;
}
}
, 저장 버튼 OnClickListener를하십시오 버튼 포커스입니다,하지만 난이 어떻게해야한다고 생각하기 때문에이 누락 사항이있을 수 있습니다
onClick(View v) {
// Restore preferences
SharedPreferences settings = getSharedPreferences("MY_PREFERENCE", 0);
int id = settings.getInt("button_id", -1);
// now safe all stuff to database
}
. 또한 .xml 정의 된 선택 자 리소스를 사용하여 선택을 수행합니다. 그러나 그것은 별도의 질문으로 다루어 져야한다.
편집 1 : 사실 이제는 내가 생각하기에 드로어 블 .xml (선택기)에 ID가있을 수 있는지 확실하지 않습니다. 나는 나중에 집에서 이것을 구현해야하고 그것을 시도해야 할 것이다.
편집 2 : sharedPreference의 추가 활동 측면 질의 : 나는 부분
편집 3 sharedPreference을 추가했습니다.
출처
2011-09-23 16:42:40
Ian
이것에 대한 답. 나는 당신이 어떤 종류의 선택 큐를 원한다면, 당신은 focusable 오브젝트가 필요하다고 믿습니다. 그러나 포커스가있는 개체를 사용하면 OnItemClickListener를 GridView에 첨부해도 작동하지 않습니다. 오히려 어댑터의 getView()에서 각 항목에 OnClickListener를 개별적으로 첨부해야합니다. – Ian
각 항목에'OnClickListener'를 붙이는 것이 실제로 가능하지 않다고 생각합니까? 그것은 가능한 해결책 일 수 있습니다. 따라서 그것에 익숙하다면 아래에서 적절한 답을 할 수 있습니다. 나는 상관하지 않을 것이다. –