0

尝试使用以下方法将列表视图中的数据获取到我的 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>

4

1 回答 1

0

由于存在编译错误,代码将无法成功编译。

错误是

  1. 类声明之后的语句(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);一文不值,这是一个编译错误(在 token 之后需要标识符{)。
  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();
    }
}
于 2013-04-30T04:56:55.953 回答