【问题标题】:Android loss of view state on scrolling expandablelistview with level state drawable objectAndroid 在使用级别状态可绘制对象滚动可扩展列表视图时丢失视图状态
【发布时间】:2016-07-05 13:07:54
【问题描述】:

我正在开发一个包含可绘制对象的清单的活动 具有级别状态。

为了达到这个目的,我有一个 MainActivity :

public class CheckListActivityOld extends ActionBarActivity {

private ExpandableListView expandableListView;
private List<String> parentHeaderInformation;
dbManager db;
String carName, userName, currDate;
Vehicle car;
Tools tools;
CheckListItem checkListItem;
long idCheckListItem, idCheckList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_checklist);

    carName = getIntent().getExtras().getString("carName");
    userName = getIntent().getExtras().getString("userName");

    tools = new Tools();
    db = new dbManager(this);

    db.openToWrite();
    String countCheckListItem = db.getCountCheckListItem(tools.getCurrentDate(),userName,carName);
    if(countCheckListItem.equals("0")){
        Toast.makeText(getApplicationContext(),"checkListItem = null",Toast.LENGTH_LONG).show();
        checkListItem = new CheckListItem(tools.getCurrentDate(),userName,carName);
        idCheckListItem = db.addCheckListItem(checkListItem);
        if(idCheckListItem == -1){
            Toast.makeText(getApplicationContext(),"The checkListItem has not been added. Please contact your administrator.",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(),"The checkListItem has been added. id = "+String.valueOf(idCheckListItem),Toast.LENGTH_LONG).show();
        }
    }else{
        checkListItem = db.getCheckListItemIinfo(tools.getCurrentDate(),userName,carName);
        idCheckListItem = Long.valueOf(checkListItem.getIdCheckListItem());
    }

    car = db.getVehicleData(carName);
    db.close();

    addCheckListItems();

    db.openToRead();
    parentHeaderInformation = db.getDomaineOnList();
    db.close();

    HashMap<String, List<CheckListSource>> allChildItems = returnGroupedChildItems();
    expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
    ExpandableListViewAdapter expandableListViewAdapter = new ExpandableListViewAdapter(getApplicationContext(), parentHeaderInformation, allChildItems);
    expandableListView.setAdapter(expandableListViewAdapter);
    expandableListViewAdapter.notifyDataSetChanged();
}

private HashMap<String, List<CheckListSource>> returnGroupedChildItems(){
    HashMap<String, List<CheckListSource>> childContent = new HashMap<String, List<CheckListSource>>();

    db.openToRead();
    for(int i=0;i<parentHeaderInformation.size();i++){
        List<CheckListSource> items = new ArrayList<CheckListSource>();
        String domaine = parentHeaderInformation.get(i).toString();
        Log.d("checklist","domaine = "+domaine);
        Log.d("idCheckListItem",String.valueOf(idCheckListItem));
        String DomaineID = db.getDomaineID(domaine);
        //items = db.getItemsOnListByDomaineID(String.valueOf(idCheckListItem),DomaineID);
        items = db.getCheckListSourceItemsByDomaineID(String.valueOf(idCheckListItem),DomaineID);
        childContent.put(domaine, items);
    }
    return childContent;
}

@Override

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void addCheckListItems() {

    String carId = car.getIdVehicle();
    String dateTime = tools.getCurrentTimeStamp();
    String statut = "Not Tested";
    String commentaire = "";

    db.openToRead();
    Cursor cItems = db.getAllItemsOnCursor();
    if(cItems.moveToFirst()){
        do{
            String itemId = cItems.getString(0);
            CheckList checkList = new CheckList(itemId,statut,commentaire,dateTime,String.valueOf(idCheckListItem));
            idCheckList = db.addCheckList(checkList);
            Log.d("CheckList->add","New Checklist item id = "+String.valueOf(idCheckList));
        }while (cItems.moveToNext());
    }
    db.close();
}}

我加入一个 hashmap 对象 HashMap> allChildItems 由下面的对象 CheckListSource 返回:

