【发布时间】:2014-12-12 07:31:38
【问题描述】:
我有什么:我有一个微调器,我已经设置了适配器,下面以 coed 显示的适配器有一个 checkbox 和一个 textview
发生了什么:: 当我选中一行中的复选框并滚动到下方时,会自动选中下方适配器中的某些项目。同样,当我向上滚动时,我检查的适配器也保持检查状态
我想做的事:: 如何始终在适配器中保留正确选择的复选框。当我上下滚动时
AdptCategories.java
public class AdptCategories extends BaseAdapter {
ArrayList<HashMap<String, String>> categorySpinnerData;
Context context;
//ArrayList<ListObject> objects;
public AdptCategories(ArrayList<HashMap<String, String>> _categorySpinnerData, Context _context) {
super();
context=_context;
categorySpinnerData=_categorySpinnerData;
}
@Override
public int getCount() {
return categorySpinnerData.size();
}
@Override
public Object getItem(int position) {
return categorySpinnerData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
HashMap<String, String> mapData=categorySpinnerData.get(position);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adpt_categories_spinner, null);
}
TextView txtCategoryNameId = (TextView) convertView.findViewById(R.id.txtCategoryNameId);
CheckBox categoryChkBxId = (CheckBox)convertView.findViewById(R.id.categoryChkBxId);
if(position==0){
txtCategoryNameId.setText(mapData.get("name"));
categoryChkBxId.setVisibility(View.INVISIBLE);
}else{
txtCategoryNameId.setText(mapData.get("name"));
txtCategoryNameId.setTag(mapData.get("id"));
categoryChkBxId.setVisibility(View.VISIBLE);
}
return convertView;
}
}
{编辑}
public class AdptCategories extends BaseAdapter {
ArrayList<HashMap<String, String>> categorySpinnerData;
Context context;
private ArrayList<Boolean> checked = new ArrayList<Boolean>();
//ArrayList<ListObject> objects;
public AdptCategories(ArrayList<HashMap<String, String>> _categorySpinnerData, Context _context) {
super();
context=_context;
categorySpinnerData=_categorySpinnerData;
}
@Override
public int getCount() {
return categorySpinnerData.size();
}
@Override
public Object getItem(int position) {
return categorySpinnerData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolderItem {
TextView txtCategoryNameId;
static CheckBox categoryChkBxId;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
HashMap<String, String> mapData=categorySpinnerData.get(position);
for (int i = 0; i < this.getCount(); i++)
{
checked.add(i, false);
}
ViewHolderItem.categoryChkBxId.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton checkbox,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked)
{
checked.set(position,true);
}
else
{
checked.set(position,false);
}
}
});
ViewHolderItem.categoryChkBxId.setChecked(checked.get(position));
if(convertView==null){
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adpt_categories_spinner, null);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.txtCategoryNameId = (TextView) convertView.findViewById(R.id.txtCategoryNameId);
viewHolder.categoryChkBxId = (CheckBox)convertView.findViewById(R.id.categoryChkBxId);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource every time
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}
if(position==0){
viewHolder.txtCategoryNameId.setText(mapData.get("name"));
viewHolder.categoryChkBxId.setVisibility(View.INVISIBLE);
}else{
viewHolder.txtCategoryNameId.setText(mapData.get("name"));
viewHolder.txtCategoryNameId.setTag(mapData.get("id"));
viewHolder.categoryChkBxId.setVisibility(View.VISIBLE);
}
return convertView;
}
}
【问题讨论】:
-
AdapterView 重用滚动视图,以便在数组的帮助下更好地跟踪选中的项目,并在 getView 方法中选中和取消选中复选框
-
@Triode 我面临同样的问题。我使用自定义适配器来填充我的列表视图行。我做了我的列表视图多项选择。当我滚动列表视图时,我的支票消失了。我该如何解决这种情况?我应该为选定的项目使用新数组吗? stackoverflow.com/questions/28714738/…