Daten von einem spinner in eine database speichern

  • 0 Antworten
  • Letztes Antwortdatum
M

Michi_Wolf123

Neues Mitglied
0
Hallo
Ich weiß das für viele das ein klax ist aber ich weis nicht mehr weiter.
Ich möchte in meiner Activity insert einem Spinner erstellen und in meiner Datenbank HTLDb speichern. In meiner Activity anzeigen habe ich ein Listview erstellt dort soll der Text drinnenstehen. Weiters möchte ich in meiner activity updaten die eingabe ändern.
Hier sind alle meine Cods:
Layout von der insert Activity
<LinearLayout 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"
android:orientation="vertical"
tools:context="at.atn.android.InsertActivity" >

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_zuname" />

<EditText
android:id="@+id/edZuname"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:ems="10"
android:inputType="textPersonName" >

<requestFocus />
</EditText>

</LinearLayout>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_start"/>

<EditText
android:id="@+id/edStart"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:ems="10"
android:inputType="time" >

</EditText>



</LinearLayout>


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_vorname" />

<EditText
android:id="@+id/edVorname"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:ems="10"
android:inputType="textPersonName" >
</EditText>

</LinearLayout>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_geschlecht" />

<EditText
android:id="@+id/edGeschlecht"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:ems="10"
android:inputType="textPersonName" >
</EditText>

<Button
android:id="@+id/btInsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bt_anlegen"
android:onClick="klick" />

</LinearLayout>


</LinearLayout> Insert code:
public class InsertActivity extends Activity {
private HTLDb myDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insert);
myDB = new HTLDb(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.insert, 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);
}

public void klick(View v) {
EditText edZuname = (EditText)findViewById(R.id.edZuname);
EditText edStart = (EditText) findViewById(R.id.edStart);
EditText edVorname = (EditText)findViewById(R.id.edVorname);
EditText edGeschlecht = (EditText)findViewById(R.id.edGeschlecht);
myDB.insertPupil(edZuname.getText().toString(),edStart.getText().toString(), edVorname.getText().toString(),
edGeschlecht.getText().toString());
}
}
Meine HTLDb:
public class HTLDb extends SQLiteOpenHelper {
private static final String DB_NAME = "htldb";
private static final int DB_VERSION = 2;

public HTLDb(Context context) {
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table klasse (_id integer primary key autoincrement," +
"zuname text not null,start text, vorname text, geschlecht text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table klasse");
onCreate(db);
}

public Cursor getAllPupil(String sort) {
SQLiteDatabase db = getReadableDatabase();
return db.query("klasse", new String[]{"_id","zuname","start","vorname","geschlecht"},
null,null,null,null,sort);
}

public void insertPupil(String zuname,String start,String vorname, String geschlecht) {
ContentValues values = new ContentValues();
values.put("zuname", zuname);
values.put("start", start);
values.put("vorname", vorname);
values.put("geschlecht", geschlecht);

SQLiteDatabase db = getWritableDatabase();
db.insert("klasse", null, values);
db.close();
}

public void updatePupil(String id,String zuname,String start,String vorname, String geschlecht) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("update klasse set zuname=?,start=?, vorname=?,geschlecht=? where _id=?",
new String[]{zuname,start,vorname,geschlecht,id});
db.close();
}

public void deletePupil(int id) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("delete from klasse where _id=" + id);
db.close();
}

public int getAnz() {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery("select * from klasse", null);
return c.getCount();
}

// Select eines Schülers nach ID
public String getPupil(int id) {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query("klasse", new String[]{"_id","zuname","start","vorname","geschlecht"},
"_id=?", new String[]{String.valueOf(id)}, null, null, null);
if (c != null)
{
c.moveToFirst();
// Rückgabe "zuname;vorname;geschlecht";
return c.getString(1) + ";" + c.getString(2) + ";" + c.getString(3) + ";"+ c.getString(4);
}
else
return "Falsche ID";
}
}
Mein Layout von der anzeigen activity:
<LinearLayout 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:orientation="vertical"
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="at.atn.android.AnzeigenActivity" >

<TextView
android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_no_pupils" />

<Spinner
android:id="@+id/spSort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/sort" />

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

</LinearLayout>
anzeigen code:
public class AnzeigenActivity extends ListActivity {
private HTLDb myDB;

private AdapterView.OnItemSelectedListener myListener =
new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Cursor cSort = null;
switch(position) {
case 0:
cSort = myDB.getAllPupil("_id");
break;
case 1:
cSort = myDB.getAllPupil("zuname");
break;
case 2:
cSort = myDB.getAllPupil("start");
break;
case 3:
cSort = myDB.getAllPupil("vorname");
break;
case 4:
cSort = myDB.getAllPupil("geschlecht");
break;
}
ListView v = getListView();
SimpleCursorAdapter cAdapt = (SimpleCursorAdapter)v.getAdapter();
cAdapt.changeCursor(cSort);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.anzeigen);
Spinner sp = (Spinner)findViewById(R.id.spSort);
sp.setOnItemSelectedListener(myListener);

myDB = new HTLDb(this);
Cursor c = myDB.getAllPupil("_id");

SimpleCursorAdapter pupilAdapter = new SimpleCursorAdapter(this,
R.layout.row,c,new String[]{"_id","zuname","start","vorname","geschlecht"},
new int[]{R.id.txID,R.id.txZuname,R.id.txStart,R.id.txVorname,R.id.txGeschlecht});
setListAdapter(pupilAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.anzeigen, 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);
}
} Meine Layout für den ListView:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">





