0

我有一个 Android 应用程序,我在其中将数据从一个活动发送到另一个活动。我可以轻松完成这部分,但问题是我还想显示一些额外的 TextView。因此,当我使用显示发送的数据时,setContentView我只能看到发送的数据。我看不到额外的 TextViews。我希望它们同时显示。

我搜索并努力寻找没有任何运气的解决方案。我附上代码以供参考。谢谢!

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop_information);
    Intent intent = getIntent();
    String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
    TextView textViewci= new TextView(this);
    textViewci.setText(message);
    TextView textView1= new TextView(this);
    TextView textView2= new TextView(this);
    textView1.setText("Major Districts");
    textView2.setText("Suitable Soil");

当我使用此代码时,我什么都看不到,活动保持空白。实际上我在TableLayout这里使用的是,以便我可以以表格的形式显示数据

4

5 回答 5

2

您必须在布局上添加文本视图。

View linearLayout =  findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);


TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));

((LinearLayout) linearLayout).addView(valueTV);
于 2013-02-04T04:52:52.470 回答
2

您必须在父视图中添加文本视图。例如,如果您在线性布局中使用了文本视图,则在线性布局中添加文本视图,然后使用 addview(linearlayout)。

下面是例子

RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(23);

TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());

layout.addView(tv1);       
layout.addView(tv2, lp);
于 2013-02-04T04:57:23.743 回答
1

您收到任何错误消息吗?如果是,请在此处发布。其他的都是对的。你需要将你的视图添加到父布局中。

于 2013-02-04T05:54:01.253 回答
1

我自己找到了答案。这很容易,这里不需要在父布局中使用视图。我修改了我的 xml 文件中的 textViews 并使用它们获取了它们的值findViewById。我正在附加更新的代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop_information);
    Intent intent = getIntent();
    String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
    textView=(TextView)findViewById(R.id.textViewci);
    textView1=(TextView)findViewById(R.id.textViewmd);
    textView2=(TextView)findViewById(R.id.textViewso);
    textView.setText(message);
    textView1.setText("Major Districts");
    textView2.setText("Suitable Soil");
    tf = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
    textView.setTypeface(tf);
    }
于 2013-02-05T10:07:53.797 回答
1
I have just make a test program for you. first class code

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent intent = new Intent(MainActivity.this, SecondClass.class);
        intent.putExtra("firstValue", "1");
        intent.putExtra("secondValue", "2");
        startActivity(intent);


        }
    });

Second class code :

setContentView(R.layout.activity_crop_information);
        Intent intent = getIntent();
        String message1 = intent.getStringExtra("firstValue");
        String message2 = intent.getStringExtra("secondValue");

        RelativeLayout layout = new RelativeLayout(this);
        TextView textView1= new TextView(this);
        TextView textView2= new TextView(this);

        TextView textViewci= new TextView(this);
        textViewci.setText(message1+""+message2);
        Log.e("intent value is", ""+message1+""+message2);


        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
        lp.addRule(RelativeLayout.RIGHT_OF, textView1.getId());


        textView1.setText("Major Districts");
        textView2.setText("Suitable Soil");

        layout.addView(textView1);
        layout.addView(textView2);
        addContentView(layout, lp);

Try and let me know. YOu have to make related layouts for both classes.
Hope this will help
于 2013-02-04T05:59:30.763 回答