데이터 관리 모듈에서 데이터를 모두 선택, 거꾸로 선택, 삭제, 숨기기/표시 여부

전체적인 사고방식:Greendao로 데이터 item의 선택 상태를 기록한 다음에 단추에 따라 유형을 선택하여 실제 조작으로 표의 기록 상태를 수정한 다음에 (여러 번의 item 조작에 대해) 정시에 갱신한다.
레이아웃 파일 activitycollection_edit.xml:



    

        

            
        

        

        
    

    

    

        

主activity CollectionEditActivty :

package com.ys.dzyjdc.about.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ys.dzyjdc.R;
import com.ys.dzyjdc.about.adapter.CollectionEditAdapter;
import com.ys.dzyjdc.about.constant.ProjectConstant;
import com.ys.dzyjdc.about.presenter.CollectionEditPresenter;
import com.ys.dzyjdc.base.BaseActivity;
import com.ys.dzyjdc.map.entity.Sign;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CollectionEditActivty extends BaseActivity {
    private CollectionEditPresenter myCollectionPresenter;
    private List signs;

    @BindView(R.id.button_back)
    LinearLayout btnBack;//    
    @BindView(R.id.text_save)
    TextView saveData;//    
    @BindView(R.id.listview_collection)
    ListView collectionListview;//      
    @BindView(R.id.select_all)
    Button selectAll;//    
    @BindView(R.id.select_invert)
    Button selectInvert;//    
    @BindView(R.id.set_state)
    Button setState;//      
    @BindView(R.id.item_delete)
    Button deleteItem;//    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection_edit);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        //EventBus.getDefault().register(this);
        myCollectionPresenter = new CollectionEditPresenter(this);
        myCollectionPresenter.getCollectionData();
    }

    @OnClick({R.id.button_back, R.id.select_all, R.id.select_invert,R.id.set_state,R.id.item_delete,R.id.text_save})
    public void onClick(View v) {
        switch (v.getId()) {
            //    
            case R.id.button_back:
                startActivity(new Intent(CollectionEditActivty.this, MyCollectionActivity.class));
                break;
            //  
            case R.id.select_all:
                collectionListview.setAdapter(new CollectionEditAdapter(this, signs, ProjectConstant.SELECT_ALL));
                break;
            //  
            case R.id.select_invert:
                collectionListview.setAdapter(new CollectionEditAdapter(this, signs, ProjectConstant.SELECT_INVERT));
                break;
                //  、      
            case R.id.set_state:
                collectionListview.setAdapter(new CollectionEditAdapter(this, signs, ProjectConstant.SET_STATE));
                //       ,       item       
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        //do something
                        myCollectionPresenter.getCollectionData();//      
                    }
                }, 1000);
                break;
            case R.id.item_delete:
                collectionListview.setAdapter(new CollectionEditAdapter(this, signs, ProjectConstant.DELETE_ITEM));
                //       ,         item   
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        //do something
                        myCollectionPresenter.getCollectionData();//      
                    }
                }, 1000);
                break;
                //    
            case R.id.text_save:
                startActivity(new Intent(CollectionEditActivty.this, MyCollectionActivity.class));
                break;
            default:
                break;
        }
    }

    /**
     *         
     *
     * @param signList
     */
    public void showCollectionData(List signList) {
        signs = signList;
        collectionListview.setAdapter(new CollectionEditAdapter(this, signList, ""));
    }

    /*@Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }*/

    /*@Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageWrap message) {
        Log.i("    ", "message is " + message);
        myCollectionPresenter.getCollectionData();//      
    }*/
}

대응 어댑터:
package com.ys.dzyjdc.about.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ys.dzyjdc.R;
import com.ys.dzyjdc.about.activity.CollectionEditActivty;
import com.ys.dzyjdc.about.constant.ProjectConstant;
import com.ys.dzyjdc.about.entity.CollectionEditSelectState;
import com.ys.dzyjdc.dao.CollectionEditSelectStateDao;
import com.ys.dzyjdc.map.entity.Sign;

import java.util.List;

public class CollectionEditAdapter extends BaseAdapter {
    private CollectionEditActivty collectionEditActivty;
    private List signList;
    private String operationType;
    private CollectionEditSelectStateDao collectionEditSelectStateDao;

    public CollectionEditAdapter(CollectionEditActivty collectionEditActivty, List signList, String operationType) {
        this.collectionEditActivty = collectionEditActivty;
        this.signList = signList;
        this.operationType = operationType;
        collectionEditSelectStateDao = collectionEditActivty.getSession().getCollectionEditSelectStateDao();
    }

    @Override
    public int getCount() {
        return signList.size();
    }

