カスタムアダプタのコントロールの焦点を「匿名の内部クラス」で巧みに取得
2398 ワード
昨日フォーラムの中で1人がこのような問題を聞いたのを見て、ちょうど以前私もこの問題に出会ったことがあって、よく考えてみると、たぶん2つの解決方法がみんなに分かち合います.
アイデア1はgetViewの方法でパラメータpositionの値を必要な方法体にどのように伝えるかです.例えばアダプタにはTextViewとbuttonが入っていますが、今buttonをクリックしたいです.
の場合は、Toastでダイナミックに表示されます.それはgetViewでbuttonを見つけてbuttonに自分でカスタマイズしたアダプタを登録し、positionをパラメータとして送ればOKです.
アイデア1はgetViewの方法でパラメータpositionの値を必要な方法体にどのように伝えるかです.例えばアダプタにはTextViewとbuttonが入っていますが、今buttonをクリックしたいです.
の場合は、Toastでダイナミックに表示されます.それはgetViewでbuttonを見つけてbuttonに自分でカスタマイズしたアダプタを登録し、positionをパラメータとして送ればOKです.
public class MyOrder_ListViewAdapter extends SimpleAdapter {
private Context mcontext;
private LayoutInflater mInflater;
public List<HashMap<String, Object>> list;
private int layoutID;
private String flag[];
private int ItemIDs[];
private List<Order> orderlists;
Button mymenu_add_button;
Button mymenu_delete_button;
private Handler handler;
public MyOrder_ListViewAdapter(Context context,
List<? extends Map<String, ?>> list2, int resource, String[] from,
int[] to, List<Order> orderlists, Handler handler) {
super(context, list2, resource, from, to);
mInflater = LayoutInflater.from(context);
list = (List<HashMap<String, Object>>) list2;
layoutID = resource;
flag = from;
ItemIDs = to;
mcontext = context;
this.orderlists = orderlists;
this.handler=handler;
// TODO Auto-generated constructor stub
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(R.layout.mymenu_list_item, null);
mymenu_add_button= (Button) convertView.findViewById(R.id.mymenu_add_btn);
mymenu_add_button.setOnClickListener(new myButtonListener(position));
return convertView;
}
class myButtonListener implements OnClickListener {
private int position;
GridViewButtonListener(int pos) {
position = pos;
}
public void onClick(View v) {
String dishnamegot = (String) ((HashMap<String, Object>) getItem(position))
.get("TextViewd key ");
}
}
}