来自 SQLite 数据的自定义对话框
我(认为)我已经成功构建了在自定义对话框中显示 SQLite 数据所需的大部分代码,但仍然不知道如何将它们组合在一起。
我的适配器看起来像这样:
public class ScoresDisplayerAdaptor extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public ScoresDisplayerAdaptor(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.playerscoresrow, parent, false);
return v;
}
/**
* @author will
*
* @param v
* The view in which the elements we set up here will be displayed.
*
* @param context
* The running context where this ListView adapter will be active.
*
* @param c
* The Cursor containing the query results we will display.
*/
@Override
public void bindView(View v, Context context, Cursor c) {
int player1Score = c.getInt(c.getColumnIndexOrThrow(DBAdaptor.KEY_PLAYER1_SCORE));
int player2Score = c.getInt(c.getColumnIndexOrThrow(DBAdaptor.KEY_PLAYER2_SCORE));
int player3Score = c.getInt(c.getColumnIndexOrThrow(DBAdaptor.KEY_PLAYER3_SCORE));
int player4Score = c.getInt(c.getColumnIndexOrThrow(DBAdaptor.KEY_PLAYER4_SCORE));
/**
* Next set the title of the entry.
*/
TextView tvwPlayer1Score = (TextView) v.findViewById(R.id.tvwPlayer1score);
TextView tvwPlayer2Score = (TextView) v.findViewById(R.id.tvwPlayer2score);
TextView tvwPlayer3Score = (TextView) v.findViewById(R.id.tvwPlayer3score);
TextView tvwPlayer4Score = (TextView) v.findViewById(R.id.tvwPlayer4score);
tvwPlayer1Score.setText(player1Score);
tvwPlayer2Score.setText(player2Score);
tvwPlayer3Score.setText(player3Score);
tvwPlayer4Score.setText(player4Score);
}
}
我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<Table>
<TableRow>
<TextView android:id="@+id/tvwPlayer1score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer2score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer3score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer4score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</Table>
</RelativeLayout>
最后,我使用它的代码是这样的:
private void showCurrentScores(){
DBAdaptor dbAdaptor;
Cursor scoresCursor;
ScoresDisplayerAdaptor scoresDisplayerAdaptor;
dbAdaptor = new DBAdaptor(this);
scoresCursor = dbAdaptor.getScores();
startManagingCursor(scoresCursor);
scoresDisplayerAdaptor = new ScoresDisplayerAdaptor(this,scoresCursor);
}
但我现在不知道在最后一行之后要放什么。
I (think) I have managed to build most of the code required to display SQLite data in a custom dialog but am still stuck on how to put it all together.
My Adaptor looks like this:
public class ScoresDisplayerAdaptor extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public ScoresDisplayerAdaptor(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.playerscoresrow, parent, false);
return v;
}
/**
* @author will
*
* @param v
* The view in which the elements we set up here will be displayed.
*
* @param context
* The running context where this ListView adapter will be active.
*
* @param c
* The Cursor containing the query results we will display.
*/
@Override
public void bindView(View v, Context context, Cursor c) {
int player1Score = c.getInt(c.getColumnIndexOrThrow(DBAdaptor.KEY_PLAYER1_SCORE));
int player2Score = c.getInt(c.getColumnIndexOrThrow(DBAdaptor.KEY_PLAYER2_SCORE));
int player3Score = c.getInt(c.getColumnIndexOrThrow(DBAdaptor.KEY_PLAYER3_SCORE));
int player4Score = c.getInt(c.getColumnIndexOrThrow(DBAdaptor.KEY_PLAYER4_SCORE));
/**
* Next set the title of the entry.
*/
TextView tvwPlayer1Score = (TextView) v.findViewById(R.id.tvwPlayer1score);
TextView tvwPlayer2Score = (TextView) v.findViewById(R.id.tvwPlayer2score);
TextView tvwPlayer3Score = (TextView) v.findViewById(R.id.tvwPlayer3score);
TextView tvwPlayer4Score = (TextView) v.findViewById(R.id.tvwPlayer4score);
tvwPlayer1Score.setText(player1Score);
tvwPlayer2Score.setText(player2Score);
tvwPlayer3Score.setText(player3Score);
tvwPlayer4Score.setText(player4Score);
}
}
My XMLs:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<Table>
<TableRow>
<TextView android:id="@+id/tvwPlayer1score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer2score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer3score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer4score" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</Table>
</RelativeLayout>
Finally, my code where I use it is like this:
private void showCurrentScores(){
DBAdaptor dbAdaptor;
Cursor scoresCursor;
ScoresDisplayerAdaptor scoresDisplayerAdaptor;
dbAdaptor = new DBAdaptor(this);
scoresCursor = dbAdaptor.getScores();
startManagingCursor(scoresCursor);
scoresDisplayerAdaptor = new ScoresDisplayerAdaptor(this,scoresCursor);
}
But I am stuck now what to put after that last line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先在这里回答...
简单显示来自 SQlite 数据库的数据 或
这里自定义列表视图,每个项目 3 个字符串?
嗯...还不错,但这就像用猎枪杀死蚊子一样,
您不需要为此构建自己的适配器,
只需使用 SimpleCursorAdapter
接下来:您能够在活动(而不是对话框)的列表视图中显示此数据吗?
编辑:
itemrow.xml
custom_dialogus.xml
而不是 Cursor c = ManagedQuery ...使用光标
first answer here ...
Simple display of data from SQlite database or
here Custom listviews, 3 Strings per item?
well ... were not bad, but it were like killing mosquito with shotgun
you don't need to build your own Adapter for that
just use SimpleCursorAdapter
next: are you able to display this data in listview in activity (not dialog) ?
EDIT:
itemrow.xml
custom_dialogus.xml
instead of Cursor c = managedQuery ... use your cursor
你的活动在哪里?活动是将要显示的“屏幕”。
Where's your activity? The activity is the "screen" that will be displayed.