안드로이드 개발 질문/답변
(글 수 45,052)
커스텀리스트뷰를 이용해 이미지와 텍스트를 보여주는 것을 구현하고 있습니다
그리고 이미지를 터치하면 어딘가에서 예제로 구한 퀵액션바를 실행하기 위해 onClickListener를 주고 싶은데
리스트뷰의 각 행이 또다른 xml로 정의되어 있어서 이것을 가져오는게 잘 되지 않습니다
질문전에 검색을 좀 해보았는데 findViewById는 OnCreate안에서 정의된 뷰들만 제어가 가능하고
차일드뷰같은 경우엔 inflater를 사용해야 하는것 같은데 사용방법을 잘 모르겠습니다.
아예 커스텀어탭터에서 OnClickListener를 주면 작동은 하지만 퀵액션바를 어탭터에선 적용을 못하는건지
부족한 프로그래밍 실력이라 잘 안됩니다
처음 써보는글이라 코드가 하나로 이어지지 않았는데 실제 적용부분은 마지막 코드블럭입니다..
그리고 죄송하지만 가능하시다면 말보단 코드로 어떻게 적용하는지 알려주시면 감사하겠습니다
mSNSList = new ArrayList<SNS_Info>(); mAdapter = new SNS_Adapter(this, R.layout.list_view_row_mypage, mSNSList); mLvSNS = (ListView)findViewById(R.id.custom_list_view_example_activity_lv_list); mLvSNS.setAdapter(mAdapter); //더미 데이터 호출 getSNS(); setListView(); // mLvSNS.setOnItemClickListener(mItemClickListener);//리스트뷰 클릭 리스너 등록 mLvSNS.setBackgroundColor(Color.GRAY);
/*QuickActionBar*/ ActionItem nextItem = new ActionItem(ID_DOWN, "Next", getResources().getDrawable(R.drawable.menu_down_arrow)); ActionItem prevItem = new ActionItem(ID_UP, "Prev", getResources().getDrawable(R.drawable.menu_up_arrow)); ActionItem searchItem = new ActionItem(ID_SEARCH, "전화", getResources().getDrawable(R.drawable.menu_search)); ActionItem infoItem = new ActionItem(ID_INFO, "문자", getResources().getDrawable(R.drawable.menu_info)); ActionItem eraseItem = new ActionItem(ID_ERASE, "채팅", getResources().getDrawable(R.drawable.menu_eraser)); ActionItem okItem = new ActionItem(ID_OK, "쪽지", getResources().getDrawable(R.drawable.menu_ok));
//use setSticky(true) to disable QuickAction dialog being dismissed after an item is clicked prevItem.setSticky(true); nextItem.setSticky(true);
//create QuickAction. Use QuickAction.VERTICAL or QuickAction.HORIZONTAL param to define layout //orientation final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);
//add action items into QuickAction quickAction.addActionItem(nextItem); quickAction.addActionItem(prevItem); quickAction.addActionItem(searchItem); quickAction.addActionItem(infoItem); quickAction.addActionItem(eraseItem); quickAction.addActionItem(okItem);
//Set listener for action item clicked
quickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
@Override
public void onItemClick(QuickAction source, int pos, int actionId) {
ActionItem actionItem = quickAction.getActionItem(pos); //here we can filter which action item was clicked with pos or actionId parameter
if (actionId == ID_SEARCH) {
// Toast.makeText(getApplicationContext(), "Let's do some search action", Toast.LENGTH_SHORT).show();
Uri number;
number = Uri.parse("tel:" + mNumber);
Intent intent_call = new Intent(Intent.ACTION_DIAL, number);
startActivity(intent_call);
} else if (actionId == ID_INFO) {
// Toast.makeText(getApplicationContext(), "I have no info this time", Toast.LENGTH_SHORT).show();
Intent intent_sms = new Intent(Intent.ACTION_SENDTO);
intent_sms.setData(Uri.parse("smsto:" + mNumber));
intent_sms.putExtra("sms_body", "");
startActivity(intent_sms);
} else if (actionId == ID_ERASE){
// Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
Intent intent_chat = new Intent(getApplicationContext(), Commerce_Chat.class);
intent_chat.putExtra("temp_id", temp_id); //Intent간 통신
intent_chat.putExtra("temp_pw", temp_pw); //
startActivity(intent_chat);
} else if (actionId == ID_OK){
// Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
Intent intent_chat = new Intent(getApplicationContext(), Letter.class);
intent_chat.putExtra("temp_id", temp_id); //Intent간 통신
intent_chat.putExtra("temp_pw", temp_pw); //
startActivity(intent_chat);
}
}
}); //set listnener for on dismiss event, this listener will be called only if QuickAction dialog was dismissed
//by clicking the area outside the dialog.
quickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
@Override
public void onDismiss() {
// Toast.makeText(getApplicationContext(), "Dismissed", Toast.LENGTH_SHORT).show();
}
}); layout = new LinearLayout(mLvSNS.getContext());
LayoutInflater inflater = (LayoutInflater)mLvSNS.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = (View)inflater.inflate(R.layout.list_view_row_mypage, layout);
//View v = (View)inflater.inflate(R.layout.list_view_row_mypage, null);
ImageView iv = (ImageView)v.findViewById(R.id.list_view_row_profile_thumbnail);
iv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "ser", Toast.LENGTH_SHORT).show();
quickAction.show(v);
}
});
/*/QuickActionBar*/
((TextView)findViewById(R.id.content_date)).setText(seller_date);
((TextView)findViewById(R.id.content_subject)).setText(seller_id);
((TextView)findViewById(R.id.content_description)).setText(seller_commnet);
});
2011.11.10 19:31:44
AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
public void onItemClick(AdapterView parent, View view, int position, long id) {
try {
JSONArray array = new JSONObject(mResult).getJSONArray("results");
JSONObject object = array.getJSONObject(position);
Intent intent1 = new Intent(Mypage.this, Mypage_Content.class);
intent1.putExtra("temp_id", temp_id); //Intent간 통신
intent1.putExtra("temp_pw", temp_pw); //
intent1.putExtra("seller_id", object.getString("id"));
intent1.putExtra("seller_date", object.getString("time"));
intent1.putExtra("seller_comment", object.getString("comment"));
intent1.putExtra("seller_image", object.getString("image"));
startActivity(intent1);
} catch (JSONException e) {e.printStackTrace();}
}
};OnClickListener를 따로 만들라는건 이걸 말슴하시는건지요 ?
그러면 어댑터에선 어떻게 설정해야하는건가요..?
그리고 이건 리스트자체가 클릭시 실행되어지고 있는데 이건 메인(부모)에서 선언한것인데 잘 되거든요..
안된다는건 어느 경우에 안되는건지도 알수있을까요..?
프로그래밍을 잘 못해서 코드를 어떻게 줘야 하는지도 잘 모르겠네요
예제 처럼 알려주시면 감사하겠습니다




리스트뷰의 한 item 안에 있는 한 depth 아래의 뷰에 대한 이벤트 처리 문제 인것같네요.
onClickListener 을 따로 미리 생성해 놓으시고, adapter 에서는 inflate 시에 setOnClickListener 해주시면 됩니다.
이벤트를 받을 뷰의 부모에게는 이벤트 거시면 안됩니다. 그리고, listview 자체에도 이벤트 거시면 안됩니다.