    @Override
    public Object getItem(int position) {
        return signList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final Sign sign = signList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(collectionEditActivty).inflate(R.layout.item_collection, null);
            holder = new ViewHolder();
            holder.collectionName = convertView.findViewById(R.id.collection_name);
            holder.createTime = convertView.findViewById(R.id.create_time);
            holder.stateShow = convertView.findViewById(R.id.state_show);
            holder.labelEdit = convertView.findViewById(R.id.label_edit);
            holder.checkBox = convertView.findViewById(R.id.data_choose);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checkBox.setVisibility(View.VISIBLE);
        holder.labelEdit.setVisibility(View.GONE);
        holder.collectionName.setText(sign.getLableName().replace("-", ""));
        holder.createTime.setText(sign.getCreateTime());
        if (sign.getShow()) {
            holder.stateShow.setText("  ");
        } else {
            holder.stateShow.setText("  ");
        }

        //       item  
        final CollectionEditSelectState oldSelectStates = collectionEditSelectStateDao.queryBuilder()
                .where(CollectionEditSelectStateDao.Properties.Position.eq(position)).unique();

        if (oldSelectStates != null) {
            if (oldSelectStates.getIsChecked()) {
                holder.checkBox.setChecked(true);
            } else {
                holder.checkBox.setChecked(false);
            }
        }

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                CollectionEditSelectState editSelectState = new CollectionEditSelectState();
                if (isChecked) {//         
                    editSelectState.setIsChecked(true);
                    editSelectState.setIsShow(sign.getShow());
                    editSelectState.setLableId(sign.getLableId());
                    editSelectState.setPosition(position);
                } else {
                    editSelectState.setIsChecked(false);
                    editSelectState.setIsShow(sign.getShow());
                    editSelectState.setLableId(sign.getLableId());
                    editSelectState.setPosition(position);
                }

                if (oldSelectStates == null) {
                    collectionEditSelectStateDao.insert(editSelectState);
                } else {
                    //       ,      
                    editSelectState.setId(oldSelectStates.getId());
                    collectionEditSelectStateDao.update(editSelectState);
                }
               // notifyDataSetChanged();
            }
        });

        //    
        if (operationType.equalsIgnoreCase(ProjectConstant.SELECT_ALL)) {
            holder.checkBox.setChecked(true);

        } else if (operationType.equalsIgnoreCase(ProjectConstant.SELECT_INVERT)) {//    
            if (oldSelectStates == null) {
                holder.checkBox.setChecked(true);

            } else {//   ,      item     
                if (oldSelectStates.getIsChecked()) {//      
                    holder.checkBox.setChecked(false);

                } else {
                    holder.checkBox.setChecked(true);
                }
            }
        } else if (operationType.equalsIgnoreCase(ProjectConstant.SET_STATE)) {//    
            if (oldSelectStates != null) {
                if (oldSelectStates.getIsChecked()) {//      
                    if (sign.getShow()) {//    
                        sign.setShow(false);
                    } else {
                        sign.setShow(true);
                    }
                    //     
                    collectionEditActivty.getSession().getSignDao().update(sign);
                    // EventBus.getDefault().post(MessageWrap.getInstance("test"));
                }
            }
        } else if (operationType.equalsIgnoreCase(ProjectConstant.DELETE_ITEM)) {//item  
            if (oldSelectStates != null) {
                if (oldSelectStates.getIsChecked()) {
                    collectionEditActivty.getSession().getSignDao().delete(sign);
                    collectionEditSelectStateDao.deleteAll();
                    /*collectionEditSelectStateDao.delete( collectionEditSelectStateDao.queryBuilder().where(CollectionEditSelectStateDao.
                            Properties.LableId.eq(sign.getLableId())).unique());*/
                }
            }

        }
        return convertView;
    }

    class ViewHolder {
        TextView collectionName;//   
        TextView createTime;//      
        TextView stateShow;//      
        LinearLayout labelEdit;//item    
        CheckBox checkBox;
    }
}

테이블 구조:
package com.ys.dzyjdc.about.entity;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;

@Entity
public class CollectionEditSelectState {
    @Id(autoincrement = true)
    private Long id;
    private int position;//      position  
    private boolean isChecked;//      
    private String lableId;//  id
    private Boolean isShow;//    
    public Boolean getIsShow() {
        return this.isShow;
    }
    public void setIsShow(Boolean isShow) {
        this.isShow = isShow;
    }
    public String getLableId() {
        return this.lableId;
    }
    public void setLableId(String lableId) {
        this.lableId = lableId;
    }
    public boolean getIsChecked() {
        return this.isChecked;
    }
    public void setIsChecked(boolean isChecked) {
        this.isChecked = isChecked;
    }
    public int getPosition() {
        return this.position;
    }
    public void setPosition(int position) {
        this.position = position;
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Generated(hash = 703581384)
    public CollectionEditSelectState(Long id, int position, boolean isChecked,
            String lableId, Boolean isShow) {
        this.id = id;
        this.position = position;
        this.isChecked = isChecked;
        this.lableId = lableId;
        this.isShow = isShow;
    }
    @Generated(hash = 1449411871)
    public CollectionEditSelectState() {
    }

}

좋은 웹페이지 즐겨찾기