2013-04-30 208 views
0

嘗試使用以下方法從列表視圖中將數據導入到GridView中。但是,當我運行應用程序,我沒有看到gridview,只有statslist和更新按鈕。爲什麼我的GridView沒有顯示?

代碼

public class StatsListActivity extends Activity { 
(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
public String PlayerData; 
public String playerNumberStr; 
public String playerPositionStr; 
public String playerTeamStr; 
private PlayerStatsDatabase dbHelper; 
private SimpleCursorAdapter statsAdapter; 
Button updateButton = null; 
TextView playerTitle = null; 
TextView playerNumber = null; 
TextView playerPosition = null; 
TextView playerTeam = null; 

public void onCreate (Bundle savedInstanceState) { 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    super.onCreate(savedInstanceState); 
dbHelper = new PlayerStatsDatabase(this); 
     dbHelper.open(); 


    displayGridView(); 
} 

private void displayGridView() { 
     // TODO Auto-generated method stub 
     //playerTitle.setText (PlayerNameText); 
    Cursor cursor = dbHelper.fetchAllStats(); 
     setContentView(R.layout.scoreupdate); 
      // The desired columns to be bound 
      String[] columns = new String[] { 
      PlayerStatsDatabase.KEY_SCORE, 
      PlayerStatsDatabase.KEY_MINUTES, 
      PlayerStatsDatabase.KEY_SUBIN, 
      PlayerStatsDatabase.KEY_SUBOUT, 
      PlayerStatsDatabase.KEY_BOOKING, 
      }; 

      // the XML defined views which the data will be bound to 
      int[] to = new int[] { 
      R.id.pGoals, 
      R.id.pMinutes, 
      R.id.pSubIn, 
      R.id.pSubOut, 
      R.id.pBook, 
      }; 

      // create the adapter using the cursor pointing to the desired data 
      //as well as the layout information 
      statsAdapter = new SimpleCursorAdapter(
      this, R.layout.statslist, 
      cursor, 
      columns, 
      to 
      ); 

      GridView grid= (GridView) findViewById(R.id.gridViewPlayers); 
      // Assign adapter to ListView 
      grid.setAdapter(statsAdapter); 

      statsAdapter.notifyDataSetChanged(); 
      dbHelper.close(); 
    } 

爲什麼沒有出現在網格也是爲什麼是空的ListView?

XML

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id = "@+id/RHE" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0" 
    android:padding="5dp"> 

<Button 
    android:id="@+id/btnUpdt" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Update" /> 

<GridView 
    android:id="@+id/gridViewPlayers" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:numColumns="5" > 
</GridView> 

+0

u能烏拉圭回合後XML .... – user1969053 2013-04-30 04:44:16

+0

發佈的XML。 – b0w3rb0w3r 2013-04-30 04:48:19

+0

這裏發佈的代碼將無法成功編譯。 – 2013-04-30 04:48:21

回答

0

代碼將無法編譯成功,因爲有編譯錯誤。

的錯誤是

  1. 聲明類聲明(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);後一分錢也不要,這是一個編譯錯誤(標識預期令牌{後)。
  2. 您不能在Java中的類外聲明或定義方法。
  3. 調用方法displayGridView();的語句應位於方法內。

您的代碼必須是如下

public class StatsListActivity extends Activity 
{ 
    public String PlayerData; 
    public String playerNumberStr; 
    public String playerPositionStr; 
    public String playerTeamStr; 
    private PlayerStatsDatabase dbHelper; 
    private SimpleCursorAdapter statsAdapter; 
    Button updateButton = null; 
    TextView playerTitle = null; 
    TextView playerNumber = null; 
    TextView playerPosition = null; 
    TextView playerTeam = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     dbHelper = new PlayerStatsDatabase(this); 
     dbHelper.open(); 
     displayGridView(); 
    } 

    private void displayGridView() 
    { 
     // TODO Auto-generated method stub 
     //playerTitle.setText (PlayerNameText); 
     Cursor cursor = dbHelper.fetchAllStats(); 
     setContentView(R.layout.scoreupdate); 
     // The desired columns to be bound 
     String[] columns = new String[] { PlayerStatsDatabase.KEY_SCORE, PlayerStatsDatabase.KEY_MINUTES, PlayerStatsDatabase.KEY_SUBIN, PlayerStatsDatabase.KEY_SUBOUT, PlayerStatsDatabase.KEY_BOOKING, }; 
     // the XML defined views which the data will be bound to 
     int[] to = new int[] { R.id.pGoals, R.id.pMinutes, R.id.pSubIn, R.id.pSubOut, R.id.pBook, }; 
     // create the adapter using the cursor pointing to the desired data 
     //as well as the layout information 
     statsAdapter = new SimpleCursorAdapter(this, R.layout.statslist, cursor, columns, to); 
     GridView grid = (GridView) findViewById(R.id.gridViewPlayers); 
     // Assign adapter to ListView 
     grid.setAdapter(statsAdapter); 
     statsAdapter.notifyDataSetChanged(); 
     dbHelper.close(); 
    } 
} 
+0

對不起,原帖中的額外括號不應該在那裏。 'displayGridView'方法在'onCreate'類中,我的gridview仍然沒有顯示。 – b0w3rb0w3r 2013-04-30 09:44:08

+0

除了你的代碼應該可以正常工作。它適用於我的靜態值。你的數據庫中有價值嗎? – 2013-04-30 10:37:49

+0

是的我在我的數據庫中有值,但它不會顯示。 – b0w3rb0w3r 2013-04-30 11:06:55