Android实例 失物招领

前言:这是Bmob 的经典案例,但是由于现在版本升级哪里也不更新了,同时我辅导员助手这个项目正好有这个功能,拿来用一下,上次去计算机程序设计大赛,评委老师提了个建议,说最后用自己的数据库,不要用公司的,这样相当于数据不在自己手里,我自己以前也腾讯上搭过了一个自己的学生Linux服务器和mysql 也使用过 阿里云mysql ,最后作为一个学生党,还是比较喜欢Bomb,对于学生来说,真的是方便与便利。

创建一个失物招领的实例,继承bmob数据库,方便获取、添加数据等。

先讲 布局RecyclerView

在使用RecyclerView时候,必须指定一个适配器Adapter和一个布局管理器LayoutManager。适配器继承RecyclerView.Adapter类,具体实现类似ListView的适配器,取决于数据信息以及展示的UI。布局管理器用于确定RecyclerView中Item的展示方式以及决定何时复用已经不可见的Item,避免重复创建以及执行高成本的findViewById()方法。

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rl_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Item的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/ll_item"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="#fff"
              android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:text="@string/t1"
        android:textColor="#000"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:text="@string/con1"
        android:textColor="#9000"
        android:textSize="15sp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/tv_num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:background="#5CBE6C"
            android:padding="5dp"
            android:text="16642712339"
            android:textColor="#fff"/>

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="2019-05-21 17:04:11"
            android:textColor="#9000"/>
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginTop="5dp"
        android:background="#5000"/>
</LinearLayout>

创建适配器

标准实现步骤如下:
创建Adapter:创建一个继承RecyclerView.Adapter<VH>的Adapter类(VH是ViewHolder的类名)
创建ViewHolder:在Adapter中创建一个继承RecyclerView.ViewHolder的静态内部类,记为VH。ViewHolder的实现和ListView的ViewHolder实现几乎一样。
③ 在Adapter中实现3个方法

nCreateViewHolder()

这个方法主要生成每个Item inflater出一个View,但是该方法返回的是一个ViewHolder。该方法把View直接封装在ViewHolder中,然后我们面向的是ViewHolder这个实例,当然这个ViewHolder需要我们自己去编写。

  @Override
    public LostAndFoundHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.lost_item, parent, false);
        LostAndFoundHolder lostAndFoundHolder = new LostAndFoundHolder(view);
        return lostAndFoundHolder;
    }

onBindViewHolder()
这个方法主要用于适配渲染数据到View中。方法提供给你了一viewHolder而不是原来的convertView。

getItemCount()
这个方法就类似于BaseAdapter的getCount方法了,即总共有多少个条目。

public class LostAndFoundAdapter extends RecyclerView.Adapter<LostAndFoundAdapter.LostAndFoundHolder> {
    private Context mContext;
    private List<LostInfomationReq> lostInfosData;
    private ItemClickListener mItemClickListener;
    public final static int EDIT_CODE = 998;
    public final static int DELETE_CODE = 997;

    public LostAndFoundAdapter(Context context) {
        this.mContext = context;
    }


    public void setData(List<LostInfomationReq> data) {
        this.lostInfosData = data;
    }

