: ListAdapter > getView에서 객체 할당시 Button 객체 onClick Event를 넘겨받기 위해 활용
1. Interface > ListItemOnClickListener
public interface ListItemOnClickListener {
public abstract void onClick(Bundle b);
}
public abstract void onClick(Bundle b);
}
2. ListAdapter
public class GiftListViewAdapter extends ArrayAdapter<GiftListViewItem>{
ListItemOnClickListener itemOnClickListener;
private Context mContext;
public GiftListViewAdapter(Context context, List<GiftListViewItem> items, ListItemOnClickListener lsnr) {
super(context, R.layout.row_gifty_fragment, items);
this.itemOnClickListener = lsnr;
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_gifty_fragment, parent, false);
viewHolder = new ViewHolder();
viewHolder.detailTextView = (TextView)convertView.findViewById(R.id.textView1);
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageView1);
viewHolder.detailTextView2 = (TextView)convertView.findViewById(R.id.textView2);
viewHolder.exchangeButton = (Button)convertView.findViewById(R.id.button1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
} //end if______________
final GiftListViewItem item = getItem(position);
Bitmap bm = item.getImageBitmap();
viewHolder.imageView.setImageBitmap(bm);
viewHolder.detailTextView.setText(item.getDetailText());
viewHolder.detailTextView2.setTextColor(Color.GRAY);
viewHolder.detailTextView2.setText(item.getDetailText2());
viewHolder.exchangeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bundle b = new Bundle();
b.putString("itemCode", item.getItemCode());
itemOnClickListener.onClick(b);
}
});
return convertView;
}
private static class ViewHolder{
ImageView imageView;
TextView detailTextView;
TextView detailTextView2;
Button exchangeButton;
}
}
ListItemOnClickListener itemOnClickListener;
private Context mContext;
public GiftListViewAdapter(Context context, List<GiftListViewItem> items, ListItemOnClickListener lsnr) {
super(context, R.layout.row_gifty_fragment, items);
this.itemOnClickListener = lsnr;
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_gifty_fragment, parent, false);
viewHolder = new ViewHolder();
viewHolder.detailTextView = (TextView)convertView.findViewById(R.id.textView1);
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageView1);
viewHolder.detailTextView2 = (TextView)convertView.findViewById(R.id.textView2);
viewHolder.exchangeButton = (Button)convertView.findViewById(R.id.button1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
} //end if______________
final GiftListViewItem item = getItem(position);
Bitmap bm = item.getImageBitmap();
viewHolder.imageView.setImageBitmap(bm);
viewHolder.detailTextView.setText(item.getDetailText());
viewHolder.detailTextView2.setTextColor(Color.GRAY);
viewHolder.detailTextView2.setText(item.getDetailText2());
viewHolder.exchangeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bundle b = new Bundle();
b.putString("itemCode", item.getItemCode());
itemOnClickListener.onClick(b);
}
});
return convertView;
}
private static class ViewHolder{
ImageView imageView;
TextView detailTextView;
TextView detailTextView2;
Button exchangeButton;
}
}
3. Activity
.........
private class CustomAsyncTask extends AsyncTask<Void,Void,Result>{
....
@Override
protected void onPostExecute(RequestResult result) {// TODO Auto-generated method stub
super.onPostExecute(result);
if(this.loadingDialog != null && loadingDialog.isShowing()) {
loadingDialog.dismiss();
}
final GiftListViewAdapter adapter = new GiftListViewAdapter(getActivity(), mItems,
new ListItemOnClickListener() {
@Override
public void onClick(Bundle b) {
if(User.isLogined(getActivity())){
String itemCode = b.getString("itemCode");
Intent intent = new Intent(getActivity(), ActGiftContent.class);
intent.putExtra("itemCode", itemCode);
startActivity(intent);
} else {
MsgBox.showToast(getActivity(), "로그인을 하셔야 이용가능합니다. ");
MainActivity mainAct = (MainActivity)getActivity();
mainAct.callLoginFragment();
}
}
});
} //end method____
댓글 없음:
댓글 쓰기