如何将选定的列表视图项目转移到另一个列表视图

时间:2013-04-18 23:19:53

标签: android listviewitem

到目前为止,我已经成功地从ListView中选择了我想要的数据并使用以下代码打开下一个活动。

    list.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> listView, View view, 
             int position, long id) {
           // Get the cursor, positioned to the corresponding row in the result set
           Cursor cursor = (Cursor) listView.getItemAtPosition(position);



           String playerName = 
            cursor.getString(cursor.getColumnIndexOrThrow("Player_Name"));
           Toast.makeText(getApplicationContext(),
             playerName, Toast.LENGTH_SHORT).show();
           Intent in = new Intent(context, PlayerStatsActivity.class);
           in.putExtra ("Name", playerName);
           startActivity(in);
         finish();
           }
      });

PlayerStatsActivity Class

 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();
     String 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);
    playerName = (TextView) findViewById (R.id.textView1);
    playerName.setText(PlayerNameText);

我希望从上一个活动中选择的playerName字符串显示在playerName TextView中。上面是我尝试但PlayerNameText返回空值。

我已经

     String value = extras.getString("name");
     }

哪个应该得到我的playerName字符串我只需要将它调用到我的playerName textfeild我想但是无法弄清楚如何。

1 个答案:

答案 0 :(得分:1)

在您的第一个活动中,您正在设置

  

in.putExtra(“名称”,playerName);

在您尝试访问的其他活动中

  

String value = extras.getString(“name”);

在一个名称中以大写N开头,在其他名称中以相同的名称开头。使它们都一样。

编辑II:

在in.putExtra(“名称”,playerName)之前尝试此操作;看你是否在挽救价值。然后在日志中看到你说的是播放器名称,而不仅仅是null。

Log.d(“TAG”,“Value:”+ playerName);