    @Override
    public LostAndFoundHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.lost_item, parent, false);
        LostAndFoundHolder lostAndFoundHolder = new LostAndFoundHolder(view);
        return lostAndFoundHolder;
    }

    @Override
    public void onBindViewHolder(final LostAndFoundHolder holder, final int position) {
        if (lostInfosData != null) {
            LostInfomationReq lostInfomationReq = lostInfosData.get(position);
            holder.title.setText(lostInfomationReq.getTitle());
            holder.desc.setText(lostInfomationReq.getDesc());
            holder.phoneNum.setText(lostInfomationReq.getPhoneNum());
            holder.time.setText(lostInfomationReq.getCreatedAt());

            holder.llItem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    //mLongClickListener.onLongClick(position);
                    showWindow(holder, position);
                    return false;
                }
            });
        }
    }

    private void showWindow(LostAndFoundHolder holder, final int pos) {
        //加载布局文件
        View contentview = LayoutInflater.from(mContext).inflate(R.layout.pop_window_view,null);
        final PopupWindow popupWindow = new PopupWindow(contentview, LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置焦点
        popupWindow.setFocusable(true);
        //触摸框外
        popupWindow.setOutsideTouchable(true);
        //点击空白处的时候让PopupWindow消失
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        //设置偏移量
        popupWindow.showAsDropDown(holder.time, 300, -100);

        //showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
       // showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
        //showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

        //点击编辑按钮
        contentview.findViewById(R.id.edit_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回调给主界面,进行数据操作
                mItemClickListener.onEditOrDeleteClick(pos, EDIT_CODE);
                //销毁弹出框
                popupWindow.dismiss();
            }
        });

        //点击删除按钮
        contentview.findViewById(R.id.delete_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回调给主界面,进行数据操作
                mItemClickListener.onEditOrDeleteClick(pos, DELETE_CODE);
                //销毁弹出框
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public int getItemCount() {
        return lostInfosData.size() == 0 ? 0 : lostInfosData.size();
    }

    public class LostAndFoundHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView desc;
        private TextView time;
        private TextView phoneNum;
        private LinearLayout llItem;

        public LostAndFoundHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tv_title);
            desc = (TextView) itemView.findViewById(R.id.tv_desc);
            time = (TextView) itemView.findViewById(R.id.tv_time);
            phoneNum = (TextView) itemView.findViewById(R.id.tv_num);
            llItem = (LinearLayout) itemView.findViewById(R.id.ll_item);
        }
    }

    //设置长按事件
    public void setLongClickListener(ItemClickListener clickListener) {
        this.mItemClickListener = clickListener;
    }

    public interface ItemClickListener {
        void onEditOrDeleteClick(int position, int code);
    }
}

其中包括 PopupWindow的使用

长按item 出现弹窗

pop_window_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/edit_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        android:padding="10dp"
        android:text="添加"
        android:textColor="#fff"/>

    <TextView
        android:id="@+id/delete_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@color/blue"
        android:text="删除"
        android:textColor="#fff" />
</LinearLayout>

使用PopupWindow很简单,可以总结为三个步骤:

  1. 创建PopupWindow对象实例;
  2. 设置背景、注册事件监听器和添加动画;
  3. 显示PopupWindow。
public class LostFoodFragment extends Fragment implements View.OnClickListener, LostAndFoundAdapter.ItemClickListener  {
    private RecyclerView recyclerView;
    private LostAndFoundAdapter lostAndFoundAdapter;
    private List<LostInfomationReq> lostInfomationReqList;

