繁体   English   中英

如何从LinearLayout中动态创建的EditText和TextView获取数据?

[英]How to get data from dynamically created EditText and TextView inside a linearlayout?

在我的main.xml中,我有一个LinearLayout:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearMain"
        >
</LinearLayout>

然后,在Main.java中,我正在从数据库中检索值(商品的名称和价格)。 然后,我使用以下代码动态创建表单:

 Cursor cursor = mydb.getAllJuice();

 lm = (LinearLayout) findViewById(R.id.linearMain);

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);

while (cursor.moveToNext()) {
            int cid = cursor.getInt(0);
            String id = Integer.toString(cid);
            String name = cursor.getString(1);
            String price = cursor.getString(2);

            // Create LinearLayout
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);

            // Create TextView
            TextView product = new TextView(this);
            product.setText(name+"    ");
            ll.addView(product);

            // Create TextView
            TextView pricetxt = new TextView(this);
            pricetxt.setText("      "+price);
            ll.addView(pricetxt);

            // Create TextView
            TextView currency = new TextView(this);
            currency.setText("  LL      ");
            ll.addView(currency);

            // Create TextView
            TextView qtylabel = new TextView(this);
            qtylabel.setText("QTY      ");
            ll.addView(qtylabel);

            EditText qty = new EditText(this);
            qty.setMinLines(1);
            qty.setMaxLines(3);
            ll.addView(qty);
            lm.addView(ll);

        }

        // Create LinearLayout
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);



        final Button btn = new Button(this);
        // Give button an ID
        int j = 122;
        btn.setId(j+1);
        btn.setText("Add To Cart");
        // set the layoutParams on the button
        btn.setLayoutParams(params);
        //Add button to LinearLayout
        ll.addView(btn);
        //Add button to LinearLayout defined in XML
        lm.addView(ll);

用户将能够输入项目的数量,因此,单击按钮时将操纵TextView。

要填充此文本视图,我将需要循环查看所有创建的项目,以检查价格并检查用户输入的数字。 我尝试使用以下代码,但是由于每行2 TextView和1 editText中的内容,我无法访问所需的特定项目:

 btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // code will be here
                btn.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        total = 0 ;

                        View v1 = null;
                        for(int i=0; i<lm.getChildCount(); i++) {
                            v1 = lm.getChildAt(i);


                        }


                    }
                });

如何访问v1中的其他元素?

您可以使用视图的标签参数(TextView,EditText等):setTag(Object)。 标签本质上是可以与视图关联的额外信息。 它们最常用于方便地在视图本身中存储与视图相关的数据,而不是将它们放在单独的结构中。 查看Android 文档以了解更多详细信息。

PS代替使用您的方法,可以将RecyclerView(或更旧的ListView)与ViewHolder一起使用。 这已在评论中提到。

回答您的问题:问题是您的“ product”,“ pricetxt”,“ btn”变量是局部变量,并且在当前循环迭代结束后会收集垃圾。

好了,您可以在项目中实现视图持有者模式。

创建一个名为ViewHolder的类

  class ViewHolder{

  LinearLayout ll;
  TextView product;
  TextView pricetxt ;
  TextView currency ;
  TextView qtylabel ;
  EditText qty ;}
  public ViewHolder(LinearLayout ll TextView product TextView pricetxt TextView currency TextView qtylabel EditText qty)
  {
    this.l1= l1;
    this.product=product;
    this.pricetxt=pricetxt;
    this.currency = currency;
    this.qtylabel = qtylevel;
    this.qty = qty;

  }

此类可以通过传递所有这些参数(qty,pricetxt等)来保存所有数据。 现在,您必须在顶部维护此ViewHolder对象的列表。 您可以这样做-

  List myList<ViewHolder> = new ArrayList<ViewHolder>();

在每次迭代结束时,创建并添加ViewHolder对象; 您可以按照以下步骤进行操作

  ViewHolder currentView = new ViewHolder(l1.product,pricetxt,currency,qtylabel,qty);

myList.add(currentView); myList.add(currentView);

您可以按以下方式访问列表内位置“ index”处维护的任何ViewHolder对象:

    myList.get(index);

或将EditText“ qty”作为

     myList.get(index).qty;

这样,即使循环迭代结束,您也可以访问创建的EditTexts和TextView。

我的建议:-没有人这样做。 正如在这里建议的一些人,您应该用Android实现推荐的RecyclerView ,它比当前的实现效率更高。 如果您甚至可以使用ListView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM