SQLite数据中的自定义对话框

问题描述:

我(想)我已经设法构建了在自定义对话框中显示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); 

} 

但我坚持现在应该在最后一行之后放置什么。

在这里的第一个答案...... Simple display of data from SQlite database或 这里Custom listviews, 3 Strings per item?

嗯......不坏,但它就像猎枪灭蚊

你不需要建立自己的适配器对于

只使用SimpleCursorAdapter

dbAdaptor = new DBAdaptor(this); 
//!!!!remeber scroresCursor MUST have "_id" column 
scoresCursor = dbAdaptor.getScores(); 
listView.setAdapter(new SimpleCursorAdapter(this, 
//this is itemrow.xml 
R.layout.itemrow, 
scoresCursor , 
//"from" this is array of columns 
new String[]{DBAdaptor.KEY_PLAYER1_SCORE, DBAdaptor.KEY_PLAYER2_SCORE, DBAdaptor.KEY_PLAYER3_SCORE, DBAdaptor.KEY_PLAYER4_SCORE}, 
//"to" next goes ids in itemrow.xml, values from "from" columns are mapped to "to" Views 
new int[] {R.id.tvwPlayer1score, R.id.tvwPlayer2score, R.id.tvwPlayer3score, R.id.tvwPlayer4score })); 

下一步:你是否能够在活动(不是对话框)的列表视图中显示这些数据?

编辑:

Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.custom_dialogus); 
    ListView lv = (ListView)dialog.getWindow().findViewById(R.id.listView1); 
      Cursor c = managedQuery(
      Uri.parse("content://selvin.ListSyncSample/List"), 
      new String[] { "_id", "Name", 
       "Description", "CreatedDate" }, null, 
      null, "Name"); 
    lv.setAdapter(new SimpleCursorAdapter(this, R.layout.listrow, cursor, new String[] { "Name", 
      "Description", "CreatedDate" }, new int[] { 
      R.id.tName, R.id.tDescription, R.id.tCreatedDate })); 
    dialog.setTitle("Custom Dialog"); 
    dialog.show(); 

itemrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:padding="6dip"> 
    <ImageView android:id="@+id/icon" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_marginRight="6dip" 
     android:src="@drawable/icon" android:layout_centerVertical="true" /> 
    <TextView android:id="@+id/tName" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_toRightOf="@id/icon" 
     android:layout_alignParentRight="true" android:layout_alignParentTop="true" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:singleLine="true" android:ellipsize="marquee" /> 
    <TextView android:id="@+id/tDescription" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_toRightOf="@id/icon" 
     android:layout_alignParentRight="true" android:layout_below="@id/tName" /> 
    <TextView android:id="@+id/tCreatedDate" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_toRightOf="@id/icon" 
     android:layout_alignParentRight="true" android:layout_below="@id/tDescription" 
     android:singleLine="true" android:ellipsize="marquee" /> 
</RelativeLayout> 

custom_dialogus.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" 
    android:orientation="vertical"> 
    <ListView android:id="@+id/listView1" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 
</LinearLayout> 

代替光标C = managedQuery ...用你的光标

+0

我”而不是开一个新的活动。这是一个在每手牌后显示的比分画面,我宁愿将它显示为对话框,以便我们可以看到背后的比赛。 – theblitz 2011-04-13 15:37:35

+0

@theblitz请参阅编辑 – Selvin 2011-04-13 15:57:33

你的活动在哪里?该活动是将要显示的“屏幕”。