[英]How to get data from dynamically created EditText and TextView inside a linearlayout?
In my main.xml I have a LinearLayout: 在我的main.xml中,我有一个LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearMain"
>
</LinearLayout>
Then, inside my Main.java I am retrieving values from the DB (name and price of an item). 然后,在Main.java中,我正在从数据库中检索值(商品的名称和价格)。 Then, I dynamically created my form using the below code:
然后,我使用以下代码动态创建表单:
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);
The user will be able to enter the number of items and consequently onClick of the button a TextView will be manipulated. 用户将能够输入项目的数量,因此,单击按钮时将操纵TextView。
To fill this textview I will need to loop on all the items that were created to check the price and check how the number that the user entered. 要填充此文本视图,我将需要循环查看所有创建的项目,以检查价格并检查用户输入的数字。 I tried using the below code, however I am not able to access the specific item that I want as I have in each row 2 TextView and 1 editText:
我尝试使用以下代码,但是由于每行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);
}
}
});
How can I access the different element in the v1? 如何访问v1中的其他元素?
You can use tag parameter of your Views (TextView, EditText etc.): setTag(Object) . 您可以使用视图的标签参数(TextView,EditText等):setTag(Object)。 Tags are essentially an extra piece of information that can be associated with a view.
标签本质上是可以与视图关联的额外信息。 They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.
它们最常用于方便地在视图本身中存储与视图相关的数据,而不是将它们放在单独的结构中。 Check Android documentation for more details.
查看Android 文档以了解更多详细信息。
PS Instead to use your approach, you can use RecyclerView (or older ListView) with ViewHolder. PS代替使用您的方法,可以将RecyclerView(或更旧的ListView)与ViewHolder一起使用。 This already mentioned in comments.
这已在评论中提到。
Answer to your question: The problem is that your "product", "pricetxt", "btn" variables are local variables and gets garbage collected after your current loop iteration is over. 回答您的问题:问题是您的“ product”,“ pricetxt”,“ btn”变量是局部变量,并且在当前循环迭代结束后会收集垃圾。
Well you can implement view holder pattern in your project. 好了,您可以在项目中实现视图持有者模式。
Create a class named ViewHolder 创建一个名为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;
}
This class can hold all your data by passing all these parameters (qty, pricetxt, etc). 此类可以通过传递所有这些参数(qty,pricetxt等)来保存所有数据。 Now you have to maintain a List for this ViewHolder Object at the top.
现在,您必须在顶部维护此ViewHolder对象的列表。 You can do this as-
您可以这样做-
List myList<ViewHolder> = new ArrayList<ViewHolder>();
At the end of every iteration, create and add the ViewHolder object; 在每次迭代结束时,创建并添加ViewHolder对象; You can do this as follows
您可以按照以下步骤进行操作
ViewHolder currentView = new ViewHolder(l1.product,pricetxt,currency,qtylabel,qty);
myList.add(currentView); myList.add(currentView); myList.add(currentView);
myList.add(currentView);
You can access any ViewHolder object maintained inside the list at position "index" as 您可以按以下方式访问列表内位置“ index”处维护的任何ViewHolder对象:
myList.get(index);
Or the EditText "qty" as 或将EditText“ qty”作为
myList.get(index).qty;
In this way you can access your created EditTexts and TextView even after the loop iteration is over. 这样,即使循环迭代结束,您也可以访问创建的EditTexts和TextView。
My Suggestion:- No one does it in this way. 我的建议:-没有人这样做。 As suggested some guys over here you should you android implement recommended RecyclerView which is much more efficient than the current implementation.
正如在这里建议的一些人,您应该用Android实现推荐的RecyclerView ,它比当前的实现效率更高。 In cases you can even you ListView .
如果您甚至可以使用ListView 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.