为什么我的GridView没有显示?

时间:2013-04-30 04:37:08

标签: android listview gridview

尝试使用以下方法将列表视图中的数据导入我的GridView。但是,当我运行应用程序时,我没有看到gridview,只看到statslist和更新按钮。

代码

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();
    }

为什么网格不显示为什么列表视图为空?

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>

1 个答案:

答案 0 :(得分:0)

代码无法成功编译,因为存在编译错误。

错误是

  1. 类声明(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);之后的语句是没有价值的,它是一个编译错误(在标记{之后预期的标识符)。
  2. 您无法在Java中声明或定义类外的方法。
  3. 调用方法displayGridView();的语句应该在方法中。
  4. 您的代码必须如下

    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();
        }
    }