数据管理模块中,对数据进行全选,反选,删除,是否隐藏/显示

总体思路:用Greendao记录数据item的选择状态,然后根据按钮选择类型,来实际操作修改表中的记录状态,之后(针对多次item操作)再定时刷新。

对应布局文件 activity_collection_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() {
    }

}

你可能感兴趣的:(Android基础,android,资源)