现在我有一个工作的sqLite数据库,一个工作的数据库搜索工作ListView,显示搜索到的数据。
但我不知道如何将数据从数据库中显示到不同的TextView中。
我有3个数据库事件活动:
搜索活动 - > ListView活动 - >详情活动
如何将ListView中的选定项目放入新活动,并使用数据库中的数据填充TextViews?
我希望你能明白我的意思。用英语写不是我最好的技能;)
这里的代码:
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;
}
}
答案 0 :(得分:1)
执行类似获取onItemClick函数中所有相应字段的操作 将信息放入Bundle这样的东西 Bundle bundle = new Bundle();
bundle.pustString(key,value);
然后通过意图
开始活动Intent intent = new Intent();
intent.pustExtra(key,bundle);
startActivity(intent);
在细节活动中,从这样的意图中获取Bundle
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
通过bundle迭代获取各个值,以便您可以通过调用
在TextViews中设置它们textView.setText(bundle.getString(key));
答案 1 :(得分:0)
在OnItemClickListener
的{{1}}中,您可以创建保存电台ID的意图,然后开始新的活动。我假设ListView
是ShowStations
。
使用以下内容编辑Details Activity
中的stationsListView
OnItemClickListener
:
ShowStationList
在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
来获取意图中保存的ID,并使用ID从数据库中检索详细信息。
getIntent()
如果有帮助,请告诉我。
修改强>
在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
方法应如下所示:
getSingleStationDetails