Java 获取自定义SimpleLayout中的ListItem位置
我有一个自定义的Java 获取自定义SimpleLayout中的ListItem位置,java,android,listview,android-layout,android-adapter,Java,Android,Listview,Android Layout,Android Adapter,我有一个自定义的列表视图,带有文本视图和复选框。我还有一个自定义的simpledapter,在其中我覆盖了getView()方法,并且能够检索对TextView和复选框所做的更改。我的问题是,我不知道如何在OnCheckedChanged或OnClick中获得正确的单击ListItem或复选框 更新添加了整个自定义适配器类: public class CustomAdapter extends SimpleAdapter { private LayoutInflater mInflate
列表视图
,带有文本视图
和复选框
。我还有一个自定义的simpledapter
,在其中我覆盖了getView()
方法,并且能够检索对TextView
和复选框所做的更改。我的问题是,我不知道如何在OnCheckedChanged
或OnClick
中获得正确的单击ListItem
或复选框
更新添加了整个自定义适配器
类:
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private int mPosicion;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
mPosicion = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Posicion",""+mPosicion);//I don't know how to retrieve clicked position
}
});
holder.chkbxEstado.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("Posicion",""+mPosicion);//Or checked
}
});
return convertView;
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
}
}
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private OnClickListener mClick;
private OnCheckedChangeListener mChecked;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
holder.posicion = position; //Add the new position into the holder for each row.
convertView.setTag(holder);
mClick = new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder viewHolder = getViewHolder(v); //Get the ViewHolder for the clicked row.
Log.i("Posicion",""+v.posicion);
}
};
mChecked = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ViewHolder viewHolder = getViewHolder(buttonView); //Get the ViewHolder for the clicked CheckBox
Log.i("Posicion",""+viewHolder.posicion);
}
};
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(mClick);
holder.chkbxEstado.setOnCheckedChangeListener(mChecked);
return convertView;
}
public ViewHolder getViewHolder(View v){ //This method returns the ViewHolder stored in the tag if available, if not it recursively checks the parent's tag.
if(v.getTag() == null){
return getViewHolder((View)v.getParent());
}
return (ViewHolder)v.getTag();
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
int posicion; //Added position attribute to ViewHolder class.
}
}
公共类CustomAdapter扩展了SimpleAdapter{
私人停车场;
私有列表m=null;
私有上下文;
私人投资;
公共CustomAdapter(上下文上下文、列表项、int资源、字符串[]从、int[]到){
超级(上下文、项目、资源、发件人、收件人);
mInflater=LayoutInflater.from(上下文);
mItems=项目;
mContext=上下文;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父级){
视窗座;
位置;
if(convertView==null){
convertView=mInflater.充气(R.layout.custom_row_view,null);
holder=新的ViewHolder();
holder.chkbxEstado=(复选框)convertView.findViewById(R.id.chkbxCompletado);
holder.txtextoagenda=(TextView)convertView.findViewById(R.id.txtextoLista);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.txtextoagenda.setText(mItems.get(position.get(“description”));
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
Log.i(“Posicion”,“”+mPosicion);//我不知道如何检索单击的位置
}
});
holder.chkbxestato.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
Log.i(“Posicion”,“”+mPosicion);//或选中
}
});
返回视图;
}
私有静态类视图持有者{
TextView txtTextoAgenda;
复选框chkbxEstado;
}
}
非常感谢您的帮助。我找到了解决方案。如果有人知道更好的,请告诉我。工作CustomAdapter
类:
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private int mPosicion;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
mPosicion = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Posicion",""+mPosicion);//I don't know how to retrieve clicked position
}
});
holder.chkbxEstado.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("Posicion",""+mPosicion);//Or checked
}
});
return convertView;
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
}
}
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private OnClickListener mClick;
private OnCheckedChangeListener mChecked;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
holder.posicion = position; //Add the new position into the holder for each row.
convertView.setTag(holder);
mClick = new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder viewHolder = getViewHolder(v); //Get the ViewHolder for the clicked row.
Log.i("Posicion",""+v.posicion);
}
};
mChecked = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ViewHolder viewHolder = getViewHolder(buttonView); //Get the ViewHolder for the clicked CheckBox
Log.i("Posicion",""+viewHolder.posicion);
}
};
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(mClick);
holder.chkbxEstado.setOnCheckedChangeListener(mChecked);
return convertView;
}
public ViewHolder getViewHolder(View v){ //This method returns the ViewHolder stored in the tag if available, if not it recursively checks the parent's tag.
if(v.getTag() == null){
return getViewHolder((View)v.getParent());
}
return (ViewHolder)v.getTag();
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
int posicion; //Added position attribute to ViewHolder class.
}
}
公共类CustomAdapter扩展了SimpleAdapter{
私人停车场;
私有列表m=null;
私有上下文;
私有onclick侦听器mClick;
private OnCheckedChangeListener McChecked;
公共CustomAdapter(上下文上下文、列表项、int资源、字符串[]从、int[]到){
超级(上下文、项目、资源、发件人、收件人);
mInflater=LayoutInflater.from(上下文);
mItems=项目;
mContext=上下文;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父级){
视窗座;
if(convertView==null){
convertView=mInflater.充气(R.layout.custom_row_view,null);
holder=新的ViewHolder();
holder.chkbxEstado=(复选框)convertView.findViewById(R.id.chkbxCompletado);
holder.txtextoagenda=(TextView)convertView.findViewById(R.id.txtextoLista);
holder.posicion=position;//将每行的新位置添加到holder中。
convertView.setTag(支架);
mClick=newonclicklistener(){
@凌驾
公共void onClick(视图v){
ViewHolder ViewHolder=getViewHolder(v);//获取单击行的ViewHolder。
Log.i(“Posicion”,“v.Posicion”);
}
};
mChecked=newoncheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
ViewHolder ViewHolder=getViewHolder(buttonView);//获取已单击复选框的ViewHolder
Log.i(“Posicion”,即“+viewHolder.Posicion”);
}
};
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.txtextoagenda.setText(mItems.get(position.get(“description”));
setOnClickListener(mClick);
holder.chkbxestato.setOnCheckedChangeListener(mChecked);
返回视图;
}
public ViewHolder getViewHolder(View v){//此方法返回存储在标记中的ViewHolder(如果可用),如果不可用,则递归检查父标记。
if(v.getTag()==null){
返回getViewHolder((视图)v.getParent());
}
return(ViewHolder)v.getTag();
}
私有静态类视图持有者{
TextView txtTextoAgenda;
复选框chkbxEstado;
int posicion;//向ViewHolder类添加了位置属性。
}
}
编辑以重新使用onClickListener()和onCheckedChangedListener()实例。OP的解决方案有效。但是,如果您正在扩展CursorAdapter,则newView()和bindView()中不存在position参数。怎么办
它们确实提供了一个光标。使用cursor.getPosition()。它做同样的事情。您有参数位置,还需要什么?如果我没有正确解释,很抱歉。position参数始终返回某个位置,但如果单击不同的行或复选框,则不会更新。这很奇怪,您可以发布整个适配器吗?使用CustomAdapter
更新。仍然不清楚是什么