操作栏上的标题显示垃圾值

时间:2015-12-19 13:34:47

标签: android android-layout android-intent android-actionbar

我尝试使用意图在下一个活动屏幕上显示操作栏上Listview项目的名称作为标题。

这是我的代码。

MathematicsDBEntry.java

public class MathematicsDBEntry extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Intent i = getIntent();
        String newActionBarTitle = i.getStringExtra("Position");

        super.onCreate(savedInstanceState);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle(newActionBarTitle);
        setContentView(R.layout.activity_mathematics_dbentry);


    }

}

MathsActivity.java

public class MathsActivity extends ListActivity implements AdapterView.OnItemClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maths);

        // storing string resources into Array
        String[] math_data = getResources().getStringArray(R.array.maths_list_data);

        ListView listView = getListView();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, math_data);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView textView = (TextView) view;
        //Toast.makeText(this, textView.getText().toString() + " " + position, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(getApplicationContext(), MathematicsDBEntry.class);
        intent.putExtra("Position", textView.toString());
        startActivity(intent);
    }
}

但我得到的只是下面显示的垃圾值。

http://imgur.com/Bbljmlx

1 个答案:

答案 0 :(得分:1)

您需要使用TextView的{​​{3}}方法从中获取数据。

您需要更改:

intent.putExtra("Position", textView.toString());

为:

intent.putExtra("Position", textView.getText().toString());