android开发遇到的问题及解决方案

由于工作需要,第一次做安卓开发,没有系统地学习过,因此做时遇到问题时解决起来很费劲!记录一下重点问题。

问题1:实现一个字段的值(值为集合),支持多选,将选中的值用逗号隔开放在下拉框中。

实现思路:第一次看到原型图画的是一个下拉框,下面是一个可以多选的列表项。因为前面没做过安卓,也不知道这样的效果能不能实现。(1)于是就在网上搜索下拉多选框,结合教程发现Spinner列表选项框,只能单选,但网上搜索可以自定义实现MultiSelectSpinner的效果,无奈功力有限,未能实现;(2)在网上搜索的过程中,就发现Dialog+Listview+checkbox,能实现这样的效果,https://www.cnblogs.com/wzqnxd/p/9144519.html 通过此网址,抓住了核心实现方式,但也遇到了listview点击与checkbox选中效果不同步的问题,经同事帮助及个人理解的加深,实现了。(3)网上搜索过程中还发现了,RecyclerView、PopupWindow等,但我没有试过。

解决方案:(1)在layout xml布局文件中用了EditText,并设置spJyyq.setFocusable(false); 不可编辑,再设置spJyyq.setOnClickListener(l_spjyyq); 点击事件,在事件实现中创建一个对话框,对话框里的View是ListView,ListView里的内容是TextView和Checkbox,主要代码如下:

View getlistview;
ArrayList<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
AlertDialog.Builder builder;
SimpleAdapter adapter;
Boolean[] bl ;

//根据字段集合的值来设置mData、bl的值

if(dictInfoList.size() > 0) {
                        jyyqs.clear();
                        bl = new Boolean[dictInfoList.size()];
                        int i=0;
                        for(DictInfo dictInfo:dictInfoList){
                                jyyqs.add(dictInfo.getDictvalue());
                                Map<String, Object> item = new HashMap<String, Object>();
                                item.put("text", dictInfo.getDictvalue());
                                mData.add(item);
                                bl[i] = true;
                                i++;
                        }
                        defJyyq = org.apache.commons.lang3.StringUtils.join(jyyqs,",");
                        spJyyq.setText(defJyyq);
                        app.setJyyqValue(defJyyq);
                    }
public void CreateDialog() {

        // 动态加载一个listview的布局文件进来
        LayoutInflater inflater = LayoutInflater.from(FunctionListActivity.this);
        getlistview = inflater.inflate(R.layout.item_jyyq_list, null);

        // 给ListView绑定内容
        ListView listview = (ListView) getlistview.findViewById(R.id.X_listview);
        adapter = new SetSimpleAdapter(FunctionListActivity.this, mData, R.layout.item_jyyq, new String[] { "text" },
                new int[] { R.id.X_item_text });
        // 给listview加入适配器
        listview.setAdapter(adapter);
        listview.setItemsCanFocus(false);
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listview.setOnItemClickListener(new ItemOnClick());

        builder = new AlertDialog.Builder(this);
//        builder.setTitle("请选择查询类型");
        builder.setIcon(R.drawable.ic_launcher);
        //设置加载的listview
        builder.setView(getlistview);
        builder.setPositiveButton("确定", new DialogOnClick());
        builder.setNegativeButton("取消", new DialogOnClick());
        //设置点击旁边,不关闭对话框
        builder.setCancelable(false);
        builder.create().show();
    }
