Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Main activity.java
Java
package com.example.controlsettings;

import android.os.Bundle;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

public class MainActivity extends Activity {
	 
private TabHost myTabHost;
SQLiteDatabase db;
Button b1,b2;
     
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Recuperation du TabHost
    myTabHost =(TabHost) findViewById(R.id.TabHost01);
// Before adding tabs, it is imperative to call the method setup()
                myTabHost.setup();
 
// Adding tabs
    // tab1 settings
        TabSpec spec = myTabHost.newTabSpec("Control");
    // text and image of tab
        spec.setIndicator("Control Device",getResources()
        		.getDrawable(android.R.drawable.ic_menu_agenda));
    // specify layout of tab
        spec.setContent(R.id.Control);
    // adding tab in TabHost
        myTabHost.addTab(spec);
 
// otherwise :
      myTabHost.addTab(myTabHost.newTabSpec("Settings").
		setIndicator("Settings",getResources().getDrawable(android.R.drawable.ic_menu_edit)).
		setContent(R.id.Settings));
     //storing the values to database
      b1=(Button)findViewById(R.id.butnsave);
      b2=(Button)findViewById(R.id.butncancel);
      try{
      db=openOrCreateDatabase("Configrtn_Settings",SQLiteDatabase.CREATE_IF_NECESSARY,null);
      Toast.makeText(MainActivity.this, "Database created successfully", 2012).show();
      db.execSQL("Create Table Config(ip text(15),port integer(4));");
      Toast.makeText(MainActivity.this, "Table created successfully", 2012).show();
      }catch(SQLException e)
      {
      }
      b1.setOnClickListener(new View.OnClickListener() {
    	  @Override
           public void onClick(View v) {
           // TODO Auto-generated method stub
           EditText cip=(EditText) findViewById(R.id.editText1);
           EditText cport=(EditText)findViewById(R.id.EditText01);
           ContentValues values=new ContentValues();
           values.put("IP Address", cip.getText().toString());
           values.put("PORT", cport.getText().toString());
           
           if((db.insert("config", null, values))!=-1)
           {
           Toast.makeText(MainActivity.this, "Record Successfully Inserted", 2014).show();
           }
           else
           {
           Toast.makeText(MainActivity.this, "Insert Error", 2014).show();
           }
           cip.setText("");
           cport.setText("");
           Cursor c=db.rawQuery("SELECT * FROM config",null);
           c.moveToFirst();
           while(!c.isAfterLast())
           {
           Toast.makeText(MainActivity.this,c.getString(0)+ " "+c.getString(1),1000).show();
           c.moveToNext();
           }
           c.close();
           }
        });    
    }
@Override
protected void onStop() {
  // TODO Auto-generated method stub
  db.close();
  super.onStop();
}
}

control .xml
HTML
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/butn3on"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/butn2on"
        android:layout_alignTop="@+id/TextView02"
        android:text="On" />

    <Button
        android:id="@+id/butn3off"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/butn2off"
        android:layout_alignTop="@+id/butn3on"
        android:text="Off" />

    <Button
        android:id="@+id/butn4on"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/butn3on"
        android:layout_alignTop="@+id/TextView03"
        android:text="On" />

    <Button
        android:id="@+id/butn4off"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/butn3off"
        android:layout_alignTop="@+id/butn4on"
        android:text="Off" />

    <Button
        android:id="@+id/butn5on"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/butn4on"
        android:layout_alignTop="@+id/TextView04"
        android:text="On" />

    <Button
        android:id="@+id/butn5off"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/butn4off"
        android:layout_alignTop="@+id/butn5on"
        android:text="Off" />

    <textview>
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView03"
        android:layout_below="@+id/butn4on"
        android:layout_marginTop="44dp"
        android:text="Light5"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <textview>
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView02"
        android:layout_below="@+id/butn3on"
        android:layout_marginTop="39dp"
        android:text="Light4"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <textview>
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView01"
        android:layout_below="@+id/butn1on"
        android:layout_marginTop="120dp"
        android:text="Light3"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <textview>
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/butn1on"
        android:layout_marginTop="39dp"
        android:text="Light2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/butn2off"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/butn2on"
        android:layout_alignBottom="@+id/butn2on"
        android:layout_alignLeft="@+id/butncancel"
        android:text="Off" />

    <Button
        android:id="@+id/butn2on"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/butn1on"
        android:layout_alignTop="@+id/TextView01"
        android:text="On" />

    <Button
        android:id="@+id/butncancel"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/butn1on"
        android:layout_alignBottom="@+id/butn1on"
        android:layout_marginLeft="38dp"
        android:layout_toRightOf="@+id/butn1on"
        android:text="Off" />

    <Button
        android:id="@+id/butn1on"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="On" />

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="28dp"
        android:layout_marginTop="68dp"
        android:layout_toLeftOf="@+id/butn1on"
        android:text="Light1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</textview></textview></textview></textview></textview></relativelayout>

settings.xml
HTML
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView2"
        android:layout_marginTop="70dp"
        android:text="IP"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <edittext>
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView1"
        android:ems="10"
        android:hint="Enter your IP address"
        android:inputType="phone" />

    <edittext>
        android:id="@+id/EditText01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:hint="Enter your port number"
        android:inputType="phone" />

    <textview>
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="75dp"
        android:text="Port"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/butnsave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/EditText01"
        android:layout_marginTop="58dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="Save" />

    <Button
        android:id="@+id/butncancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/butnsave"
        android:layout_alignBottom="@+id/butnsave"
        android:layout_alignRight="@+id/EditText01"
        android:layout_marginRight="21dp"
        android:text="Cancel" />

</textview></edittext></edittext></textview></relativelayout>


the database and table gets created whereas the values doesn't gets inserted....shows d error message which i have given in the toast....pls help
Posted
Updated 15-Jul-13 5:11am
v4
Comments
Shubhashish_Mandal 15-Jul-13 11:04am    
If you post your queries like this then nobody can help you. People skip to read this post .
Try to post more precise and minimal portion of the code , and also post your code inside the code block .
User1454 9-Oct-13 0:39am    
Sorry sir....i was new to this......I will do it properly here on.....
shanmuga1509 16-Jul-13 5:03am    
Edit the xml it is having lots of errors and activity.java too

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900