如何将动态列添加到我的表?

时间:2015-09-15 04:58:30

标签: android android-sqlite

我的应用程序中有一个表单,我正在以编程方式创建textview和edittext,根据用户要求,用户可以创建一些textview和edittext,点击添加按钮,现在问题是我无法存储所有动态创建的textview和edittexts的值到我的表...值nameofevent,日期,时间,持续时间正确存储在我的表中..以下是我的表单的图像..任何人都可以告诉错误是什么?

enter image description here

Mainactivity.java

public class MainActivity extends Activity {

    private Button addnewdata;
    private Button submit;
    private EditText edtnmofevent;
    private EditText edtdtofevent;
    private EditText edttmofevent;
    private EditText edtdurationofevent;
     private LinearLayout lnr;
        private TextView valueTV;
        private EditText edtvalues;

        int totalFields = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

          addnewdata = (Button) findViewById(R.id.addnewdata);
            submit = (Button) findViewById(R.id.btnsubmit);
            edtnmofevent = (EditText) findViewById(R.id.edtnameofevent);
            edtdtofevent = (EditText) findViewById(R.id.edtdateofevent);
            edttmofevent = (EditText) findViewById(R.id.edttimeofevent);
            edtdurationofevent = (EditText) findViewById(R.id.edtdurationofevent); 

            addnewdata.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LayoutInflater li = LayoutInflater.from(MainActivity.this);
                    View promptsView = li.inflate(R.layout.prompts, null);
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            MainActivity.this);
                    alertDialogBuilder.setView(promptsView);
                    final EditText userInput = (EditText) promptsView
                            .findViewById(R.id.editTextDialogUserInput);
                    alertDialogBuilder
                            .setCancelable(false)
                            .setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {


                                        public void onClick(DialogInterface dialog, int id) {

                                               lnr = (LinearLayout) findViewById(R.id.addnewlinear);
                                                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                                        LinearLayout.LayoutParams.WRAP_CONTENT);
                                                lp.setMargins(25, 0, 0, 0);
                                                valueTV = new TextView(MainActivity.this);
                                                // valueTV.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                                                valueTV.setText(userInput.getText());
                                                valueTV.setLayoutParams(lp);
                                                valueTV.setTextSize(18);
                                                valueTV.setTextColor(Color.parseColor("#2d6cae"));
                                                LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                        LinearLayout.LayoutParams.WRAP_CONTENT);
                                                lp1.setMargins(25, 0, 25, 0);
                                                lp1.height = 50;
                                                edtvalues = new EditText(MainActivity.this);
                                                edtvalues.setBackgroundResource(R.drawable.rect_edt);
                                                edtvalues.setLayoutParams(lp1);
                                                lnr.addView(valueTV);
                                                lnr.addView(edtvalues);
                                        }
                                    })
                            .setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            dialog.cancel();
                                        }
                                    });
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();

                }
            });

            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String edcity=edtnmofevent.getText().toString();
                    String dtofevent = edtdtofevent.getText().toString();
                    String timeofevent = edttmofevent.getText().toString();
                    String duration = edtdurationofevent.getText().toString();

                    queries q=new queries(MainActivity.this);
                  //  q.insert(edcity, dtofevent, timeofevent, duration);


                        String dlabl=valueTV.getText().toString();
                        String dedt=edtvalues.getText().toString();
                        q.insertevnt(dlabl,dedt);
                        System.out.print("values of textand edit"+valueTV.getText().toString());

                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    startActivity(intent);
                }
            });
    }


}

queries.java

public class queries {
    SQLiteDatabase sd;
    public queries(Context c)
    {
        db_handler dh=new db_handler(c);
        sd=dh.getWritableDatabase();
    }
    public void insert(String city,String dateofevent,String timeofevent,String durationofevent)
    {
        String query="insert into tbl_spin(city,dateofevent,timeofevent,durationofevent)values('"+city+"','"+dateofevent+"','"+timeofevent+"','"+durationofevent+"')";
        sd.execSQL(query);
    }
    public Cursor select()
    {
     //  String query="select tbl_spin.city from tbl_spin INNER JOIN tbl_event ON tbl_spin.id=tbl_event.id";
      String query="select * from tbl_event";
        //String query="select city from tbl_spin CROSS JOIN tbl_event";
        Cursor c=   sd.rawQuery(query, null);
        return c;
    }