//ListView 条目点击事件
class ItemOnClick implements AdapterView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
        CheckBox cBox = (CheckBox) view.findViewById(R.id.X_checkbox);
        TextView textView = view.findViewById(R.id.X_item_text);
        textView.setText(mData.get(position).get("text").toString());
     //将复选框中的选中状态与listview的 保持一致
        if (cBox.isChecked()) {
            bl[position] = false;
            cBox.setChecked(false);
        } else {
            bl[position] = true;
            cBox.setChecked(true);
        }

    }

}
//对话框 按钮事件
class DialogOnClick implements DialogInterface.OnClickListener {
    ListView listview = (ListView) getlistview.findViewById(R.id.X_listview);
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case Dialog.BUTTON_POSITIVE:
                //确定按钮的事件
              //  SparseBooleanArray checklist =  listview.getCheckedItemPositions();
                String strcheck = "";
                for (int i = 0; i < bl.length; i++) {
                    if (bl[i]) {
                        final Object item = mData.get(i).get("text");
                        strcheck += item + ",";
                    }
                }
                if(strcheck.length()>0){
                    strcheck = strcheck.substring(0,strcheck.length()-1);
                }
                spJyyq.setText(strcheck);
                app.setJyyqValue(strcheck);
                break;
            case Dialog.BUTTON_NEGATIVE:
                //取消按钮的事件
                String[] checkJyyqArray = null;
                String checkJyyq = spJyyq.getText().toString();
                if(checkJyyq.length()>0){
                    checkJyyqArray = checkJyyq.split(",");
                }
                if(checkJyyqArray == null){
                    for (int i = 0; i < bl.length; i++) {
                        bl[i]=false;
                    }
                }else {
                    List<String> jyyqLst = Arrays.asList(checkJyyqArray);
                    for (int i = 0; i < bl.length; i++) {
                        String item = mData.get(i).get("text").toString();
                        if(!jyyqLst.contains(item)){
                            bl[i]=false;
                        }else{
                            bl[i]=true;
                        }
                    }
                }
                adapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
    }
}

//重写simpleadapterd的getview方法
class SetSimpleAdapter extends SimpleAdapter {

    public SetSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from,
                            int[] to) {
        super(context, data, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LinearLayout.inflate(getBaseContext(), R.layout.item_jyyq, null);
        }
        CheckBox ckBox = (CheckBox) convertView.findViewById(R.id.X_checkbox);
        TextView textView = convertView.findViewById(R.id.X_item_text);
        textView.setText(mData.get(position).get("text").toString());
        //每次都根据 bl[]来更新checkbox
        if (bl[position] == true) {
            ckBox.setChecked(true);
        } else if (bl[position] == false) {
            ckBox.setChecked(false);
        }
        return super.getView(position, convertView, parent);
    }
}

 

问题2:数据明细采用三列的网格方式展示 不能出现滚动条,即全部展示。用GridView 实现,但老是出现显示不全的问题。

思路:这个功能是前面同事已经做好的功能,但在不同的平板上就会出现显示不全的情况(尤其本来是横板,竖着看时,但用户非要竖着固定平板),通过贴子 https://blog.csdn.net/lintax/article/details/52072074 得出结论:
1, GridView有一个视图高度,其计算方法是:第一个格子的高度*总的排数;
2, GridView有一个内容高度,其计算方法是:每排最后一个格子的高度之和;
3, 若视图高度小于内容高度,则内容显示不全,可以滑动来显示;
4,GridView每排的高度,不是固定的,使用的是最后一个格子的高度。
5,若某一排中有内容超过最后一个格子高度的,则多出的部分,会显示到下一排上,从而出现重叠现象。若这排就是最后一排了,则多出的部分不能显示。

鉴于此问题,同事建议我用TableLayout 动态添加行的方式来实现,事实证明姜还是老的辣!通过此方式解决了问题。

解决方案:1、在layout xml里定义TableLayout 标签,此处尤其说明一下,要合理使用android:shrinkColumns和android:stretchColumns属性 ,若每列显示相同的内容,则这两个属性的值可设置为"*",即每列可拉伸和收缩;若每列显示的内容不一样,某一列的值可能会比较长,希望长的换行显示,则需要将shrinkColumns的值设置为此列号,注:列号从0开始。

2、在获取数据的接口中,设置数据明细集合,并发消息通知 handler,如:

//发消息通知处理明细信息
Message msg = new Message();
msg.what = REFRESH;
mHandler.sendMessage(msg);

//在handler中 根据数据明细数据,动态添加tablelayout

mHandler=new Handler(){
    @Override
    public void handleMessage(Message msg){
        if(msg.what == REFRESH) {
            //移除药味
            tableLayout.removeAllViews();
            setCfMxGXInfo(cfMxInfoList,tableLayout,4);

        }
    }
};

 