    private long exitTime = 0;
    private final static int REQUEST_CODE = 999;
    public LostFoodFragment() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lost_food,container,false);
        initView(view);
        initData();
        return view;
    }


    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.rl_recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        lostAndFoundAdapter = new LostAndFoundAdapter(getActivity());
        lostAndFoundAdapter.setLongClickListener(this);

    }
    private void initData() {
        BmobQuery<LostInfomationReq> lostInfomationReqBmobQuery = new BmobQuery<>();
        lostInfomationReqBmobQuery.order("-updatedAt");//排序
        lostInfomationReqBmobQuery.findObjects(new FindListener<LostInfomationReq>() {
            @Override
            public void done(List<LostInfomationReq> list, BmobException e) {
                if (e == null) {
                    lostInfomationReqList = list;
                    lostAndFoundAdapter.setData(list);
                    recyclerView.setAdapter(lostAndFoundAdapter);
                } else {
                    showToast("查询数据失败");
                }
            }
        });
    }
    private void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onEditOrDeleteClick(int position, int code) {
        if (code == LostAndFoundAdapter.EDIT_CODE) {
            Intent intent = new Intent(getActivity(), Lost_fondActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("editData", lostInfomationReqList.get(position));
            intent.putExtras(bundle);
            startActivityForResult(intent, REQUEST_CODE);
        } else if (code == LostAndFoundAdapter.DELETE_CODE) {
            deleteItemData(position);
        }

    }

    private void deleteItemData(final int position) {
        if (lostInfomationReqList.size() != 0) {
            LostInfomationReq lostInfomationReq = new LostInfomationReq();
            lostInfomationReq.setObjectId(lostInfomationReqList.get(position).getObjectId());
            lostInfomationReq.delete(new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        lostInfomationReqList.remove(position);
                        lostAndFoundAdapter.setData(lostInfomationReqList);
                        lostAndFoundAdapter.notifyDataSetChanged();
                    } else {
                        showToast("删除数据失败");
                    }
                }
            });
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    refreshData();//数据刷新
                }
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }



    /**
     * 查询数据库中最新的数据
     * */
    private void refreshData() {
        BmobQuery<LostInfomationReq> lostInfomationReqBmobQuery = new BmobQuery<>();
        lostInfomationReqBmobQuery.order("-updatedAt");//按更新时间排序
        lostInfomationReqBmobQuery.findObjects(new FindListener<LostInfomationReq>() {
            @Override
            public void done(List<LostInfomationReq> list, BmobException e) {
                if (e == null) {
                    lostInfomationReqList = list;
                    lostAndFoundAdapter.setData(list);
                    lostAndFoundAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}

RecyclerView实现Item点击事件处理

RecyclerView并没有像ListView那样提供了OnItemClick,OnItemLongClick等事件回调接口,所以,我们需要自己去实现。

定义回调事件接口

  public interface ItemClickListener {
        void onEditOrDeleteClick(int position, int code);
    }

 声明该接口,并提供setter方法

    //设置长按事件
    public void setLongClickListener(ItemClickListener clickListener) {
        this.mItemClickListener = clickListener;
    }

实现接口

implements View.OnClickListener, LostAndFoundAdapter.ItemClickListener

实现编辑和删除方法

 @Override
    public void onEditOrDeleteClick(int position, int code) {
        if (code == LostAndFoundAdapter.EDIT_CODE) {
            Intent intent = new Intent(getActivity(), Lost_fondActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("editData", lostInfomationReqList.get(position));
            intent.putExtras(bundle);
            startActivityForResult(intent, REQUEST_CODE);
        } else if (code == LostAndFoundAdapter.DELETE_CODE) {
            deleteItemData(position);
        }

    }

    private void deleteItemData(final int position) {
        if (lostInfomationReqList.size() != 0) {
            LostInfomationReq lostInfomationReq = new LostInfomationReq();
            lostInfomationReq.setObjectId(lostInfomationReqList.get(position).getObjectId());
            lostInfomationReq.delete(new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        lostInfomationReqList.remove(position);
                        lostAndFoundAdapter.setData(lostInfomationReqList);
                        lostAndFoundAdapter.notifyDataSetChanged();
                    } else {
                        showToast("删除数据失败");
                    }
                }
            });
        }
    }

通过参数传递数据判断 点击按钮

 //点击编辑按钮
                contentview.findViewById(R.id.edit_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //回调给主界面,进行数据操作
                        mItemClickListener.onEditOrDeleteClick(pos, EDIT_CODE);
                        //销毁弹出框
                        popupWindow.dismiss();
                    }
                });

                //点击删除按钮
                contentview.findViewById(R.id.delete_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //回调给主界面,进行数据操作
                        mItemClickListener.onEditOrDeleteClick(pos, DELETE_CODE);
                        //销毁弹出框
                        popupWindow.dismiss();
            }
        });

这个和前几篇一样,将数据获取和数据插入。写博客的好处,就是梳理思路,理解过程。

失物招领 我来二改了 只有自己发布的可以长按编辑修改/删除  也可以新发布一个新的

实现物品的发布、呈现、修改和删除,用户捡到或丢失物品,打开手机软件,填写物品的招领信息(标题、描述和联系方式);所有用户可以查看到失物和招领的信息列表,如果用户自己发布的消息,长按弹出悬浮窗,可进行编辑和删除。

基于Bmob后端云,添加失物/招领信息到服务器中。查询失物招领所有数据,在列表中显示所有用户发布的失物/招领信息。编辑数据,会将该数据内容直接显示到添加失物招领界面,提交后进行数据更新。删除数据,删除已发布的失物/招领信息。

效果图:

 

public class LostAndFoundAdapter extends RecyclerView.Adapter<LostAndFoundAdapter.LostAndFoundHolder> {
    private Context mContext;
    private List<LostInformation> mLostInformationList;
    private ItemClickListener mItemClickListener;
    public final static int EDIT_CODE = 998;
    public final static int DELETE_CODE = 997;

    public LostAndFoundAdapter(Context context) {
        mContext = context;
    }

    public void setLostInformationList(List<LostInformation> lostInformationList) {
        mLostInformationList = lostInformationList;
    }

