通过数组分配动态生成的具有不同动作的按钮

时间:2014-02-25 20:27:35

标签: android

我希望创建的每个textView运行时在toast中显示不同的值,但它只显示数组中的最后一个值..

        response = (SoapPrimitive) envelope.getResponse();
        News = response.toString().split("`");

        LinearLayout Newslayout = (LinearLayout) findViewById(R.id.newslayout);
        while (i < News.length) {

            TextView title = new TextView(this);
            title.setText(News[i]);
            Newslayout.addView(title);
            TextView seeMore = new TextView(this);
            seeMore.setText("See More...");
            seeMore.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), News[i], Toast.LENGTH_LONG).show();

                }
            });
            Newslayout.addView(seeMore);
            TextView title1 = new TextView(this);
            title1.setText("");
            Newslayout.addView(title1);
            i++;

        }
    }

1 个答案:

答案 0 :(得分:0)

我认为这是因为seeMore显示News[i]的值,而i总是等于News.length - 1,所以你可以做类似的事情

seeMore.setId(i);

并将您的代码更改为

Toast.makeText(getApplicationContext(), News[v.getId()], Toast.LENGTH_LONG).show();

祝你好运