mercredi 11 février 2015

Storing in a Database?

I am new to Android and was wondering how to store data into database and access them. I referred Developer.Android , but it seems incomplete to me. Currently I am developing an application that consist of multiple EditText fields, and my requirement is to save these data into a db when a user enters data. here's my java code:



package labs.dc.cart.cart;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;


public class MainActivity extends ActionBarActivity {

//static int count = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


final Switch mySwitch = (Switch)findViewById(R.id.switchButton);
mySwitch.setChecked(false);
mySwitch.setOnCheckedChangeListener
(newCompoundButton.OnCheckedChangeListener() {
@Override
publicvoidonCheckedChanged
(CompoundButtonbuttonView,booleanisChecked)
{

if(isChecked)
{
LinearLayout pop=(LinearLayout)findViewById(R.id.linear);
pop.setVisibility(View.VISIBLE);
}

else
{

LinearLayout pop=(LinearLayout)findViewById(R.id.linear);
pop.setVisibility(View.INVISIBLE);
}

}
});



}




@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, 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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


here is the XML code:



<Switch
android:id="@+id/switchButton"
android:background="#199405"
android:text="@string/switchText"
android:alpha="0.6"
android:textOff="ADD"
android:textOn="COLLAPSE"
android:gravity="center"
android:switchMinWidth="14.5sp"
android:switchPadding="14.5sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout

android:id="@+id/linear"

android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">



<EditText
android:id="@+id/editTextOne"
android:inputType="text"
android:layout_marginTop="110dp"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<EditText
android:id="@+id/editTextTwo"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editTextThree"
android:inputType="text"
android:layout_marginTop="20dp"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<EditText
android:id="@+id/editTextFour"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>

<EditText
android:id="@+id/editTextFive"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editTextSix"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editTextSeven"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editTextEight"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editTextNine"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText10"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText11"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText12"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText13"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText14"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText15"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText16"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText17"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText18"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cacaca"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>
<EditText
android:id="@+id/editText19"
android:inputType="text"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1ec0e9"
android:alpha="0.5"
android:ems="13"
android:gravity="start"
android:typeface="monospace"
/>



</LinearLayout>
</ScrollView>


</LinearLayout>


Suggest me any beginners resource that'll help me to learn sqlite and content providers in Android. Also, please help me with the above case as well! Thanks,


Aucun commentaire:

Enregistrer un commentaire