public void setCfMxGXInfo(List<CfMxInfo> cfMxInfoList, TableLayout tableLayout,int cols){
    int size = cfMxInfoList.size();
    int fontsize = 20;
    int align = Gravity.LEFT;
    for (int i = 0; i < size; i++) {
        CfMxInfo item = cfMxInfoList.get(i);
        //创建一行
        TableRow row = new TableRow(getApplicationContext());
        for(int j=0;j<cols;j++) {
                //创建显示的内容,这里创建的是一列

                  TextView text = new TextView(getApplicationContext());
                  String content="";
                if(j==0) {
                    StringBuffer sbstr = new StringBuffer();
                    if (StringUtils.isNotBlank(item.getGoodsname())) {
                        sbstr.append(item.getGoodsname());
                        sbstr.append(" ");
                    }
                    if (StringUtils.isNotBlank(item.getTotaldosage())) {
                        sbstr.append(item.getTotaldosage() + item.getGoodscountunit());
                    }
                    content = sbstr.toString();
                }else if(j==1){
                    content = item.getBatchgoodscode();
                }else if(j==2){
                    content = item.getTotalprice() + "元";
                }else if(j==3){
                    content = item.getManufacturer();
                }

                  //设置显示内容
                  text.setText(content);
                  text.setTextSize(fontsize); //字体大小
                  text.getPaint().setFakeBoldText(true); //加粗
                  text.setGravity(align); //居左显示
                  text.setTextColor(Color.WHITE);
            TableRow.LayoutParams params =  new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
               params.setMargins(0,0,15,0);
                  text.setLayoutParams(params);

                  //添加到Row
                  row.addView(text);
              }

        TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER_HORIZONTAL;
        params.setMargins(0,20,0,0); //设置了行高为20
        row.setLayoutParams(params);
        //将一行数据添加到表格中
        tableLayout.addView(row);
    }


}

 

问题3:扫码框,有的平板或手机扫不上条形码,或容易识别错误。

思路:顺着代码摸索实现方式,发现系统是用zxing扫描和识别二维码的,于是网上搜索解决方案,解决时参考:

https://blog.csdn.net/u012917700/article/details/52369175?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-9&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-9

此问题是同事解决的,目前也不太了解zxing,因此先记录到这里。

还有就是屏幕的适配问题,目前只是按目前的平板调整好了,但没有从根本解决问题

目前是在已有项目上做修改,本次开发算是对安桌稍有了解,但仍需努力深入学习。

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
声明:这些内容是逐步总结过来的,所以可能有当时的理解不正确,只希望大家能做个参考: 内容如下: 目录 一句话总结汇总: Copy project into workspace 和add project into work set 的含义 数字签名总结 JNI 基础及注意: Ndk的使用方法: Ant 与 android update project 命令行只有在非根盘符上才能运行 android Launcher Android 运行环境搭建 Android:name什么时候加”.” Activity class {package/class} does not exist 问题解决 Activity 中两次调用OnCreate的原因 ByteBuffer 和 FloatBuffer 的直接分配和非直接分配 Application的使用小总结 “call to OpenGL ES API with no current context (logged once per thread” 问题解决 2013年9月7日19:15:33:我的平板分辨率很高可是运行public void onSurfaceChanged(GL10 gl, int width, int height)函数时,分辨率只有 455*320,这是为什么? 关于android添加第三方字体的方法 android-apt-compiler: [t1] res\layout\LinearLayout.xml: Invalid file name: must contain only [a-z0-9_.] Buttons in button bars should be borderless android 支持的距离单位 使用adb shell命令进入手机后使用ls命令提示: opendir failed permission denied 使用adb pull 命令提示permission denied Button 中的setLayoutParams使用注意: layout文件夹和raw文件下面的文件读取 Matrix方法中的set方法和post方法 android 中调用drawBitmap时理解dip(屏幕密度)和px(像素)的区别 SQLiteDatabase 的setTransactionSuccessful作用 终于弄明白 paddingleft margineleft layout_gravity 和gravity之间的区别 自定义控件时要注意的问题。 obtainMessage 的作用: FrameLayout 需要注意的地方: EditText 禁止弹出按键盘: 获取控件屏幕位置和窗口位置: 为什么MyAdapater的getView没有被调用 XmlSerializer使用总结: ListView中的Item自定义点击后的背景色的方法。 drawable各个分辨率 fragment 的几种创建方式 fragment第一次使用遇到问题 activity变身对话框 onMeasure 中的AT_MOST EXACTLY UNSPECIFIED MotionEvent的触发记录 对于Drawable 的 getIntrinsicHeight 和getIntrinsicWidth的理解 IntentService 使用总结: 文件读写总结: AES 解密失败: XML中的include标签加入后崩溃 Button的background标签使图像拉伸的问题 SharedPreferences 的getString 的陷阱 TextView 中的EMS和Maxlength

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值