从循环得到编辑的文本值 [英] Get edit text values from a loop

查看:168
本文介绍了从循环得到编辑的文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于一些数据库中的数据的创建编辑文本。我的问题是我怎么能检索循环创建的编辑文本中的文本,只是实例化的TextView?

I'm creating edit texts based on a number of data in the database. My problem is how can I retrieve the text inside the edit text created from a loop and just instantiating the textview?

DBAdapter db = new DBAdapter(this);
        db.open();

        Cursor c = db.getUserExercises(Integer.parseInt(id), musclegroup);
        tag = new int[c.getCount()];
        int n = 0;
        if(c.moveToFirst())
        {

            do{
                LinearLayout linearlayout = new LinearLayout(this);
                TextView tv = new TextView(this);
                tv.setText(c.getString(0));
                tv.setTextColor(Color.DKGRAY);
                tv.setPadding(10, 10, 10, 10);

                et = new EditText(this);
                et.setPadding(5, 5, 5, 5);
                et.setText(c.getString(1));
                et.setEms(2);
                et.setGravity(Gravity.CENTER);
                InputFilter maxLengthFilter = new InputFilter.LengthFilter(1);
                et.setFilters(new InputFilter[]{ maxLengthFilter });

                update = new ImageButton(this);
                update.setBackgroundResource(R.drawable.updatebtn);
                update.setTag(tag[n++]);
                update.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        for(int i=0;i<tag.length;i++)
                        {
                            if(Integer.parseInt(update.getTag().toString()) == i)
                            {
                                Toast.makeText(getBaseContext(), update.getTag().toString(), Toast.LENGTH_SHORT).show();
                            }
                        }
                     }
                    });


                TextView border1 = new TextView(this);
                border1.setHeight(2);
                border1.setBackgroundResource(android.R.color.darker_gray);

                mainLayout.addView(linearlayout);
                linearlayout.addView(tv);
                linearlayout.addView(et);
                linearlayout.addView(update);
                mainLayout.addView(border1);

            }while(c.moveToNext());



        }
        db.close();

我试过的东西在这里,但它似乎并没有工作。还有一件事,我怎么能检查,如果编辑文本是空的?例如:编辑文本具有一个第三,当我preSS键盘后退按钮,这一数字将被删除的。是editext空?或者是等于或?谢谢!

I tried something here but it doesn't seem to work. And another thing, how can I check if the edit text is empty? Ex: The edit text has a number of three, when I press the back button in the keyboard, the number will be erased. Is the editext null? or is it equal to "" or " "? Thanks!

推荐答案

我认为最简单的方法是将它们添加到一个ArrayList或其他集合,你正在创建它们。然后通过这个集合循环,当你希望你的文字。

i think the easiest way would be to add them to an arraylist or other collection as you're creating them. then loop through this collection when you want your texts.

如果你的文字确实是空的,你可以尝试像

if your text is indeed empty, you could try something like

if(et.getText().toString().length() <= 0)


我真的认为这种事情会更适合于一个ListView,但如果你一定要在这条路上进行查看是否存在以下让任何意义。


I really think that this kind of thing would be much better suited for a listview, but if you must carry on this way see if the following makes any sense.

DBAdapter db = new DBAdapter(this);
        db.open();
        Cursor c = db.getUserExercises(Integer.parseInt(id), musclegroup);
        int total = c.getCount();    

        arrEt = new EditText[total];  
        arrTv = new TextView[total];
        arrStr = new String[total];

        int n = 0;
        while (c.moveToNext()) {
            // creation code block
            LinearLayout linearlayout = new LinearLayout(this);

            TextView tv = new TextView(this);
            tv.setText(c.getString(0));
            tv.setTextColor(Color.DKGRAY);
            tv.setPadding(10, 10, 10, 10);

            EditText et = new EditText(this);
            et.setPadding(5, 5, 5, 5);
            et.setText(c.getString(1));
            et.setEms(2);
            et.setGravity(Gravity.CENTER);
            InputFilter maxLengthFilter = new InputFilter.LengthFilter(1);
            et.setFilters(new InputFilter[]{ maxLengthFilter });

            ImageButton update = new ImageButton(this);
            update.setBackgroundResource(R.drawable.updatebtn);
            update.setTag(n);
            update.setOnClickListener(listener);
            //^ the tagging and listener

            TextView border1 = new TextView(this);
            border1.setHeight(2);
            border1.setBackgroundResource(android.R.color.darker_gray);

            // add to layout
            mainLayout.addView(linearlayout);
            linearlayout.addView(tv);
            linearlayout.addView(et);
            linearlayout.addView(update);
            mainLayout.addView(border1);

            // add to arrays
            arrEt[n] = et;
            arrTv[n] = tv;

            n++;
        }
    }

    OnClickListener listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int index = (Integer) v.getTag();
            String value = arrEt[index].getText().toString();
            arrStr[index] = value;
        }
    };

这篇关于从循环得到编辑的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