public class CheckListSource {

public String DateCheckList;
public String userName;
public String carName;
public String Comments;
public String Domain;
public String DomainID;
public String itemsName;
public String itemID;
public String idCheckList;
public String Statut;
public String idCheckListItem;

public CheckListSource(String dateCheckList, String userName, String carName, String comments, String domain, String domainID, String itemsName, String itemID, String idCheckList, String statut, String idCheckListItem) {
    DateCheckList = dateCheckList;
    this.userName = userName;
    this.carName = carName;
    Comments = comments;
    Domain = domain;
    DomainID = domainID;
    this.itemsName = itemsName;
    this.itemID = itemID;
    this.idCheckList = idCheckList;
    Statut = statut;
    this.idCheckListItem = idCheckListItem;
}

...还有一些 getter/setter。

expandableListViewAdapter 是:

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> parentDataSource;
HashMap<String, List<CheckListSource>> childDataSource;
dbManager db;

public ExpandableListViewAdapter(Context context, List<String> childParent, HashMap<String, List<CheckListSource>> child) {
    this.context = context;
    this.parentDataSource = childParent;
    this.childDataSource = child;
    db = new dbManager(context);
}

@Override
public int getGroupCount() {
    return this.parentDataSource.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return this.childDataSource.get(this.parentDataSource.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return parentDataSource.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.childDataSource.get(parentDataSource.get(groupPosition)).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    View view = convertView;


    if(view == null){
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.parent_layout, parent, false);
    }else{
        view = convertView;
    }

    String parentHeader = (String)getGroup(groupPosition);
    TextView parentItem = (TextView)view.findViewById(R.id.parent_layout);
    parentItem.setText(parentHeader.toUpperCase());
    return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    View view = null;

    final CheckListSource dataItems = childDataSource.get(parentDataSource.get(groupPosition)).get(childPosition);
    final Object oItems = getChild(groupPosition,childPosition);

    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.child_layout, parent, false);

        // well set up the ViewHolder
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.textViewItemName = (TextView) view.findViewById(R.id.text_item_name);
        viewHolder.Comments = (TextView) view.findViewById(R.id.textViewChecklistComment);
        viewHolder.itemState = (ImageView) view.findViewById(R.id.imageViewCheckboxState);

        viewHolder.itemState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String dbTaskState = dataItems.getStatut();
                String idCheckList = dataItems.getIdCheckList();

                final ImageView tickbox = (ImageView) view.findViewById(R.id.imageViewCheckboxState);
                db.openToWrite();
                int checkState = viewHolder.itemState.getDrawable().getLevel();
                if(checkState == 2) {
                    viewHolder.itemState.getDrawable().setLevel(0);
                    db.setCheckListStatut(idCheckList,"Not Tested");
                    tickbox.getDrawable().setLevel(0);
                }else if(checkState == 1) {
                    viewHolder.itemState.getDrawable().setLevel(2);
                    db.setCheckListStatut(idCheckList,"OK");
                    tickbox.getDrawable().setLevel(2);
                }else if(checkState == 0){
                    viewHolder.itemState.getDrawable().setLevel(1);
                    db.setCheckListStatut(idCheckList,"NOK");
                    tickbox.getDrawable().setLevel(1);
                }
                db.close();
            }
        });

        // store the holder with the view.
        view.setTag(viewHolder);

    }else{
        // we've just avoided calling findViewById() on resource everytime
        // just use the viewHolder
        view = convertView;

    }

    ViewHolder holder = (ViewHolder)view.getTag();

    String taskName = dataItems.getItemsName();

    holder.textViewItemName.setText(taskName);
    holder.Comments.setText(dataItems.getComments());

    String Statut = dataItems.getStatut();

    if(Statut.equals("Not Tested")){
        holder.itemState.getDrawable().setLevel(0);
    }else if(Statut.equals("OK")){
        holder.itemState.getDrawable().setLevel(2);
    }else if(Statut.equals("NOK")){
        holder.itemState.getDrawable().setLevel(1);
    }

    /*if(childPosition % 2 == 0){
        view.setBackgroundColor(Color.rgb(238, 233, 233));
    }
    else {    view.setBackground(context.getDrawable(R.color.color_continental2));
    }*/
    return view;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {

    return true;
}

