将来自 sqlite 数据库的数据显示到不同的 TextViews
Display Data from sqLite DB into different TextViews
现在我有一个工作的 sqlite 数据库,一个在工作的 ListView 上的工作数据库搜索,显示了搜索到的数据。
但我不知道如何将数据库结果中的数据显示到不同的 TextView 中。
我有 3 个数据库活动:
搜索Activity -> 列表视图Activity -> 详细信息Activity
如何将 ListView 中的选定项目放入新的 activity 并使用数据库中的数据填充 TextView?
希望你能明白我的意思。用英语写作不是我最好的技能 ;)
代码在这里:
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;
}
}
在onItemClick 函数中执行get 之类的操作获取所有相应的字段
像这样把信息放到 Bundle 中
Bundle bundle = new Bundle();
bundle.pustString(key,value);
然后使用信息
启动activity through intent
Intent intent = new Intent();
intent.pustExtra(key,bundle);
startActivity(intent);
并且在细节中 activity 从像这样的意图中获取 Bundle
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
遍历 bundle 以获取各个值,以便您可以通过调用
在 TextViews 中设置它们
textView.setText(bundle.getString(key));
在您的 ListView
的 OnItemClickListener
中,您可以创建保存站 ID 的意图,然后开始您的新 activity。我假设 ShowStations
是 Details Activity
.
使用以下内容编辑 ShowStationList
中的 stationsListView
OnItemClickListener
:
stationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int stationID = stations.get(position).getId();
Intent intent = new Intent(ShowStations.this, ShowStationList.class);
intent.putExtra("station_id",stationID);
startActivity(intent);
}
});
在您的 ShowStations
class 中,您需要调用 getIntent()
以获取保存在 Intent 中的 ID,并使用该 ID 从数据库中检索详细信息。
public class ShowStations extends ActionBarActivity {
DatabaseSourceStations databaseSourceStations;
Station aStation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_stations2);
int stationID = getIntent().getIntExtra("station_id", 0);
databaseSourceStations = new DatabaseSourceStations(this);
try {
databaseSourceStations.open();
// retrieve the station details from the database here,
// I assume you have a method called 'getSingleStationDetails'
// that returns a single station with the specified ID
aStation = databaseSourceStations.getSingleStationDetails(stationID);
} catch (IOException e) {
e.printStackTrace();
}
databaseSourceStations.close();
//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);
if (aStation != null) {
// set the details of the text views here
}
}
...
如果有帮助请告诉我。
编辑
在您的 DatabaseSourceStations
class 中,getSingleStationDetails
方法应该如下所示:
public Station getSingleStationDetails(int stationID) {
// I assume that your station's table name is DatabaseHelperStations.TABLE_NAME
// and that it has a column named DatabaseHelperStations.COLUMN_ID
String selectQuery = "SELECT * FROM " + DatabaseHelperStations.TABLE_NAME + " WHERE " + DatabaseHelperStations.COLUMN_ID + " = " + stationID + ";";
Cursor cursor = database.rawQuery(selectQuery, null);
Station aStation = null;
if (cursor.moveToFirst()) {
aStation = cursorToStation(cursor);
}
cursor.close();
return aStation;
}
现在我有一个工作的 sqlite 数据库,一个在工作的 ListView 上的工作数据库搜索,显示了搜索到的数据。
但我不知道如何将数据库结果中的数据显示到不同的 TextView 中。
我有 3 个数据库活动:
搜索Activity -> 列表视图Activity -> 详细信息Activity
如何将 ListView 中的选定项目放入新的 activity 并使用数据库中的数据填充 TextView?
希望你能明白我的意思。用英语写作不是我最好的技能 ;)
代码在这里:
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;
}
}
在onItemClick 函数中执行get 之类的操作获取所有相应的字段 像这样把信息放到 Bundle 中 Bundle bundle = new Bundle();
bundle.pustString(key,value);
然后使用信息
启动activity through intentIntent intent = new Intent();
intent.pustExtra(key,bundle);
startActivity(intent);
并且在细节中 activity 从像这样的意图中获取 Bundle
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
遍历 bundle 以获取各个值,以便您可以通过调用
在 TextViews 中设置它们textView.setText(bundle.getString(key));
在您的 ListView
的 OnItemClickListener
中,您可以创建保存站 ID 的意图,然后开始您的新 activity。我假设 ShowStations
是 Details Activity
.
使用以下内容编辑 ShowStationList
中的 stationsListView
OnItemClickListener
:
stationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int stationID = stations.get(position).getId();
Intent intent = new Intent(ShowStations.this, ShowStationList.class);
intent.putExtra("station_id",stationID);
startActivity(intent);
}
});
在您的 ShowStations
class 中,您需要调用 getIntent()
以获取保存在 Intent 中的 ID,并使用该 ID 从数据库中检索详细信息。
public class ShowStations extends ActionBarActivity {
DatabaseSourceStations databaseSourceStations;
Station aStation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_stations2);
int stationID = getIntent().getIntExtra("station_id", 0);
databaseSourceStations = new DatabaseSourceStations(this);
try {
databaseSourceStations.open();
// retrieve the station details from the database here,
// I assume you have a method called 'getSingleStationDetails'
// that returns a single station with the specified ID
aStation = databaseSourceStations.getSingleStationDetails(stationID);
} catch (IOException e) {
e.printStackTrace();
}
databaseSourceStations.close();
//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);
if (aStation != null) {
// set the details of the text views here
}
}
...
如果有帮助请告诉我。
编辑
在您的 DatabaseSourceStations
class 中,getSingleStationDetails
方法应该如下所示:
public Station getSingleStationDetails(int stationID) {
// I assume that your station's table name is DatabaseHelperStations.TABLE_NAME
// and that it has a column named DatabaseHelperStations.COLUMN_ID
String selectQuery = "SELECT * FROM " + DatabaseHelperStations.TABLE_NAME + " WHERE " + DatabaseHelperStations.COLUMN_ID + " = " + stationID + ";";
Cursor cursor = database.rawQuery(selectQuery, null);
Station aStation = null;
if (cursor.moveToFirst()) {
aStation = cursorToStation(cursor);
}
cursor.close();
return aStation;
}