    @NonNull
    @Override
    public LostAndFoundHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_lost, parent, false);
        LostAndFoundHolder lostAndFoundHolder = new LostAndFoundHolder(view);
        return lostAndFoundHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull LostAndFoundHolder holder, int position) {
        if (!mLostInformationList.isEmpty()) {
            LostInformation lostInformation = mLostInformationList.get(position);
            holder.title.setText(lostInformation.getTitle());
            holder.desc.setText(lostInformation.getDesc());
            holder.telephone.setText(lostInformation.getPhoneNum());
            holder.time.setText(lostInformation.getCreatedAt());
            holder.llItem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
                    if (myUser.getUsername().equals(lostInformation.getUsername())){
                        showWindow(holder, position);
                        return true;
                    }
                    return false;
                }
            });
        }

    }

    private void showWindow(LostAndFoundHolder holder, int position) {
        //加载布局文件
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.pop_window_view, null);
        final PopupWindow popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置焦点
        popupWindow.setFocusable(true);
        //触摸框外
        popupWindow.setOutsideTouchable(true);
        //点击空白处的时候让PopupWindow消失
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        //设置偏移量
        popupWindow.showAsDropDown(holder.time, 300, -100);
        //点击编辑按钮
        contentView.findViewById(R.id.edit_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回调给主界面,进行数据操作
                mItemClickListener.onEditOrDeleteClick(position, EDIT_CODE);
                //销毁弹出框
                popupWindow.dismiss();
            }
        });

        //点击删除按钮
        contentView.findViewById(R.id.delete_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回调给主界面,进行数据操作
                mItemClickListener.onEditOrDeleteClick(position, DELETE_CODE);
                //销毁弹出框
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mLostInformationList.size() == 0 ? 0 : mLostInformationList.size();
    }

    //设置长按事件
    public void setLongClickListener(ItemClickListener clickListener) {
        this.mItemClickListener = clickListener;
    }

    public interface ItemClickListener {
        void onEditOrDeleteClick(int position, int code);
    }

    public class LostAndFoundHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView desc;
        private TextView time;
        private TextView telephone;
        private LinearLayout llItem;

        public LostAndFoundHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.lost_title);
            desc = (TextView) itemView.findViewById(R.id.lost_desc);
            time = (TextView) itemView.findViewById(R.id.lost_time);
            telephone = (TextView) itemView.findViewById(R.id.lost_telephone);
            llItem = (LinearLayout) itemView.findViewById(R.id.ll_item);
        }
    }

}
public class LostFoundFragment extends BaseFragment implements LostAndFoundAdapter.ItemClickListener {
    @BindView(R.id.recyclerview_lost)
    RecyclerView recyclerViewLost;
    LostAndFoundAdapter lostAndFoundAdapter;
    List<LostInformation> mLostInformationList;

    public static Fragment getInstance() {
        Fragment fragment = new LostFoundFragment();
        return fragment;
    }

    @Override
    protected int contentViewID() {
        return R.layout.fragment_lost_found;
    }

    @Override
    protected void initialize() {
        EventBus.getDefault().register(this);
        lostAndFoundAdapter = new LostAndFoundAdapter(getContext());
        lostAndFoundAdapter.setLongClickListener(this);
        recyclerViewLost.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
        queryData();
    }

    private void queryData() {
        BmobQuery<LostInformation> lostInformationBmobQuery = new BmobQuery<>();
        lostInformationBmobQuery.order("-updatedAt");//排序
        lostInformationBmobQuery.findObjects(new FindListener<LostInformation>() {
            @Override
            public void done(List<LostInformation> object, BmobException e) {
                if (e == null) {
                    mLostInformationList = object;
                    lostAndFoundAdapter.setLostInformationList(mLostInformationList);
                    recyclerViewLost.setAdapter(lostAndFoundAdapter);
                } else {
                    ToastUtils.showShort(getContext(), getString(R.string.tip_query_lost));
                }
            }
        });
    }

