2015-03-02 42 views
-1

我想編輯循環內的TextViews。 TextViews可以在循環外定義,但它們必須在循環內編輯。我試圖把我的代碼放在while循環中,但是當我運行它時,應用程序崩潰了。 這裏是我的代碼:在循環中編輯TextViews

package com.example.ani.mapactivity; 

import android.graphics.Color; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.Random; 


public class LotA extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_lot); 
    LinearLayout toplayout = (LinearLayout) findViewById(R.id.toplayout); 
    Random findnum = new Random(); 
    int i = findnum.nextInt(1-0) + 0; 
    LinearLayout layout = (LinearLayout) toplayout.getChildAt(i); 
    Random spotinit = new Random(); 
    int a = spotinit.nextInt(9-0) + 0; 
    TextView spot = (TextView) layout.getChildAt(a); 
    spot.setBackgroundColor(Color.RED); 
} 


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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

這裏是我的XML代碼:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:id="@+id/layout1" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot1" 
     android:layout_marginTop="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot2" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot3" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot4" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot5" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:id="@+id/layout2" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot6" 
     android:layout_marginTop="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot7" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot8" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot9" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot10" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:id="@+id/layout3" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot11" 
     android:layout_marginTop="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot12" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot13" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot14" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
    <TextView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     android:id = "@+id/spot15" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#ffffff"/> 
</LinearLayout> 

下面是我一直在試圖把它放在一個while循環。我寧願一個while循環(因爲我最適合這個),但for循環也很好。

package com.example.ani.mapactivity; 

import android.graphics.Color; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.Random; 


public class LotA extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_lot); 
    LinearLayout toplayout = (LinearLayout) findViewById(R.id.toplayout); 
    int loopnum = 1; 
    while(loopnum <= 3){ 
     Random findnum = new Random(); 
     int i = findnum.nextInt(1-0) + 0; 
     LinearLayout layout = (LinearLayout) toplayout.getChildAt(i); 
     Random spotinit = new Random(); 
     int a = spotinit.nextInt(9-0) + 0; 
     TextView spot = (TextView) layout.getChildAt(a); 
     spot.setBackgroundColor(Color.RED); 
     loopnum += 1; 
    } 

} 


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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

如果我的代碼需要任何澄清,請在評論回覆。我對此很陌生,所以請儘量不要對我的代碼做出粗魯的評論。感謝您的幫助。

問候,

阿尼

+4

請發佈logcat輸出。 你已經寫了'LinearLayout toplayout =(LinearLayout)findViewById(R.id.toplayout);'。但是我沒有看到一個帶'toplayout'作爲'id'的'LinearLayout'。 – Kishore 2015-03-02 07:15:25

+0

首先檢查你的佈局和它的id,如果textview是在你的佈局然後找到它是在xml佈局定義。 – 2015-03-02 07:29:58

+0

請從您使用的IDE發佈您的LogCat – 2015-03-02 08:06:33

回答

0

考慮你有一個的LinearLayout這與ID topLayout您rootView

您randomNumber限制是錯誤的,下面的代碼both..Refer ..

Random findnum = new Random(); 
int i = findnum.nextInt(2); //Since you have 3 linear layouts 
LinearLayout layout = (LinearLayout) toplayout.getChildAt(i); 
Random spotinit = new Random(); 
int a = spotinit.nextInt(4) ;//Since each linear layout has 5 textViews 
TextView spot = (TextView) layout.getChildAt(a); 
spot.setBackgroundColor(Color.RED); 
loopnum += 1; 
+0

嗨,如果它已幫助,請將其標記爲已接受的答案 – 2015-05-27 10:12:19