0

i create 1 LinearLayout inside LinearLayout, that contains 1 EditText and 1 TextView programmatically and i need to get text from EditText and TextView. And this is my code:

main.xml:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp" >

        <LinearLayout
            android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

MainActivity.java:

    linearLayout = (LinearLayout) view.findViewById(R.id.result);

    .......

 public void addItem(){
    LinearLayout childLayout = new LinearLayout(context);
    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    childLayout.setLayoutParams(linearParams);
    childLayout.setTag(item_name);
    childLayout.setOrientation(LinearLayout.HORIZONTAL);


    TextView item = new TextView(context);
    item.setTag(item_name);
    item.setText(item_name);
    item.setTextSize(16);
    item.setGravity(Gravity.LEFT);
    LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    llp1.setMargins(0, 0, 15, 0);
    item.setLayoutParams(llp1);
    item.setTextColor(context.getResources().getColor(android.R.color.black));

    EditText quantity = new EditText(context);
    quantity.setText(nama_barang);
    quantity.setTextSize(16);
    quantity.setGravity(Gravity.CENTER_HORIZONTAL);
    LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    llp2.setMargins(10, 0, 15, 0);
    quantity.setLayoutParams(llp2);
    quantity.setTextColor(context.getResources().getColor(android.R.color.black));

    childLayout.addView(item);
    childLayout.addView(quantity);
    linearLayout.addView(childLayout);
 }

Help me, please.

I've edit my question

Actually, my case above happened when i clicked addItem(), my app will display new TextView and EditText, so if this process is finished, and move to the next Fragment to display all the text of TextViews and EditTexts like this using StringBuilder:

Items    Qtt
Cola......1

Chicken...1

Meat......2

Help me.

wdyz
  • 185
  • 4
  • 19

4 Answers4

0

Do you want to get like this?

String str = item.getText().toString();
String str2 = quantity.getText().toString();
Rodrigo Paixão
  • 246
  • 5
  • 12
  • yes, but my problem is, i want to get all text from the TextViews and all the EditTexts from all ChildLayout inside LinearLayout. There is more TextView & EditText, not just one that created by for condition. – wdyz Nov 16 '16 at 19:48
  • Ok, we can create a Array to get all the strings. Declare outside of the `for` `List strings = new ArrayList<>();` at the end of for add them `strings.add(str);` and `strings.add(str2);` – Rodrigo Paixão Nov 16 '16 at 19:53
  • sorry, i've edited my questions. Please check above. Thanks – wdyz Nov 16 '16 at 20:11
0

As stated above, using item.getText().toString(); is all you need, nonetheless I do presume your problem lies in the fact that you are declaring your Views inside the loop, by doing it locally later anywhere on the code you won't be getting any text at all.

Try declaring your Views as global variables or doing it outside of the loop such as;

//....

TextView item;
EditText quantity;    


for(int i = 0; i < cartCount; i++) {
LinearLayout childLayout = new LinearLayout(context);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childLayout.setLayoutParams(linearParams);
childLayout.setTag(item_name);
childLayout.setOrientation(LinearLayout.HORIZONTAL);


item = new TextView(context);
item.setTag(item_name);
item.setText(item_name);
item.setTextSize(16);
item.setGravity(Gravity.LEFT);
LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp1.setMargins(0, 0, 15, 0);
item.setLayoutParams(llp1);
item.setTextColor(context.getResources().getColor(android.R.color.black));

quantity = new EditText(context);
quantity.setText(nama_barang);
quantity.setTextSize(16);
quantity.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 0, 15, 0);
quantity.setLayoutParams(llp2);
quantity.setTextColor(context.getResources().getColor(android.R.color.black));

childLayout.addView(item);
childLayout.addView(quantity);
linearLayout.addView(childLayout);

}

On the other hand, by looking at your code, are you trying to create a list of EditText and TextViews? If so, it would be wise to consider using a ListView with custom items; Here you can find a nice tutorial doing something simmilar.

  • actually, my app. display "add item" button. So, if "add item" clicked, then TextView and EditText that programatically created show up. My problem is, if i move to the next fragment, i want to display every TextViews and EditTexts that.. – wdyz Nov 16 '16 at 20:06
  • ...using StringBuilder. – wdyz Nov 16 '16 at 20:06
0

This feels like you should be looking at an adapter view such as a recyclerview. You might start to see some performance issues using a linearlayout.

Anyway. You could add the views to an list as you add them to the linearlayout.

List<TextView> items = new LinkedList<>();
List<EditText> quantities = new LinkedList<>();  
for(int i = 0; i < cartCount; i++) {
   ...
    childLayout.addView(item);
    childLayout.addView(quantity);
    items.add(item);
    quantities.add(quantity);
    linearLayout.addView(childLayout);
}

then loop through your views

for (TextView textView : items) {

}

for (EditText editText : quantities) {

}
Matthew Shearer
  • 2,715
  • 3
  • 23
  • 32
-1
item.getText().toString()
quantity.getText().toString()
Pein
  • 1,059
  • 3
  • 10
  • 31
  • please read my sourcecode above, bro. I need to get all TextViews text and all EditTexts text from all childLayout of LinearLayout. – wdyz Nov 16 '16 at 19:46