-1

Iam doing an android application. In that i want to create dynamic controls(edittext) like android core contacts application. After entering data in the ediitext i want to save the data to sqlite database by clicking a button named save. As Iam new to android i dont have any idea to create dynamic controls and storing its row values. Please help me if anybody knows.

My code:

package com.xiaochaoyang.dynamicviews;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    // Parent view for all rows and the add button.
    private LinearLayout mContainerView;
    // The "Add new" button
    private Button mAddButton;

    // There always should be only one empty row, other empty rows will
    // be removed.
    private View mExclusiveEmptyView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.row_container);

        mContainerView = (LinearLayout) findViewById(R.id.parentView);
        mAddButton = (Button) findViewById(R.id.btnAddNewItem);

        // Add some examples
        inflateEditRow("Xiaochao");
        inflateEditRow("Yang");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // TODO: Handle screen rotation:
        // encapsulate information in a parcelable object, and save it
        // into the state bundle.

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // TODO: Handle screen rotation:
        // restore the saved items and inflate each one with inflateEditRow;

    }

    // onClick handler for the "Add new" button;
    public void onAddNewClicked(View v) {
        // Inflate a new row and hide the button self.
        inflateEditRow(null);
        //v.setVisibility(View.GONE);
    }

    // onClick handler for the "X" button of each row
    public void onDeleteClicked(View v) {
        // remove the row by calling the getParent on button
        mContainerView.removeView((View) v.getParent());            
    }

    // Helper for inflating a row
    private void inflateEditRow(String name) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.row, null);
        final ImageButton deleteButton = (ImageButton) rowView
                .findViewById(R.id.buttonDelete);
        final EditText editText = (EditText) rowView
                .findViewById(R.id.editText);


        if (name != null && !name.isEmpty()) {
            editText.setText(name);
        } else {
            mExclusiveEmptyView = rowView;
            //deleteButton.setVisibility(View.INVISIBLE);
        }

        // A TextWatcher to control the visibility of the "Add new" button and
        // handle the exclusive empty view.
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().isEmpty()) {
                    //mAddButton.setVisibility(View.GONE);
                    //deleteButton.setVisibility(View.INVISIBLE);

                    if (mExclusiveEmptyView != null
                            && mExclusiveEmptyView != rowView) {
                        mContainerView.removeView(mExclusiveEmptyView);
                    }
                    mExclusiveEmptyView = rowView;
                } else {

                    if (mExclusiveEmptyView == rowView) {
                        mExclusiveEmptyView = null;
                    }

                    mAddButton.setVisibility(View.VISIBLE);
                    deleteButton.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }
        });

        // Inflate at the end of all rows but before the "Add new" button
        mContainerView.addView(rowView, mContainerView.getChildCount() );
    }
}

My layouts:

Row Cointainer.xml

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

   <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btnAddNewItem"
        android:layout_width="21dp"
        android:layout_height="wrap_content"
        android:background="@drawable/transparent_background"
        android:gravity="center_vertical"
        android:onClick="onAddNewClicked"
        android:layout_gravity="right"
        android:paddingLeft="5dp"
        android:text="+"
        android:textColor="@android:color/black" />
  </ScrollView>
</LinearLayout>

Row.xml

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

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:singleLine="true"
        android:ems="10"
        android:hint="" >
        <requestFocus />
    </EditText>

    <Spinner
        android:id="@+id/spinnerCategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.15"
        android:entries="@array/categories"
        android:gravity="right" />

    <ImageButton
        android:id="@+id/buttonDelete"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/content_remove" 
        android:background="@drawable/transparent_background"
        android:contentDescription="@string/button_delete_row_description"
        android:onClick="onDeleteClicked"/>

</LinearLayout>
sundar
  • 27
  • 6
  • http://stackoverflow.com/questions/12524589/how-can-we-add-buttons-at-dynamic-positions-in-layout. Follow this link . It might be helps u. – Venkatesh Nov 18 '13 at 10:28
  • Or else. Create a layout with edittext and set their visibility into GONE. And set back to visible where do u want? In this case the area of edit text goes to nothing and their relative view accomplish this place – Venkatesh Nov 18 '13 at 10:31
  • @sundar can you tell me, have you found solution for your problem? if yes can you please put the code? thanks – Aoyama Nanami Nov 28 '13 at 03:40

1 Answers1

2

First of all, while posting your question in SO, please show what have you tried so far, post your logcat if your app is getting force closed.

To work with controls, please refer to http://developer.android.com/reference/packages.html There you find the methods available to work with controls.

Check this link: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ It helps to learn and perform CRUD operations on SQLite Database.

At last, what you left with is, get data (or input) from controls, and insert into database.

Also, please go for googling.

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174