2013-04-20 149 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