mercredi 18 février 2015

Display Data from sqLite DB into difrent TextViews

Now i have a working sqLite Database, a working Database search an a working ListView that showing the data that was searched.


But i down't know how to display the Data from the result from the database into diferent TextViews.


I have 3 aktivitys for the database thing:


Search Activity -> ListView Activity -> Details Activity


How can i put the selectet item from the ListView into a new aktivity and fill the TextViews with the Data from the database?


I hope that you can undersand what i mean. Write in english issn't my best skill ;)


Here the Code:


ShowStations.java



package san.tal.tfapp.Bahnhoefe;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


import san.tal.tfapp.R;

public class ShowStations extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_stations2);


//TextViews werden eingebunden
TextView station_ckanal = (TextView) findViewById(R.id.tv_show_ckanal);
TextView station_fdl = (TextView) findViewById(R.id.tv_show_fdl);
TextView station_lastrecke = (TextView) findViewById(R.id.tv_show_laStrecke);
TextView station_linie = (TextView) findViewById(R.id.tv_show_linie);
TextView station_art = (TextView) findViewById(R.id.tv_art);

}




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


}


ShowStationList.java



package san.tal.tfapp.Bahnhoefe;

import android.content.Intent;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;

import san.tal.tfapp.Database.DatabaseSourceStations;
import san.tal.tfapp.Models.Station;
import san.tal.tfapp.R;
import san.tal.tfapp.showPhoneNumbers;

public class ShowStationList extends ActionBarActivity {

public static final String KEY_SEARCH_STRING = "key_search_string";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_station_list);

final ListView stationsListView = (ListView) findViewById(R.id.listView);
stationsListView.setEmptyView(findViewById(android.R.id.empty));

Intent intent = getIntent();
String searchString = intent.getStringExtra(KEY_SEARCH_STRING);
if (searchString != null) {

final List<Station> stations = queryList(searchString);
String[] namesArray = Station.getNamesArray(stations);
stationsListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, namesArray));

stationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
stations.get(position).getId();
}
});
} else {
Toast.makeText(this, "Kein Suchbegriff eingegeben", Toast.LENGTH_LONG).show();
}


}

private List<Station> queryList(String searchString) {

DatabaseSourceStations databaseSourceStations = new DatabaseSourceStations(this);
try {
databaseSourceStations.open();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Datenbank konnte nicht kopiert werden!", Toast.LENGTH_LONG);
}
List<Station> stationList = databaseSourceStations.searchStation(searchString);
databaseSourceStations.close();

return stationList;
}

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


DatabaseSourceStations.java



package san.tal.tfapp.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import san.tal.tfapp.Models.Station;

/**
* Created by Talsan on 17.02.2015.
*/
public class DatabaseSourceStations {

SQLiteDatabase database;
DatabaseHelperStations databaseHelperStations;

public DatabaseSourceStations(Context context) {
databaseHelperStations = DatabaseHelperStations.instance(context);
}

public void open() throws IOException {
database = databaseHelperStations.getWritableDatabase();
}

public void close() {
database.close();
}

public long addStation(Station station) {

ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelperStations.COLUMN_NAME, station.getName());
contentValues.put(DatabaseHelperStations.COLUMN_RIL100, station.getRil100());
contentValues.put(DatabaseHelperStations.COLUMN_CKANAL, station.getCkanal());
contentValues.put(DatabaseHelperStations.COLUMN_LASTRECKE, station.getLastrecke());
contentValues.put(DatabaseHelperStations.COLUMN_LINIE, station.getLinie());
contentValues.put(DatabaseHelperStations.COLUMN_FDL, station.getFdl());
contentValues.put(DatabaseHelperStations.COLUMN_ART, station.getArt());

return database.insert(DatabaseHelperStations.DB_NAME, null, contentValues);
}

public void addStations(List<Station> stationList) {

for (Station station : stationList) {
addStation(station);
}
}

public List<Station> getEverything() {
String query = "SELECT * FROM " + DatabaseHelperStations.DB_NAME + ";";

return cursorToStationList(database.rawQuery(query, null));
}

public List<Station> searchStation(String searchString) {

String optimizedQuery = "%" + searchString + "%";

String query = "SELECT * FROM " + DatabaseHelperStations.DB_NAME +
" WHERE " + DatabaseHelperStations.COLUMN_NAME + " LIKE '" + optimizedQuery + "' OR " + DatabaseHelperStations.COLUMN_RIL100 + " LIKE '" + optimizedQuery + "';";

return cursorToStationList(database.rawQuery(query, null));
}

public List<Station> cursorToStationList(Cursor cursor) {

if (cursor == null) {
return null;
}

cursor.moveToFirst();

List<Station> stations = new ArrayList<>();

while (!cursor.isAfterLast()) {

stations.add(cursorToStation(cursor));

cursor.moveToNext();
}

return stations;
}

public Station cursorToStation(Cursor cursor) {
Station station = new Station();
station.setId(cursor.getInt(0));
station.setName(cursor.getString(1));
station.setRil100(cursor.getString(2));
station.setCkanal(cursor.getString(3));
station.setLastrecke(cursor.getInt(4));
station.setLinie(cursor.getString(5));
station.setFdl(cursor.getString(6));
station.setArt(cursor.getString(7));
return station;
}

}

Aucun commentaire:

Enregistrer un commentaire