Adding new fields dynamically

9

I'm creating an app that calculates credits from a school report.

DUVIDA : How to add a new EditText when the user clicks the next "+" button and sums the values of each EditText .

    
asked by anonymous 11.09.2016 / 05:17

2 answers

7

Simple example of EditText with ListView :

XML - { main.xml }

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="onClick"
        android:text="Add" />

    <ListView
        android:id="@+id/listViewMain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:descendantFocusability="beforeDescendants"></ListView>
</LinearLayout>

XML - { item_edittext.xml }

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/ItemCaption"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dip"
        android:singleLine="true"></EditText>

</LinearLayout>

Java - { Main.class }

public class Main extends AppCompatActivity implements View.OnClickListener {
    private Button btnAdd;

    private ArrayList arrTemp = new ArrayList();

    private ArrayList array = new ArrayList();
    MyListAdapter myListAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(this);

        array.add("teste");

        arrTemp.add(array);

        myListAdapter = new MyListAdapter();
        ListView listView = (ListView) findViewById(R.id.listViewMain);
        listView.setAdapter(myListAdapter);

    }

    public void onClick(View view) {
        array.add("teste");
        myListAdapter.notifyDataSetChanged();
    }

    private class MyListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (array != null && array.size() != 0) {
                return array.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return array.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {

                holder = new ViewHolder();
                LayoutInflater inflater = Main.this.getLayoutInflater();
                convertView = inflater.inflate(R.layout.item_edittext, null);

                holder.editText1 = (EditText) convertView.findViewById(R.id.ItemCaption);

                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();
            }

            holder.ref = position;

            return convertView;
        }

        private class ViewHolder {
            EditText editText1;
            int ref;
        }
    }
}
    
11.09.2016 / 17:29
4

I believe you can add a new botton with addView.

Example: "linearLayout.addView (botton);"

Example I have here:

View view = getActivity().getLayoutInflater().inflate(R.layout.scroll_crews, (ViewGroup) getView(), false);
LinearLayout linearLayout = (LinearLayout) getView().findViewById(R.id.scroll_crew_liner);
View layoutScroll = view.findViewById(R.id.scroll_crews_linearlayout);    
TextView textCrewJob = (TextView) view.findViewById(R.id.textCrewJob);    
TextView textCrewNome = (TextView) view.findViewById(R.id.textCrewNome);
ImageView imgPagerCrews = (ImageView) view.findViewById(R.id.imgPagerCrews);
ProgressBar progressBarCrew = (ProgressBar) view.findViewById(R.id.progressBarCrews);    
linearLayout.addView(layoutScroll);

In the example, I inflate an xml, and with it I edit what I need and after that I add it in another layout.

Where you need to add the botton, from a findViewById () in the layout and with it, speaks a layout.addView ();

    
13.09.2016 / 04:19