    @Override
    public void onEditOrDeleteClick(int position, int code) {
        if (code == LostAndFoundAdapter.EDIT_CODE) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("editData", mLostInformationList.get(position));
            startActivity(EditLostActivity.class, bundle);
        }
        if (code == LostAndFoundAdapter.DELETE_CODE) {
            deleteItemData(position);
        }

    }


    private void deleteItemData(int position) {
        if (!mLostInformationList.isEmpty()) {
            LostInformation lostInformation = new LostInformation();
            lostInformation.setObjectId(mLostInformationList.get(position).getObjectId());
            lostInformation.delete(new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        mLostInformationList.remove(position);
                        lostAndFoundAdapter.setLostInformationList(mLostInformationList);
                        lostAndFoundAdapter.notifyDataSetChanged();
                    } else {
                        ToastUtils.showShort(getContext(), getString(R.string.delete_failure));
                    }
                }
            });
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {
        switch (messageEvent.getMessage()) {
            case ConstantConfig.ADD_LOST_PUSH:
            case ConstantConfig.UPDATE_LOST_PUSH:
                refreshData();
                break;
            default:

        }
    }


    private void refreshData() {
        BmobQuery<LostInformation> lostInformationBmobQuery = new BmobQuery<>();
        lostInformationBmobQuery.order("-updatedAt");//排序
        lostInformationBmobQuery.findObjects(new FindListener<LostInformation>() {
            @Override
            public void done(List<LostInformation> object, BmobException e) {
                if (e == null) {
                    mLostInformationList = object;
                    lostAndFoundAdapter.setLostInformationList(mLostInformationList);
                    lostAndFoundAdapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showShort(getContext(), getString(R.string.tip_query_lost));
                }
            }
        });
    }

    @OnClick(R.id.btn_add_lost)
    public void OnClick(View view){
        startActivity(EditLostActivity.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
public class EditLostActivity extends BaseActivity {
    @BindView(R.id.lost_username)
    TextView lostUsername;
    @BindView(R.id.lost_et_title)
    TextInputEditText etLostTitle;
    @BindView(R.id.lost_et_contact)
    TextInputEditText etLostContact;
    @BindView(R.id.lost_et_desc)
    EditText etLostDesc;
    private boolean isEditLost = false;
    LostInformation lostInformation;

    @Override
    protected int contentViewID() {
        return R.layout.activity_edit_lost;
    }

    @Override
    protected void initialize() {
        setTopTitle(getString(R.string.add_lost), true);
        setLeftBtnFinish();
        MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
        lostUsername.setText(myUser.getUsername());
        Intent intent = getIntent();
        lostInformation = (LostInformation) intent.getSerializableExtra("editData");
        if (lostInformation != null) {
            isEditLost = true;
            setLostText(lostInformation);
        }
    }

    private void setLostText(LostInformation lostInformation) {
        lostUsername.setText(lostInformation.getUsername());
        etLostTitle.setText(lostInformation.getTitle());
        etLostContact.setText(lostInformation.getPhoneNum());
        etLostDesc.setText(lostInformation.getDesc());
    }

    @OnClick(R.id.btn_lost)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_lost:
                if (isEditLost == true) {
                    updateLostInfo();
                } else {
                    addLostInfo();
                }
                break;
            default:
        }
    }

    private void addLostInfo() {
        MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
        LostInformation newLostInformation = new LostInformation();
        newLostInformation.setUsername(myUser.getUsername());
        newLostInformation.setTitle(etLostTitle.getText().toString());
        newLostInformation.setPhoneNum(etLostContact.getText().toString());
        newLostInformation.setDesc(etLostDesc.getText().toString());
        newLostInformation.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null) {
                    ToastUtils.showShort(EditLostActivity.this, getString(R.string.lost_add_success));
                    EventBus.getDefault().post(new MessageEvent(ConstantConfig.ADD_LOST_PUSH));
                } else {
                    ToastUtils.showShort(EditLostActivity.this, getString(R.string.lost_add_failure));
                }
            }
        });
    }

    private void updateLostInfo() {
        MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
        LostInformation newLostInformation = new LostInformation();
        newLostInformation.setUsername(myUser.getUsername());
        newLostInformation.setTitle(etLostTitle.getText().toString());
        newLostInformation.setPhoneNum(etLostContact.getText().toString());
        newLostInformation.setDesc(etLostDesc.getText().toString());
        newLostInformation.update(lostInformation.getObjectId(),new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    ToastUtils.showShort(EditLostActivity.this, getString(R.string.lost_update_success));
                    EventBus.getDefault().post(new MessageEvent(ConstantConfig.UPDATE_LOST_PUSH));
                } else {
                    ToastUtils.showShort(EditLostActivity.this, getString(R.string.lost_update_failure));
                }
            }
        });
    }
}

 

  • 12
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南:-)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值