<TextView
android:id="@+id/txID"
android:layout_width="wrap_content"
android:layout_weight="0.1"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingTop="2dip"
android:textSize="20sp" />


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txStart"
android:paddingRight="2dip"
android:paddingLeft="2dip"
android:paddingTop="2dip"
android:textSize="20sp"
android:layout_weight="0.10" />

<TextView
android:id="@+id/txZuname"
android:layout_width="0sp"
android:layout_weight="0.29"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingTop="2dip"
android:textSize="20sp" />

<TextView
android:id="@+id/txVorname"
android:layout_width="0sp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingTop="2dip"
android:textSize="20sp" />

<TextView
android:id="@+id/txGeschlecht"
android:layout_width="wrap_content"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingTop="2dip"
android:textSize="20sp" />


</TableLayout>

Layout von der Update activity:
<LinearLayout 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"
android:orientation="vertical"
tools:context="at.atn.android.UpdateActivity" >

<TextView
android:id="@+id/txID"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_zuname" />

<EditText
android:id="@+id/edZuname"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:ems="10"
android:inputType="textPersonName" >

<requestFocus />
</EditText>

</LinearLayout>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_start" />

<EditText
android:id="@+id/edStart"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:ems="10"
android:inputType="time" >
</EditText>

</LinearLayout>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_vorname" />

<EditText
android:id="@+id/edVorname"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:ems="10"
android:inputType="textPersonName" >
</EditText>

</LinearLayout>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_geschlecht" />

<EditText
android:id="@+id/edGeschlecht"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:ems="10"
android:inputType="textPersonName" >
</EditText>

<Button
android:id="@+id/btUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bt_aendern"
android:onClick="klick" />

</LinearLayout>

</LinearLayout>
Mein Code von der Update Activity:
public class UpdateActivity extends Activity {
private HTLDb myDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
myDB = new HTLDb(this);

// Anzeigen der ID
String id;
TextView txID = (TextView)findViewById(R.id.txID);
Bundle daten = getIntent().getExtras();
id = daten.getString("id");
txID.setText(id);

// Holen und Anzeigen von Zuname, Vorname und Geschlecht
String schuelerDaten = myDB.getPupil(Integer.parseInt(id));
String[] schuelerWerte = schuelerDaten.split(";");
EditText edZuname = (EditText)findViewById(R.id.edZuname);
EditText edStart = (EditText) findViewById(R.id.edStart);
EditText edVorname = (EditText)findViewById(R.id.edVorname);
EditText edGeschlecht = (EditText)findViewById(R.id.edGeschlecht);
edZuname.setText(schuelerWerte[0]);
edStart.setText(schuelerWerte[1]);
edVorname.setText(schuelerWerte[2]);
edGeschlecht.setText(schuelerWerte[3]);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.update, 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);
}

public void klick(View v) {
TextView txID = (TextView)findViewById(R.id.txID);
EditText edZuname = (EditText)findViewById(R.id.edZuname);
EditText edStart = (EditText) findViewById(R.id.edStart);
EditText edVorname = (EditText)findViewById(R.id.edVorname);
EditText edGeschlecht = (EditText)findViewById(R.id.edGeschlecht);
myDB.updatePupil(txID.getText().toString(),edZuname.getText().toString(),edStart.getText().toString(),
edVorname.getText().toString(), edGeschlecht.getText().toString());
}
}
Mein Main Layout:
<LinearLayout 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="at.atn.android.MainActivity"
android:orientation="vertical" >

<Button
android:id="@+id/btAnzeigen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bt_anzeigen"
android:onClick="klick" />

<Button
android:id="@+id/btAnlegen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bt_anlegen"
android:onClick="klick" />

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

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tx_id" />

<EditText
android:id="@+id/edID"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:ems="10" >
<requestFocus />
</EditText>

<Button
android:id="@+id/btAendern"
android:layout_width="0px"
android:layout_height="wrap_content"
android:text="@string/bt_aendern"
android:layout_weight="0.4"
android:onClick="klick" />

<Button
android:id="@+id/btLoeschen"
android:layout_width="0px"
android:layout_height="wrap_content"
android:text="@string/bt_loeschen"
android:layout_weight="0.4"
android:onClick="klick" />

</LinearLayout>

<Button
android:id="@+id/btAnzahl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bt_anzahl"
android:onClick="klick" />

</LinearLayout>
Zum Schluss den Code für die Main Activity
public class MainActivity extends Activity {
private HTLDb myDB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myDB = new HTLDb(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public void klick(View v) {
Intent intent;
EditText edID;
switch(v.getId()) {
case R.id.btAnzeigen:
intent = new Intent(this,AnzeigenActivity.class);
startActivity(intent);
break;
case R.id.btAnlegen:
intent = new Intent(this,InsertActivity.class);
startActivity(intent);
break;
case R.id.btAendern:
edID = (EditText)findViewById(R.id.edID);
intent = new Intent(this,UpdateActivity.class);
intent.putExtra("id", edID.getText().toString());
startActivity(intent);
break;
case R.id.btLoeschen:
edID = (EditText)findViewById(R.id.edID);
myDB.deletePupil(Integer.parseInt(edID.getText().toString()));
break;
case R.id.btAnzahl:
int anz = myDB.getAnz();
Toast.makeText(this, "Anzahl: " + anz, Toast.LENGTH_LONG).show();
break;
}
}
}
I hoffe einer von auch kann mir helfen.
Danke im voraus.
Michi_Wolf123
 
Zurück
Oben Unten