Hi everyone,

I have a table (RoomName) who has 3 columns (RoomID, RoomName,
RoomSuffix). RoomID is primary field. what I want is if suppose user
enter duplicate RomName then that Rooms RoomSuffix value should be
increased by 1.
Suppose there are following record already

RoomID        RoomName        RoomSuffix
    1                  Hall                      0
    2                  Kitchen                0
    3                  BedRoom             0

Now if user enter another Hall then d/b must be

  4                   Hall                     1
  5                    Hall                    2
  6                    Kitchen               1

I have following code

public class AddRoom extends Activity
{
        ListView roomList;
        String[] roomArray = {"Hall","Kitchen","BedRoom","Dining
Hall","BathRoom","Study Room","Guest Room","Other"};
        String roomName,roomname;
        int roomSuffix,roomSX;
        boolean roomExist;
        String SAMPLE_DBNAME = "NewHomeAutoDataBase";
        String ROOM_TABLE_NAME = "RoomTable";
        SQLiteDatabase sampleDB = null;
        public void onCreate(Bundle savedInstanceState)
        {
                System.out.println ("First stmt in onCreate of AddRoom");
                super.onCreate(savedInstanceState);
                setContentView(R.layout.addroom);

                roomList = (ListView)findViewById(R.id.list);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listitem, roomArray);
                roomList.setAdapter(adapter);


                //add listener on which list item user click

                roomList.setOnItemClickListener(new OnItemClickListener()
                {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View 
view, int
position,long id)
                        {
                                roomName = (String) 
roomList.getItemAtPosition(position);
                                System.out.println ("User click on
"+roomList.getItemAtPosition(position));

                                if (roomName.equalsIgnoreCase("other"))
                                {
                                        System.out.println ("Inside if 
condition. i.e User click on other
room");
                                }

                                createRoomTable();
                                roomExist = checkRoomExistOrNot(roomName);

                                if (roomExist)
                                {
                                        System.out.println ("Room existed");
                                        int test = getRoomSuffix(roomName);
                                        roomSX = ++test;
                                        System.out.println ("Value return by 
getRoomSuffix()"+roomSX);
                                        insertToRoomTable1(roomName,roomSX);
                                        return;
                                }

                                /*
                                 * Now add room to roomtable
                                 */
                                insertToRoomTable(roomName);
                        }
                });
        }

        public void createRoomTable()
        {
                /* This method first create room table if it is not exist
                 * table has 3 columns 1.RoomID 2.RoomName 3.RoomSuffix
                 */

                sampleDB =  this.openOrCreateDatabase(SAMPLE_DBNAME, 
MODE_PRIVATE,
null);

                sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                ROOM_TABLE_NAME +
                " (RoomID integer primary key autoincrement,RoomName
VARCHAR,RoomSuffix integer);");

                sampleDB.close();
        }

        public boolean checkRoomExistOrNot(String rName)
        {
                /* check inside room table for room exist or not.
                 *
                 */
                System.out.println ("Here rName is "+rName);
                sampleDB =  this.openOrCreateDatabase(SAMPLE_DBNAME, 
MODE_PRIVATE,
null);
                Cursor c = sampleDB.rawQuery("SELECT RoomName FROM "
+ROOM_TABLE_NAME , null);

                if (c != null )
        {
                System.out.println ("Total number of records in room table
are"+c.getCount());

                if  (c.moveToFirst())
                {
                        do
                        {
                                roomname = 
c.getString(c.getColumnIndex("RoomName"));

                                System.out.println ("RoomName is "+roomname);

                                if (roomname.equalsIgnoreCase(rName))
                                {
                                        System.out.println ("Room Exist in room 
table");

                                        return true;
                                }

                        }
                        while (c.moveToNext());
                }
        }

        c.close();
        sampleDB.close();
        return false;

        }


        public void insertToRoomTable(String rName)
        {
                sampleDB =  this.openOrCreateDatabase(SAMPLE_DBNAME, 
MODE_PRIVATE,
null);

                sampleDB.execSQL("INSERT INTO " +
                        ROOM_TABLE_NAME +
                        " Values (null,'"+rName+"','"+roomSuffix+"');");

        System.out.println ("Inserted values into RoomTable are"+rName
+":"+roomSuffix);
        sampleDB.close();
        }

        public void insertToRoomTable1(String rName,int rx)
        {
                sampleDB =  this.openOrCreateDatabase(SAMPLE_DBNAME, 
MODE_PRIVATE,
null);

                sampleDB.execSQL("INSERT INTO " +
                        ROOM_TABLE_NAME +
                        " Values (null,'"+rName+"','"+roomSuffix+"');");

        System.out.println ("Inserted values into RoomTable are"+rName
+":"+roomSuffix);
        sampleDB.close();
        }

        public int getRoomSuffix(String name)
        {
                sampleDB =  this.openOrCreateDatabase(SAMPLE_DBNAME, 
MODE_PRIVATE,
null);
                Cursor c = sampleDB.rawQuery("SELECT RoomSuffix FROM "
+ROOM_TABLE_NAME +" WHERE RoomName= '"+name+"'" , null);
                if (c != null )
        {
                System.out.println ("Total number of records for RoomSuffix
are"+c.getCount());

                if  (c.moveToFirst())
                {
                        do
                        {
                                roomSuffix = 
c.getInt(c.getColumnIndex("RoomSuffix"));

                                System.out.println ("RoomSuffix is 
"+roomSuffix);
                        }
                        while (c.moveToNext());
                }
        }

        c.close();
        sampleDB.close();

        return roomSuffix;

        }
}


But my problem is that every time getRoomSuffix(String str) method
returns value 0. Instead of that it must return next suffix value. i.e
IF user entered 2nd time Hall entry then it must return 1, for 3rd
time 2 and so on....

Thanks

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to