android 多级下拉菜单实现教程
n级下拉菜单需要n个 NiceSpinner,我实现的是3个;
就是在一级的iten点击事件(addOnItemClickListener),进行数据的装载;但是存在问题,不能在子item进行展示;
编辑
NiceSpinner 文字展示不全
NiceSpinner padding太多,文字展示不全
mSpinner.setPadding(0,0,0,0); mSpinner.setGravity(Gravity.CENTER); 复制代码
居中这个问题可以直接设置在父layout
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal"> <org.angmarch.views.NiceSpinner android:id="@+id/nsp_way" android:layout_margin="-9dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="政府下发隐患" android:textColor="@color/color_content_name" /> <org.angmarch.views.NiceSpinner android:id="@+id/nsp_way_three" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center|left" android:text="三方机构" android:textColor="@color/color_content_name" /> <org.angmarch.views.NiceSpinner android:id="@+id/nsp_way_three_expert" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center|left" android:text="三方机构" android:textColor="@color/color_content_name" /> </LinearLayout>
可以居中的
编辑
NiceSpinner demo
NiceSpinner niceSpinner = (NiceSpinner) findViewById(R.id.nice_spinner); niceSpinner.setTextColor(Color.GREEN); LinkedList<String> data=new LinkedList<>(Arrays.asList("Zhang", "Phil", "@", "CSDN")); niceSpinner.attachDataSource(data); //装载数据 niceSpinner.addOnItemClickListener(new AdapterView.OnItemClickListener() {//item监听事件 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView) view).setCompoundDrawablePadding(10); ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(getApplicationContext(), R.drawable.ic_aa), null, null, null); } });
NiceSpinner加图标
Drawable icon=this.getResources().getDrawable(R.drawable.ic_aa); icon.setBounds(0,0, 30,30); mSpinner.setCompoundDrawables(icon,null,null,null);
可以在下拉框的选项(item)中加图片:
mSpinner.addOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView) view).setCompoundDrawablePadding(10); ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(getApplicationContext(), R.drawable.ic_aa), null, null, null); } });
android greendao 3.3 工具类
android studio 4.2
gradle 版本:
编辑
使用greendao:
compile 'org.greenrobot:greendao:3.3.0' // 添加库
编辑
工具类:
根据自己的需求自己新建bean;
在代码后部分有activity ,里面进行单例模式调用;
package com.aa.db; /** * Created by Administrator on 2019/5/15 0015. */ public class DBManager { private final static String dbName = "test_db"; private static DBManager mInstance; private DaoMaster.DevOpenHelper openHelper; private Context context; public DBManager(Context context) { this.context = context; openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } /** * 获取单例引用 * * @param context * @return */ public static DBManager getInstance(Context context) { if (mInstance == null) { synchronized (DBManager.class) { if (mInstance == null) { mInstance = new DBManager(context); } } } return mInstance; } private SQLiteDatabase getReadableDatabase() { if (openHelper == null) { openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } SQLiteDatabase db = openHelper.getReadableDatabase(); return db; } private SQLiteDatabase getWritableDatabase() { if (openHelper == null) { openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } SQLiteDatabase db = openHelper.getWritableDatabase(); return db; } /** * 插入一条记录 * * @param data_new */ public void insertUser(data_new data_new) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); userDao.insertOrReplace(data_new); } public void insertUserList(List<data_new> BlackInfoCallMIssedBeans) { if (BlackInfoCallMIssedBeans == null || BlackInfoCallMIssedBeans.isEmpty()) { return; } DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); userDao.insertOrReplaceInTx(BlackInfoCallMIssedBeans); } public void deleteUser(data_new data_new) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); userDao.queryBuilder() .where(data_newDao.Properties.NUMBERING.eq(data_new.getNUMBERING())) .buildDelete() .executeDeleteWithoutDetachingEntities(); } public void updateUser(data_new data_new) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); userDao.update(data_new); } public List<data_new> queryUserList() { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); QueryBuilder<data_new> qb = userDao.queryBuilder(); // .orderDesc(data_newDao.Properties.Numbering); List<data_new> list = qb.list(); return list; } /** * 查询用户列表 */ public List<data_new> queryUserList(String path) { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); QueryBuilder<data_new> qb = userDao.queryBuilder(); qb.where(data_newDao.Properties.NUMBERING.gt(path)); // .orderAsc(data_newDao.Properties.PhoneNumber); List<data_new> list = qb.list(); return list; } // public List<data_new> queryUser(String numbering) { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); QueryBuilder<data_new> qb = userDao.queryBuilder(); // qb.where(data_newDao.Properties.NUMBERING.gt(numbering)); qb.where(data_newDao.Properties.NUMBERING.eq(numbering)); // .orderAsc(data_newDao.Properties.PhoneNumber); List<data_new> list = qb.list(); return list; } //查询已盘数量 public List<data_new> queryUserInventoried() { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); QueryBuilder<data_new> qb = userDao.queryBuilder(); // qb.where(data_newDao.Properties.NUMBERING.gt(numbering)); qb.where(data_newDao.Properties.Status.eq("已盘")); // .orderAsc(data_newDao.Properties.PhoneNumber); List<data_new> list = qb.list(); return list; } } 这是Activity,在这里进行调用:红色标记是调用 public class ActivityPropertyCheckActivity extends Activity implements View.OnClickListener { private TextView tvAllQuantity; private TextView tvcheckQuantity; private int anIntAllAmount;//总数量 private int anIntCheckAmount;//已盘点数量 private String mAssertNameing;//资产编号 查询关键字 private EditText getEd1() { return (EditText) findViewById(R.id.ed_1); } private EditText getEd2() { return (EditText) findViewById(R.id.ed_2); } private EditText getEd3() { return (EditText) findViewById(R.id.ed_3); } private EditText getEd4() { return (EditText) findViewById(R.id.ed_4); } private EditText getEd5() { return (EditText) findViewById(R.id.ed_5); } private EditText getEd6() { return (EditText) findViewById(R.id.ed_6); } private EditText getEd7() { return (EditText) findViewById(R.id.ed_7); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_property_check); //获得数据数目 initData(); findViewById(R.id.bt_back).setOnClickListener(this); findViewById(R.id.bt_conrm_check).setOnClickListener(this); tvcheckQuantity = (TextView) findViewById(R.id.tv_check_quantity); tvAllQuantity = (TextView) findViewById(R.id.tv_all_quantity); IntentFilter filter = new IntentFilter(); filter.addAction("com.android.server.scannerservice.broadcast"); this.registerReceiver(mResultReceiver, filter); tvcheckQuantity.setText(anIntCheckAmount + ""); tvAllQuantity.setText(anIntAllAmount + ""); } private void SearchData() { data_new dataNew = new data_new(); dataNew.setNUMBERING(mAssertNameing); if (DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUser(mAssertNameing).size() == 0) { Toast.makeText(this, "数据不存在,请重新扫描", Toast.LENGTH_SHORT).show(); } else { //数据回显 dataNew = DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUser(mAssertNameing).get(0); getEd2().setText(dataNew.getUSER_OF_DEPARTMENT()); getEd3().setText(dataNew.getSPECIFICATIONA()); // getEd7().setText(dataNew.getINVENTORY_AMOUNT()); getEd5().setText(dataNew.getLOCATION()); getEd4().setText(dataNew.getUSER_OF_DEPARTMENT()); getEd6().setText(dataNew.getNOTE()); //盘点结果状更新; //盘点结果状态修改 数据修改 if (Double.parseDouble(dataNew.getBOOK_AMOUNT()) < Double.parseDouble(dataNew.getINVENTORY_AMOUNT())) { dataNew.setINVENTORY_RESULT("盘实");//根据要求进行更改 } else if (Double.parseDouble(dataNew.getBOOK_AMOUNT()) == Double.parseDouble(dataNew.getINVENTORY_AMOUNT())) { dataNew.setINVENTORY_RESULT("盘实");//根据要求进行更改 } else if (Double.parseDouble(dataNew.getBOOK_AMOUNT()) > Double.parseDouble(dataNew.getINVENTORY_AMOUNT())) { dataNew.setINVENTORY_RESULT("盘亏");//根据要求进行更改 } }//其实green到进行了很多优化,增删改查使用合适的数据结构,这里的updata是根据id这是Long型的,是主键。主键必有的。 DBManager.getInstance(ActivityPropertyCheckActivity.this).updateUser(dataNew);//数据更新 } private void initData() { anIntAllAmount = (DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUserList()).size(); anIntCheckAmount = DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUserInventoried().size(); } private BroadcastReceiver mResultReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals("com.android.server.scannerservice.broadcast")) { mAssertNameing = intent.getStringExtra("scannerdata"); getEd1().setText(mAssertNameing); List<data_new> list = DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUser(mAssertNameing); //扫码后进行资产数量修改 // DBManager.getInstance(ActivityPropertyCheckActivity.this).modicationInventoryResult(list.get(0)); list.get(0).setINVENTORY_AMOUNT(Double.parseDouble(list.get(0).getINVENTORY_AMOUNT()) + 1 + ""); list.get(0).setStatus("已盘"); DBManager.getInstance(ActivityPropertyCheckActivity.this).updateUser(list.get(0)); if (mAssertNameing != null) { SearchData(); } refreshData(); } } }; @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_back: //TODO implement finish(); break; case R.id.bt_conrm_check: refreshData(); //TODO implement break; } } /*刷新盘点数目和未盘点数目*/ private void refreshData() { initData(); anIntCheckAmount = DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUserInventoried().size(); tvAllQuantity.setText(anIntAllAmount + ""); tvcheckQuantity.setText(anIntCheckAmount + ""); } @Override protected void onDestroy() { super.onDestroy(); if (mResultReceiver != null) { this.unregisterReceiver(mResultReceiver); } } }
SQLiteSpy 工具不能查询数据库;
新版的greendao生成3个文件,我们需要从as中复制带同一目录;
把db文件拖拽进SQLiteSpy就能显示;
db文件目录:
data/data /包名/