   public void insertevnt(String txtofevent,String edtofevent)
    {
        String querys="insert into tbl_event(txtofevent,edtofevent)values('"+txtofevent+"','"+edtofevent+"')";
        sd.execSQL(querys);
    }

    /*public Cursor selectevent()
    {
        String querys="select * from tbl_event";
        Cursor c=   sd.rawQuery(querys, null);
        return c;
    }*/

}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"


    >

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#000000"
        android:gravity="center"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="IT ADD$"
            android:id="@+id/itaddestxt"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:textSize="20sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="UP"
            android:textColor="#ffffff"
            android:layout_toRightOf="@+id/itaddestxt"
            android:textSize="20sp" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:id="@+id/addnewlinear"
            android:orientation="vertical"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ADD NEW EVENT"
                android:layout_marginTop="10dp"
                android:textSize="15dp"
                android:id="@+id/txtaddnewevent"
                android:textColor="#73b5fa"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txtaddnewevent"
                android:layout_marginTop="10dp"
                android:id="@+id/bluelines"
                />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name of Event:*"
                android:layout_below="@+id/bluelines"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="15dp"
                android:textSize="18dp"
                android:textColor="#2d6cae"
                android:id="@+id/txtnameofevent"
                />


            <EditText
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:id="@+id/edtnameofevent"
                android:layout_below="@+id/txtnameofevent"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="15dp"
                />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date of Event:*"
                android:layout_below="@+id/edtnameofevent"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="15dp"
                android:textSize="18dp"
                android:textColor="#2d6cae"
                android:id="@+id/txtdateofevent"
                />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txtdateofevent"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:id="@+id/reledtdate"
                >

            <EditText
                android:layout_width="250dp"
                android:layout_height="30dp"
                android:id="@+id/edtdateofevent"

                />

                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_toRightOf="@+id/edtdateofevent"
                    android:layout_marginLeft="10dp"
                    android:id="@+id/calndrdat"
                    />
            </RelativeLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Time of Event:*"
                android:layout_below="@+id/reledtdate"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="15dp"
                android:textSize="18dp"
                android:textColor="#2d6cae"
                android:id="@+id/txttimeofevent"
                />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txttimeofevent"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:id="@+id/reledttime"
                >
            <EditText
                android:layout_width="250dp"
                android:layout_height="30dp"
                android:id="@+id/edttimeofevent"

                />

                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_toRightOf="@+id/edttimeofevent"
                    android:layout_marginLeft="10dp"
                    android:id="@+id/timepickrs"
                    />
            </RelativeLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Duration of Event:*"
                android:layout_below="@+id/edttimeofevent"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="15dp"
                android:textSize="18dp"
                android:textColor="#2d6cae"
                android:id="@+id/txtdurationofevent"
                />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:id="@+id/edtdurationofevent"
                android:layout_below="@+id/txtdurationofevent"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="15dp"
                />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/edtdurationofevent"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:paddingBottom="10dp"
                android:id="@+id/addnewdata"
                android:text="Add"
                />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Submit"
                android:textColor="@android:color/white"
                android:layout_gravity="center"
                android:id="@+id/btnsubmit"
                />

            </LinearLayout>


        </ScrollView>

</RelativeLayout>

db_handler

public class db_handler extends SQLiteOpenHelper{

    public db_handler(Context context
    ) {
        super(context, "databs.db", null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table tbl_spin(id integer primary key autoincrement,city text,dateofevent text,timeofevent text,durationofevent text)");
        db.execSQL("create table tbl_event(id integer primary key autoincrement,txtofevent text,edtofevent text)");


       // db.execSQL("CREATE TABLE COMPANY(ID INT PRIMARY KEY NOT NULL,NAME CHAR(50) NOT NULL,AGE CHAR(50) NOT NULL,ADDRESS CHAR(50) NOT NULL,SALARY CHAR(50) NOT NULL);");
      //  db.execSQL("CREATE TABLE DEPARTMENT(ID INT PRIMARY KEY NOT NULL,DEPT CHAR(50) NOT NULL,EMP_ID INT NOT NULL);");


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

0 个答案:

没有答案