2013-04-20 151 views
2

我試圖從列表視圖中獲取字符串值並將其放入另一個視圖。我已經成功地爲我的第一個listview做到了這一點,但它似乎不適用於我的第三個。 (我設法讓它工作,但它停止工作,因爲我不能理解的原因)我使用完全相同的方法,我試圖檢索的信息正在檢索,因爲我已經確認,而運行問題是它只是不變了。.setText方法不起作用

代碼

public void onCreate (Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.statseditor); 
    dbHelper = new PlayerStatsDatabase(this); 
     dbHelper.open(); 
    Bundle extras = getIntent().getExtras(); 
    if(extras !=null){ 
     //String value = extras.getString("Name"); 
    } 
     //Clean all data 
    // dbHelper.deleteAllStats(); 
     //Add some data 
     //dbHelper.insertSomeCountries(); 

     //Generate ListView from SQLite Database 
     //displayListView(); 
    // list = getListView(); 
     PlayerNameText = extras.getString("Name"); 
    btnSave2 = (Button) findViewById(R.id.btnSaveStats); 
    btnClear = (Button) findViewById(R.id.btnClearStats); 
    txtGoalsScored = (EditText) findViewById(R.id.txtGoal); 
    txtMinutesPlayed = (EditText) findViewById(R.id.txtMinPlayed); 
    txtSubstituteIn = (EditText) findViewById(R.id.txtSubIn); 
    txtSubstituteOut = (EditText) findViewById(R.id.txtSubOut); 
    radioSubIn = (RadioButton) findViewById (R.id.rdoSubIn); 
    radioSubOut = (RadioButton) findViewById (R.id.rdoSubOut); 
    playerRating = (RatingBar) findViewById (R.id.ratingBar1); 
    //playerTitle = (TextView) findViewById (R.id.textTitle); 
    playerName = (TextView) findViewById (R.id.textView1); 
    playerName.setText(PlayerNameText); 
    //playerTitle.setText (PlayerNameText); 
    checkBox = (CheckBox) findViewById (R.id.checkBox1); 
    if (checkBox.isChecked()){ 
     checkBox.append("Booked"); 
    } 
    final CharSequence checkText = checkBox.getText(); 
    //final Context context = 
btnSave2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      PlayerStatsDatabase db = new PlayerStatsDatabase(getApplicationContext()); 
      db.open(); 
      db.createStats(txtGoalsScored.getText().toString(), txtMinutesPlayed.getText().toString(),txtSubstituteIn.getText().toString(),txtSubstituteOut.getText().toString(), checkText.toString()); 
      db.close(); 
      dipsplayPlayerName(); 
      displayListView(); 

      //Intent intent = new Intent(context, ListItemsActivity.class); 
       // startActivity(intent); 
     } 
    }); 
} 

在我用這個方法playerName實際上得到改變PlayerNameText 但進一步下跌,當我再次嘗試使用這個一審它不工作

private void dipsplayPlayerName() { 
    setContentView (R.layout.statslist); 
    Bundle extras = getIntent().getExtras(); 
    PlayerNameText = extras.getString("Name"); 
    playerTitle = (TextView) findViewById (R.id.textTitle); 
    playerTitle.setText (PlayerNameText); 

} 

任何想法?沒有LogCat作爲應用程序不會崩潰

+0

你在哪裏首先實例化'意圖'發送?你確認「名稱」在那裏有價值嗎? – codeMagic 2013-04-20 19:48:13

+0

是的,因爲我說過,該方法的第一個實例按預期工作。而如果我檢查「名稱」,它運行時,它有我正在尋找的價值。 – b0w3rb0w3r 2013-04-20 19:54:07

+0

您是否曾經通過創建Activity的新實例或調用invalidate()來重新創建'view'?它有點混淆哪部分是和不在這裏工作 – codeMagic 2013-04-20 19:54:56

回答

2

將您收到的文本數據放入一個字段,並使用它而不是從意圖再次獲取它。

另外,檢查displayListView()不會更改textView。

+0

displayView並沒有改變TextView,因爲它有不同的佈局。關於你所建議的不是我用'PlayerNameText = extras.getString(「Name」);''做了什麼?如果不是這樣,我怎麼會把它放在一個字段中,因爲如果不先調用這個包,我不能使用它。 – b0w3rb0w3r 2013-04-21 12:07:30

+0

@ user2268970我不明白。一個字段是一個屬於一個類的變量。只需爲要保存和使用的文本創建一個,並使用它來代替一次又一次地訪問該意圖。只設置一次,然後在下次使用它。 – 2013-04-21 14:16:01