Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value from dynamically created textview in android

MainActivity's code

How can I get values from the text boxes and the qty box in that user will input the value. price will come from server and than i want to calculate the bill and show it in next layout. plz give me some easy explanation as i am not expert in it. thanks in advance.

MainActivity .java

package com.example.myone;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
     EditText textIn;
     Button buttonAdd;
     LinearLayout container;
     private Button done;
     private TextView tv;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              textIn = (EditText)findViewById(R.id.textin);
              buttonAdd = (Button)findViewById(R.id.add);
              container = (LinearLayout)findViewById(R.id.container);
              done = (Button) findViewById(R.id.cal);
              tv= (TextView) findViewById(R.id.tv);

             buttonAdd.setOnClickListener(new OnClickListener(){

              @Override
               public void onClick(View arg0) {
                LayoutInflater layoutInflater = 
                  (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.row, null);
                TextView textOut = (TextView)addView.findViewById(R.id.textout);
                textOut.setText(textIn.getText().toString());

                //(int1);

                Button buttonRemove = (Button)addView.findViewById(R.id.remove);
                buttonRemove.setOnClickListener(new OnClickListener(){

                 @Override
                 public void onClick(View v) {
                  ((LinearLayout)addView.getParent()).removeView(addView);
                 }});
                container.addView(addView);
               }});

      }

}

MainActivity.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@drawable/mainbg"
    tools:context=".MainActivity" >


  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/textin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Enter your item here"
            />
        <Button 
            android:id="@+id/cal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_below="@+id/textin"
            android:text="Done!"
            android:layout_alignParentLeft="true"
            />
        <Button 
            android:id="@+id/add"
            android:text="Add"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignBaseline="@+id/cal"
            />

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

</LinearLayout>

Row.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@drawable/borderbg"
    android:orientation="horizontal"
    android:id="@+id/rel"
    android:layout_height="wrap_content">

    <TextView 
        android:id="@+id/textout"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:hint="Item"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
                />

    <EditText
        android:id="@+id/qty"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignTop="@+id/price"
        android:layout_toRightOf="@+id/price"
        android:layout_marginLeft="50dp"
        android:textSize="12sp"
        android:hint="Qty" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textout"
        android:layout_alignBottom="@+id/textout"
        android:layout_marginLeft="42dp"
        android:layout_toRightOf="@+id/textout"

        android:hint="Price" />

    <Button
        android:id="@+id/remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="left"
        android:text="remove" />

</RelativeLayout>

Simply I want to create a basic shopping app that have 20 products. the price and product name will come from server and quantity will input by user so how do I that?

like image 734
Devendra singh Avatar asked Feb 07 '26 17:02

Devendra singh


1 Answers

You are looking for something like this

int count = container.getChildCount(); 
for (int i = 0; i < count; i++) {
   final View row = container.getChildAt(i);
   TextView textOut = (TextView)row.findViewById(R.id.textout);
   String data = textOut.getText().toString();
}

Where container is your LinearLayout in which you have added your inflated rows.

like image 127
Nitesh Garg Avatar answered Feb 09 '26 10:02

Nitesh Garg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!