如何在android中将数组元素设置为动态创建的Textview

时间:2013-12-26 05:59:39

标签: android

如何在android

中将数组元素设置为动态创建的textview

MainActivity1:

public class MainActivity1 extends Activity implements OnClickListener {
    EditText editText;
    Button button;
    public String Message;
    public String[] mobileArray;
    int len;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        editText = (EditText) findViewById(R.id.editText1);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);

        mobileArray = editText.getText().toString().split(",");
        len = mobileArray.length;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        LinearLayout rl = (LinearLayout) findViewById(R.id.linear1);

        TextView[] tv = new TextView[len];

        for (int i = 0; i <=len; i++) {
            tv[i] = new TextView(getBaseContext());
            tv[i].setText("" +mobileArray[i] );
            tv[i].setPadding(50, 50, 0, 0);
            tv[i].setTextColor(Color.parseColor("#FF000000"));
            rl.addView(tv[i]);

        }
    }

}

如何在android

中将数组元素设置为动态创建的textview

这里everthing工作正常,它创建了dyanmic textview。但我需要在textview中显示数组元素。

当我尝试设置数组元素时,app停止工作。 请帮帮我。

这是我的编辑代码

我的日志猫是

      12-26 02:13:44.897: E/AndroidRuntime(2779): FATAL EXCEPTION: main
12-26 02:13:44.897: E/AndroidRuntime(2779): Process: com.example.new_word, PID: 2779
12-26 02:13:44.897: E/AndroidRuntime(2779): java.lang.NullPointerException
12-26 02:13:44.897: E/AndroidRuntime(2779):     at com.example.new_word.MainActivity1.onClick(MainActivity1.java:56)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.view.View.performClick(View.java:4438)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.view.View$PerformClick.run(View.java:18422)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.os.Handler.handleCallback(Handler.java:733)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.os.Looper.loop(Looper.java:136)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at java.lang.reflect.Method.invoke(Method.java:515)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-26 02:13:44.897: E/AndroidRuntime(2779):     at dalvik.system.NativeStart.main(Native Method)
12-26 02:13:47.467: I/Process(2779): Sending signal. PID: 2779 SIG: 9

Xml代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:id="@+id/linear1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="Button" />

</LinearLayout>
</ScrollView>

6 个答案:

答案 0 :(得分:1)

尝试以下内容:

 if(!(editText.getText().toString().equals(""))){

      TextView[] tv = new TextView[len];
      for (int i = 0; i <len; i++) {
        tv[i] = new TextView(getBaseContext());
        tv[i].setText(mobileArray[i]);
        tv[i].setPadding(50, 50, 0, 0);
        tv[i].setTextColor(Color.parseColor("#000000"));
        rl.addView(tv[i]);
        }

  }

答案 1 :(得分:1)

试试这个......

TextView[] tv = new TextView[len];
for (int i = 0; i < len; i++) {
    tv[i] = new TextView(this);
    tv[i].setText(mobileArray[i]);
    tv[i].setPadding(50, 50, 0, 0);
    tv[i].setTextColor(Color.parseColor("#FF000000")); 
    // tv[i].setTextColor(Color.BLACK);
    rl.addView(tv[i]);
}

这里你使用了颜色#000000,这意味着透明的颜色。因此您无法看到文本

答案 2 :(得分:0)

我可以指出的第一个错误是你的for循环条件:[也改变它]

for (int i = 0; i <len; i++)

如果错误仍然存​​在,请发布您的logcat;

同时更改此代码以显示该数组的文本:

tv[i] = new TextView(getBaseContext());
        tv[i].setText(mobileArray[i]);

答案 3 :(得分:0)

试试这个,

TextView[] tv = new TextView[len];
        for (int i = 0; i <len; i++) 
        {
            tv[i] = new TextView(MainActivity1.this);
            tv[i].setText(mobileArray[i]);
            tv[i].setPadding(50, 50, 0, 0);
            tv[i].setTextColor(Color.parseColor("#FF000000"));
            rl.addView(tv[i]);

        }

答案 4 :(得分:0)

试试这个示例代码 -

final int N = 10; // total number of textviews to add
final TextView[] tv = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
    // create a new textview
    final TextView rowTextView = new TextView(this);
    // set some properties of rowTextView or something
    rowTextView.setText("This is row #" + i);
    // add the textview to the linearlayout
    rl.addView(rowTextView);
    // save a reference to the textview for later
    tv[i] = rowTextView;
}

答案 5 :(得分:0)

使用此代码可以帮助您

    int []a=new int[4];
    a[0]=1;
    a[1]=2;
    a[2]=3;
    a[3]=4;
    for(int i=1;i<5;i++)
    {
        str4=str4+a.toString().valueOf(i)+"\n";
    }
RelativeLayout rl=(RelativeLayout) findViewById(R.id."**Your layout ID**");
        TextView textDynamic = new TextView(this);
        textDynamic.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        textDynamic.setText(str4);
        textDynamic.setBackgroundColor(Color.GREEN);
        textDynamic.setTextColor(Color.RED);
        textDynamic.setTextScaleX(2);
        textDynamic.setTextSize(20);
        textDynamic.isClickable();
        textDynamic.setPadding(10, 10, 10, 10);
        rl.addView(textDynamic);