gpt4 book ai didi

java - 将 sqLite DB 中的数据显示到不同的 TextView 中

转载 作者:行者123 更新时间:2023-12-01 11:53:44 25 4
gpt4 key购买 nike

现在我有一个工作的 sqLite 数据库,一个工作 ListView 上的工作数据库搜索,显示搜索到的数据。

但我不知道如何将数据库结果中的数据显示到不同的 TextView 中。

我有 3 个关于数据库的 Activity :

搜索 Activity -> ListView 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;
}

}

最佳答案

执行类似 get 的操作,获取 onItemClick 函数中的所有相应字段将信息放入 Bundle 中,如下所示 bundle = new Bundle();

bundle.pustString(key,value);

然后通过 Intent 与信息启动 Activity

Intent intent = new Intent();
intent.pustExtra(key,bundle);
startActivity(intent);

在详细信息 Activity 中,从 Intent 中获取 bundle ,如下所示

Intent intent = getIntent();
Bundle bundle = intent.getExtras();

迭代bundle以获取各个值,以便您可以通过调用在TextViews中设置它们

textView.setText(bundle.getString(key));

关于java - 将 sqLite DB 中的数据显示到不同的 TextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28593589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com