myListActivity 和 myListAdapter 的控件传值有关问题
myListActivity 和 myListAdapter 的控件传值问题。
myListActivity 继承于 ListActivity
myListAdapter 继承于 BaseAdapter
myListAdapter 收集的数据存入 List<myLatlon> lml, 然后将lml递交个myListAdapter显示,
部分代码如下:
我的问题是:当CheckBox点中时,如何将其值更新到lml的对应项中?
------解决思路----------------------
注册一个点击事件监听器到CheckBox就可以了。
------解决思路----------------------
一般做法是
自定义一个Listener
比如
public interface OnCheckBoxClickListener {
public void onClick(CompoundButton buttonView,
boolean isChecked);
}
并在Activity中 实现它, 在new Adapter 的时候传实例进去
在你的39行直接调用这个实例的 onClick(...);
------解决思路----------------------
在CheckBox上注册一个OnCheckBoxClickListener 监听器,然后在事件函数里修改数据,然后调用adapter的NotifyDatasetChanged函数。
------解决思路----------------------
可以在adapter构造函数那 加个listener参数 然后再adapter里面声明这接口 在activity里实现这listener 即2L的做法
------解决思路----------------------
写错了函数名,应该是notifyDataSetChanged
myListActivity 继承于 ListActivity
myListAdapter 继承于 BaseAdapter
myListAdapter 收集的数据存入 List<myLatlon> lml, 然后将lml递交个myListAdapter显示,
部分代码如下:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.logs_list, null);
holder = new ViewHolder();
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.lat = (TextView) convertView.findViewById(R.id.lat);
holder.lon = (TextView) convertView.findViewById(R.id.lon);
holder.select = (CheckBox) convertView.findViewById(R.id.select);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
myLatlon ml = lml.get(position);
int d = ml.getDate();
int m = d / 100;
int y = m / 100;
int s = ml.getTime();
int n = s / 100;
int h = n / 100;
holder.date.setText(String.format("%02d/%02d/%02d", y-2000, m%100, d%100));
holder.time.setText(String.format("%02d:%02d:%02d", h, n%100, s%100));
holder.lat.setText(String.format("%.6f", (double)ml.getLat()/1E6));
holder.lon.setText(String.format("%.6f", (double)ml.getLon()/1E6));
holder.select.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
return convertView;
}
private class ViewHolder
{
CheckBox select;
TextView date;
TextView time;
TextView lat;
TextView lon;
}
我的问题是:当CheckBox点中时,如何将其值更新到lml的对应项中?
------解决思路----------------------
注册一个点击事件监听器到CheckBox就可以了。
------解决思路----------------------
一般做法是
自定义一个Listener
比如
public interface OnCheckBoxClickListener {
public void onClick(CompoundButton buttonView,
boolean isChecked);
}
并在Activity中 实现它, 在new Adapter 的时候传实例进去
在你的39行直接调用这个实例的 onClick(...);
------解决思路----------------------
在CheckBox上注册一个OnCheckBoxClickListener 监听器,然后在事件函数里修改数据,然后调用adapter的NotifyDatasetChanged函数。
------解决思路----------------------
可以在adapter构造函数那 加个listener参数 然后再adapter里面声明这接口 在activity里实现这listener 即2L的做法
------解决思路----------------------
写错了函数名,应该是notifyDataSetChanged