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

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

对应布局文件 activity_collection_edit.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/activity_datalist_toolbar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimaryDark"
        app:contentInsetStart="0dp">

        <LinearLayout
            android:id="@+id/button_back"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingRight="20dp"
            android:visibility="gone">

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:layout_marginStart="12dp"
                android:background="@drawable/button_back" />
        </LinearLayout>

        <TextView
            android:id="@+id/data_edit_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/my_collect"
            android:layout_marginLeft="30dp"
            android:textColor="#ffffff"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/text_save"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:gravity="center"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="保 存"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </android.support.v7.widget.Toolbar>

    <ListView
        android:id="@+id/listview_collection"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_operation"
        android:layout_below="@id/activity_datalist_toolbar" />

    <LinearLayout
        android:id="@+id/bottom_operation"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/select_all"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:background="@drawable/button_opration_blue"
            android:text="全 选"
            android:textColor="#ffffff"
            android:textSize="13sp" />

        <Button
            android:id="@+id/select_invert"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:background="@drawable/button_opration_blue"
            android:text="反 选"
            android:textColor="#ffffff"
            android:textSize="13sp" />

        <Button
            android:id="@+id/set_state"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:background="@drawable/button_opration_blue"
            android:text="隐藏/显示"
            android:textColor="#ffffff"
            android:textSize="13sp" />

        <Button
            android:id="@+id/item_delete"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/button_opration_blue"
            android:text="删 除"
            android:textColor="#ffffff"
            android:textSize="13sp" />

    </LinearLayout>

</RelativeLayout>

主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<Sign> 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<Sign> 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<Sign> signList;
    private String operationType;
    private CollectionEditSelectStateDao collectionEditSelectStateDao;

    public CollectionEditAdapter(CollectionEditActivty collectionEditActivty, List<Sign> 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() {
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值