我试图从我的SQLite数据库中获取一些数据到我的(ptr + i)->roll
。现在,当应用程序启动时,我的数据库中的数据会加载到ListView
,所以我相信我正在走上正轨。我无法将数据放入Toast
。我已经查看了一些教程,但我仍然不确定。这是我的ListView
课程:
OverviewActivity.java
这是我的public class OverviewActivity extends ListActivity implements View.OnClickListener {
private DBAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview_activity);
dbAdapter = new DBAdapter(this);
dbAdapter.open();
Cursor c = dbAdapter.getAllValues();
if (c.moveToFirst()) {
do {
DisplayTitle(c);
} while (c.moveToNext());
}
}
public void DisplayTitle(Cursor c) {
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Number: " + c.getString(1) + "\n" +
"Name: " + c.getString(2) + "\n" +
"Version: " + c.getString(3) + "\n" +
"Method: " + c.getString(4) + "\n" +
"Chain Number: " + c.getString(5) + "\n" +
"Caught: " + c.getString(6) + "\n",
Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View view) {
//TODO: AUTOGENERATED METHOD
}
@Override
public void onDestroy(){
super.onDestroy();
dbAdapter.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
Intent intent = new Intent(OverviewActivity.this, VersionMethodActivity.class);
startActivity(intent);
break;
}
return false;
}
}
课程:
DBAdapter.java
有关如何实现这一目标的任何建议吗?
答案 0 :(得分:1)
假设您对数据库中的每个列使用TextView
,则可以在加载ListView
的活动中使用以下代码:
DBAdapter db = new DBAdapter(this);
Cursor cursor = db.getAllValues();
// The columns that you want to bind
String[] columns = new String[] {
db.KEY_NUMBER,
db.KEY_NAME,
db.KEY_VERSION,
db.KEY_METHOD,
db.KEY_CHAIN_NUMBER,
db.KEY_CAUGHT
};
// The IDs of the TextViews you want to bind the columns with
int[] to = new int[] {
R.id.number,
R.id.name,
R.id.version,
R.id.method,
R.id.chain_number,
R.id.caught
};
// Attach the layout file and the columns to the ListView
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.your_listview_layout,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.your_listview);
// Assign the adapter to the ListView
listView.setAdapter(dataAdapter);
确保您拥有ListView
的布局文件。您可能需要更改此代码以满足您的需求。如果您有任何疑问,请在下面评论。