static class ViewHolder {
    TextView textViewItemName;
    Switch taskState;
    TextView Comments;
    ImageView itemState;
}

}

我的问题是:

  • 当我滚动浏览我的列表视图时,选中的状态不是 持久的。但是,数据库已正确更新。

你能帮我让状态元素持久化吗?

谢谢。

【问题讨论】:

    标签: listview state expandablelistadapter


    【解决方案1】:

    使用带有适配器的 POJO 类,并通过 setter 和 getter 更改 POJO 类中的 bool 选择或未选择值。

    因此,在此的帮助下,当ListView 重绘滚动视图时,值不会反映,您会获得正确的数据或选择。

    public class CheckListSource {
            public String DateCheckList;
            public String userName;
            public String carName;
            public String Comments;
            public String Domain;
            public String DomainID;
            public String itemsName;
            public String itemID;
            public String idCheckList;
            public String Statut;
            public String idCheckListItem;
            public boolean isSelected;
    
            public boolean isSelected() {
                    return isSelected;
            }
    
            public void setSelected(boolean selected) {
                    isSelected = selected;
            }
            public String getDateCheckList() {
                    return DateCheckList;
            }
    
            public void setDateCheckList(String dateCheckList) {
                    DateCheckList = dateCheckList;
            }
    
            public String getUserName() {
                    return userName;
            }
    
            public void setUserName(String userName) {
                    this.userName = userName;
            }
    
            public String getCarName() {
                    return carName;
            }
    
            public void setCarName(String carName) {
                    this.carName = carName;
            }
    
            public String getComments() {
                    return Comments;
            }
    
            public void setComments(String comments) {
                    Comments = comments;
            }
    
            public String getDomain() {
                    return Domain;
            }
    
            public void setDomain(String domain) {
                    Domain = domain;
            }
    
            public String getDomainID() {
                    return DomainID;
            }
    
            public void setDomainID(String domainID) {
                    DomainID = domainID;
            }
    
            public String getItemsName() {
                    return itemsName;
            }
    
            public void setItemsName(String itemsName) {
                    this.itemsName = itemsName;
            }
    
            public String getItemID() {
                    return itemID;
            }
    
            public void setItemID(String itemID) {
                    this.itemID = itemID;
            }
    
            public String getIdCheckList() {
                    return idCheckList;
            }
    
            public void setIdCheckList(String idCheckList) {
                    this.idCheckList = idCheckList;
            }
    
            public String getStatut() {
                    return Statut;
            }
    
            public void setStatut(String statut) {
                    Statut = statut;
            }
    
            public String getIdCheckListItem() {
                    return idCheckListItem;
            }
    
            public void setIdCheckListItem(String idCheckListItem) {
                    this.idCheckListItem = idCheckListItem;
            }
    }
    

    在您的适配器中:

    private ArrayList<CheckListSource > itemsData;
    
    public ChildListAdapter(Activity activity, ArrayList<ChildListResponse> baseResponse) {
        this.itemsData = baseResponse;
        this.activity = activity;
    }
    

    在适配器的 BindViewHolder 中:

    viewHolder.checkParentView.setTag(itemsData.get(position));
    viewHolder.checkParentView.setOnClickListener(checkedListener);
    
    
    private View.OnClickListener checkedListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            CheckListSource childListResponse = (CheckListSource ) v.getTag();
            if (childListResponse.isSelected())
                childListResponse.setSelected(false);
            else
                childListResponse.setSelected(true);
            notifyDataSetChanged();
        }
    };
    

    【讨论】:

    • 非常感谢 Chirag :) ...我会做一些测试并写下我的反馈 ;-)
    • 您好 P.Francis .. 请检查我的回答是否对您有帮助.. 实际上我被否决了.. 如果您有任何问题,请回复,否则请投票
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多