mercredi 8 juillet 2015

How can i add multiple values into one column in sql?

i am creating app where you can register a person and then add it to list, next u can view this list with all info Name, Surname, Time, Date. But i dont know how to do it ? First of all i am countering a problem of sqlite exception that i cant set 4 values to one column. Secondly i dont know how to make custom list view where i can set Name is Item, Surname as subItem, in one column.

There is my Permission class, where i handle all edit text fields and button ok to add all strings to list.

  package com.example.pass;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;




public class Permission extends ActionBarActivity{
 EditText Date;
 EditText Time;
 EditText userName;
 EditText userSurname;

 EditText Name;
 String Surname;

 //INITIALIZING THE VARIABLES...
 Button b1,b2;
 EditText e1;
 String text,u,text2,text3,text4;
 String un[]={"","","","","","","","","","","","","","",""};
 String pas;
 Cursor c;
 int i=0;
 String s,sqlquery;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_permission);

    Button add = (Button) findViewById(R.id.add);
    Button cancel = (Button) findViewById(R.id.cancel);


    // Create custom dialog object          
    // set values for custom dialog components - text, image and button
    userName = (EditText) findViewById(R.id.dialog_username);
    userSurname = (EditText)findViewById(R.id.dialog_usersurname);
    Time = (EditText)findViewById(R.id.dialog_time2);
    Date = (EditText)findViewById(R.id.dialog_date2);   


           Date.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //To show current date in the datepicker
                    Calendar mcurrentDate=Calendar.getInstance();
                    int mYear = mcurrentDate.get(Calendar.YEAR);
                    int mMonth = mcurrentDate.get(Calendar.MONTH);
                    int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog mDatePicker=new DatePickerDialog(Permission.this, new OnDateSetListener() {                  
                        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                            // TODO Auto-generated method stub                      
                            /*      Your code   to get date and time    */
                            Date.setText(selectedyear+"-"+(selectedmonth+1)+"-"+selectedday);
                        }
                    },mYear, mMonth, mDay);
                    mDatePicker.setTitle("Select date");                
                    mDatePicker.show();  }
            });


           Time.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   //To show current date in the datepicker

                   Calendar mcurrentTime=Calendar.getInstance();
                   int mHour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                   int mMinute = mcurrentTime.get(Calendar.MINUTE);


                   TimePickerDialog mTimePicker=new TimePickerDialog(Permission.this, new OnTimeSetListener() {                  
                       public void onTimeSet(TimePicker timepicker, int selectedhour, int selectedminute ) {
                           // TODO Auto-generated method stub                      
                           /*      Your code   to get date and time    */
                          if (selectedminute<10){
                              Time.setText(selectedhour+":"+"0"+(selectedminute)+""+"val");
                          }
                          else
                        Time.setText(selectedhour+":"+(selectedminute));
                       }
                   },mHour, mMinute, false);
                   mTimePicker.setTitle("Select date");                
                   mTimePicker.show();  }
           });


          cancel.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   // TODO Auto-generated method stub
                   //To show current date in the datepicker
                    startActivity(new Intent(Permission.this, Choose.class));
                  Permission.this.finish();
               }     
           });

          add.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  text=userName.getText().toString();
                  text2=userSurname.getText().toString();
                  text3=Date.getText().toString();
                  text4=Time.getText().toString();
                  //CREATING A DATABASE OBJECT..HERE db is SQLITEDATABASE OBJECT AND
                  // todo is our database name AND
                  //WE ARE USING IT IN WRITE MODE
                  SQLiteDatabase db=openOrCreateDatabase("todo", Context.MODE_WORLD_WRITEABLE, null);

                  text="'"+text+"'";
                  text2="'"+text2+"'";
                  text3="'"+text3+"'";
                  text4="'"+text4+"'";

          //SQL QUERY TO CREATE TABLE
          s="CREATE TABLE if not exists TODOLIST"+" ("+ "list" +" VARCHAR(100)"+");";
                  db.execSQL(s);

                  // QUERY TO INSERT THE DATA INTO TABLE
                  sqlquery="INSERT INTO TODOLIST"+ " VALUES"+"("+text+","+text2+","+text3+","+text4+");";
                  db.execSQL(sqlquery);
                  userName.setText("");

                  Toast.makeText(getApplicationContext(), "TASK ADDED IN LIST", Toast.LENGTH_SHORT).show();
              }     
          });

        }


    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.permission, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}









  }

this is my permission xml file.

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://ift.tt/nIICcg"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <EditText
    android:id="@+id/dialog_username"
    android:hint="Name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

  </EditText>
  <EditText
    android:id="@+id/dialog_usersurname"
    android:hint="Surname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
  </EditText>
  <EditText
    android:id="@+id/dialog_date2"
    android:hint="Enter date"
    android:focusable="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
  </EditText>
  <EditText
    android:id="@+id/dialog_time2"
    android:hint="Enter time"
    android:focusable="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
 </EditText>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Cancel" />

</LinearLayout>


   </LinearLayout>

this is my detail class, it handles list view with back and clear buttons, so in this place i want to make R.layout.simple_expandable_list_item_1 custom and i dont know how? As i said that Name will be Item, surname as subItem, and show them both in one column.

   package com.example.pass;

 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemLongClickListener;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ListView;
 import android.widget.Toast;

 public class Detail extends Activity {

    Button b, clr;
    ListView lv;
    String query;
    Cursor c;
    String uns[] = { "", "", "", "", "", "", "","","",""};
    int i = 0;
    int t = 0;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.detail);
            b = (Button) findViewById(R.id.btn);
            clr = (Button) findViewById(R.id.clear);
            lv = (ListView) findViewById(R.id.listView1);

            // Declaring arrayadapter to store the items and return them as a view
            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_expandable_list_item_1, uns);

            db = openOrCreateDatabase("todo", Context.MODE_WORLD_READABLE, null);

            //READING THE DATA FROM THE TABLE TODOLIST AND SETTING IN AN      ARRAYADAPTER...HERE C IS A CURSOR...
            //WHICH POINTS TO A ROW..AND MOVE TO THE NEXT ROW....
            c = db.rawQuery("SELECT * FROM TODOLIST; ", null);
            i = 0;
            while (c.moveToNext()) {

                    uns[i] = c.getString(0);
                    i++;
            }
            t = i;

            lv.setAdapter(adapter);

            // clear all the data from table..
            clr.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                            // TODO Auto-generated method stub

                            db = openOrCreateDatabase("todo",     Context.MODE_WORLD_WRITEABLE,
                                            null);

                            //QUERY TO DELETE ALL THE DATA FROM TABLE
                            db.delete("TODOLIST", null, null);

                            db.close();

                            for (int j = 0; j < t; j++) {
                                    uns[j] = "";
                                    lv.setAdapter(adapter);
                            }

                    }
            });

            //GOING BACK TO MAIN ACTIVITY
            b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                            // TODO Auto-generated method stub
                            startActivity(new     Intent(getApplicationContext(),
                                            MainActivity.class));
                    }
            });


    }

 }

detail activity xml file

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://ift.tt/nIICcg"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back" />

    <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear all" />

 </LinearLayout>

 <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>

 </LinearLayout>

my list looks like that

enter image description here

and register fields

enter image description here

Aucun commentaire:

Enregistrer un commentaire