我正在开发一个应用程序,它显示带有复选框
的listView
。当我滚动列表查看
复选框
的值被清除时。
下面是我的代码
适配器类别:
public class Conference_listViewAdapter extends BaseAdapter {
Activity context;
LayoutInflater inflater;
public static List<Model> data = null;
ArrayList<String> id_list = new ArrayList<String>();
ViewHolder holder;
int pos;
public Conference_listViewAdapter(Activity context,List<Model> list, ArrayList<String> id_list) {
data = list;
this.id_list = id_list;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position).getName();
}
@Override
public long getItemId(int position) {
return Long.valueOf(id_list.get(position));
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.people_list_item1, null);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.tv_name);
holder.image = (ImageView) view.findViewById(R.id.avtaar);
holder.favorite = (CheckBox) view.findViewById(R.id.fav);
holder.favorite.setTag(position);
view.setTag(holder);
holder.favorite.setTag(data.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).favorite.setTag(data.get(position));
}
holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Model element = (Model) holder.favorite.getTag();
element.setSelected(buttonView.isChecked());
//notifyDataSetChanged();
}
});
ViewHolder holder = (ViewHolder) view.getTag();
holder.favorite.setChecked(data.get(position).isSelected());
holder.title.setText(data.get(position).getName());
try {
holder.image.setImageBitmap(openPhoto(Long.valueOf(id_list.get(position))));
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
class ViewHolder {
TextView title;
TextView model;
ImageView image;
CheckBox favorite;
}
}
数据模型类
public class Model {
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
selected = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
不知道我哪里做错了。
在类似的情况下,例如带有复选框,单选按钮,计数框等的列表视图,我使用如下方式1)保留一个大小类似于listview(行数)的数组列表2)此数组/数组列表将在适配器类的构造函数中初始化,所有位置为零3)在声明复选框并将其标记设置为位置后在getView()中声明复选框并将其标签设置为位置
public class Conference_listViewAdapter extends BaseAdapter {
Activity context;
LayoutInflater inflater;
public static List<Model> data = null;
ArrayList<String> id_list = new ArrayList<String>();
ArrayList<String> myarray=new java.util.ArrayList<String>();
ViewHolder holder;
int pos;
public Conference_listViewAdapter(Activity context,List<Model> list, ArrayList<String> id_list) {
data = list;
this.id_list = id_list;
this.context = context;
for(int i=0;i<id_list.size();i++)
myarray.add("0");
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.people_list_item1, null);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.tv_name);
holder.image = (ImageView) view.findViewById(R.id.avtaar);
holder.favorite = (CheckBox) view.findViewById(R.id.fav);
holder.favorite.setTag(position);
view.setTag(holder);
if(myarray.get(Integer.parseInt(holder.favorite.getTag().toString()).equalsIgnoreCase("1")
//make your checkbox checked
else
//keep checkbox unchecked
holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if( isChecked) myarray.set(Integer.parseInt(holder.favorite.getTag().toString()),"1");
else
myarray.set(Integer.parseInt(holder.favorite.getTag().toString()),"0");
notifydatasetchanged();
});
holder.favorite.setTag(data.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).favorite.setTag(data.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.favorite.setChecked(data.get(position).isSelected());
holder.title.setText(data.get(position).getName());
try {
holder.image.setImageBitmap(openPhoto(Long.valueOf(id_list.get(position))));
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
class ViewHolder {
TextView title;
TextView model;
ImageView image;
CheckBox favorite;
Boolean selected;
}
}
总是这保存我的问题与列表视图滚动...
改变这个...
@Override
public Object getItem(int position) {
return position;
}
到..
@Override
public Object getItem(int position) {
return data.get(position);
}
更新:
试试这个。。
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.people_list_item1, null);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.tv_name);
holder.image = (ImageView) view.findViewById(R.id.avtaar);
holder.favorite = (CheckBox) view.findViewById(R.id.fav);
holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do what you want..
}
});
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.favorite.setChecked(data.get(position).isSelected());
holder.title.setText(data.get(position).getName());
try {
holder.image.setImageBitmap(openPhoto(Long.valueOf(id_list.get(position))));
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
您必须保存视图持有人的状态
。然后在convertView==null
时设置标记
,并将其置于或
条件下。
检查我做的方式:
public class Conference_listViewAdapter extends ArrayAdapter<Model> {
Activity context;
LayoutInflater inflater;
public static List<Model> data = null;
ArrayList<String> id_list = new ArrayList<String>();
int pos;
public Conference_listViewAdapter(Activity context, List<Model> list, ArrayList<String> id_list) {
super(context, R.layout.people_list_item1, list);
this.context = context;
this.data = list;
this.id_list = id_list;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.people_list_item1, null);
final ViewHolder holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.tv_name);
holder.image = (ImageView) view.findViewById(R.id.avtaar);
holder.favorite = (CheckBox) view.findViewById(R.id.fav);
view.setTag(holder);
holder.favorite.setTag(data.get(position));
holder.favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Model element = (Model) holder.favorite.getTag();
element.setSelected(buttonView.isChecked());
}
});
} else {
view = convertView;
((ViewHolder) view.getTag()).favorite.setTag(data.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.favorite.setChecked(data.get(position).isSelected());
holder.title.setText(data.get(position).getName());
try {
holder.image.setImageBitmap(openPhoto(Long.valueOf(id_list.get(position))));
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
static class ViewHolder {
protected TextView title;
protected CheckBox favorite;
ImageView image;
}
我目前正在开发一个应用程序,但其中有一个错误。每当用户安装应用或清除数据时,应用都应重置。但相反,应用会为用户填充标准数据,而不是显示用户可以自己输入的开始屏幕。 我的问题主要是关于方法。我想输入: 不幸的是,我无法让它工作。有人能指出手头的问题吗?
我有一个代码,向用户显示一个项目列表。列表项旁边有一个切换按钮。因此,当用户打开toggle时,就会触发警报。现在的问题是,当我设置列表中第一项的切换按钮时,当我滚动到列表视图的底部并再次回到第一项时,它消失和/或重置。我完全不明白为什么要把它设置为保持选中状态。在这个论坛里请求某人帮助我。 我的类CSVAdapter。我附在这里。
Deprecated since version 0.52. When you delete objects (and buckets/containers), the Gateway marks the data for removal, but it is still available to users until it is purged. Since data still resides
我有一个奇怪的问题。我在JavaFX中使用了一个ComboBox,其中每一项都是一个HBox,是一个图像和一个标签的组合。 是字符串的数组列表。只是一个
我们最近已经将log4j2-beta9迁移到log4j2-2.0版本。我们正面临滚动文件的问题。 第一个问题:滚动文件没有压缩,只保留了. log文件。 第二个问题:活动文件没有被清除。日志不断被添加到同一个文件中,从而增加了文件大小。 请找到我的log4j2。xml:
问题内容: 默认情况下,扑增加一个发光效果到手机上 我想完全删除此效果或在一个特定的滚动条上删除此效果。我知道我可以在弹跳/钳位之间进行更改。但这实际上并不能消除发光效果。 我能做什么 ? 问题答案: 光晕效果来自GlowingOverscrollIndicator 通过添加ScrollBehavior 要消除这种影响,您需要指定一个custom ScrollBehavior。为此,只需将应用程序