如何根据从微调器中选择的整数创建动态文本视图?
How can I create dynamic text views according to a selected integer from a spinner?
03-05 11:31:07.042 1897-1897/com.example.project.a D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
03-05 11:31:07.050 1897-1897/com.example.project.a E/AndroidRuntime﹕ FATAL
EXCEPTION: main
Process: com.example.project.a, PID: 1897
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project.a/com.example.project.a.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.removeAllViews()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
// Toast.makeText(getApplicationContext(),"The day qqq"+arg0.getItemAtPosition(arg2).toString(),Toast.LENGTH_LONG).show();
String control2=arg0.getItemAtPosition(arg2).toString();
int noOfBulb= (int) spinner1.getSelectedItem();
final TextView tryText2=(TextView)findViewById(R.id.textView);
tryText2.setText("No"+noOfBulb);
mdynamiclayout = (LinearLayout) findViewById(R.id.dynamiclayout);
mdynamiclayout.removeAllViews();//need to remove the view before it create
for (int i = 0; i < noOfBulb; i++) {//6 is your selected item from spinner it can be any nymber
TextView t = new TextView(MainActivity.this);
t.setText("value" + i);
// t.setTextSize(20);
// t.setTextColor(Color.BLUE);
mdynamiclayout.addView(t);
}
}
public void onNothingSelected(AdapterView<?> arg0){}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SelectBulbs();
rl = (RelativeLayout) findViewById(R.id.rl);
tv1 = new TextView (MainActivity.this);
tv1.setText("Dynamic TextView");
tv1.setTextColor(Color.RED);
tv1.setTextSize(20);
RelativeLayout.LayoutParams params1=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params1.leftMargin=115;
params1.topMargin=120;
rl.addView(tv1);
public class Unit {
private String _bulbNo;
public Unit(String bulbNo) {
_bulbNo = bulbNo;
}
public String getBulbNo(){
return _bulbNo;
}
}
// I created this class
private void addBulbsFans(String bulb)
{
Units.add(new Unit(bulb));
}
//我将此方法添加到 MainActivity.java。在此之后我需要知道如何从按钮单击事件传递微调器值。
您只需按照以下步骤将选定的字符串转换为整数
int value = Integer.valueOf(spiner.getselecteditem);
你使用Integer.valueOf()
,你的情况是这样的:
String[] bulbs = {"0","1","2","3","4","5"};
Integer[] bulbsConvert = new Integer[6];
//Array to int
for(String item : bulbs){
bulbsConvert.add(Integer.valueOf(item));
}
如果您想按垂直顺序创建动态文本视图,请在 xml 中创建 LinearLayout,并将 Orientation 设置为 Vertical,如下所示
<LinearLayout
android:id="@+id/dynamiclayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/registerlayout"
android:layout_below="@+id/registerlayout"
android:layout_marginLeft="16dp"
android:layout_marginTop="34dp"
android:orientation="vertical" >
</LinearLayout>
然后在您的 activity
中添加以下代码行
private LinearLayout mdynamiclayout;
mdynamiclayout = (LinearLayout) findViewById(R.id.dynamiclayout);
mdynamiclayout.removeAllViews();//need to remove the view before it create
for (int i = 0; i < 6; i++) {//6 is your selected item from spinner it can be any nymber
TextView t = new TextView(MainActivity.this);
t.setText("value" + i);
mdynamiclayout.addView(t);
}
将一个 activity 传递给另一个 activity :
Intent intent_value = new Intent(Mainactivity.this,
yoursecondactivity.class);
intent_value.putExtra("key", value);
intent_value.putExtra("key2", value2);
startActivity(intent_value);
检索值:
Intent getValues = getIntent();
String value1 = getValues.getStringExtra("key");
如果您需要任何帮助,请告诉我
03-05 11:31:07.042 1897-1897/com.example.project.a D/AndroidRuntime﹕ Shutting down VM --------- beginning of crash 03-05 11:31:07.050 1897-1897/com.example.project.a E/AndroidRuntime﹕ FATAL
EXCEPTION: main Process: com.example.project.a, PID: 1897 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project.a/com.example.project.a.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.removeAllViews()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
// Toast.makeText(getApplicationContext(),"The day qqq"+arg0.getItemAtPosition(arg2).toString(),Toast.LENGTH_LONG).show();
String control2=arg0.getItemAtPosition(arg2).toString();
int noOfBulb= (int) spinner1.getSelectedItem();
final TextView tryText2=(TextView)findViewById(R.id.textView);
tryText2.setText("No"+noOfBulb);
mdynamiclayout = (LinearLayout) findViewById(R.id.dynamiclayout);
mdynamiclayout.removeAllViews();//need to remove the view before it create
for (int i = 0; i < noOfBulb; i++) {//6 is your selected item from spinner it can be any nymber
TextView t = new TextView(MainActivity.this);
t.setText("value" + i);
// t.setTextSize(20);
// t.setTextColor(Color.BLUE);
mdynamiclayout.addView(t);
}
}
public void onNothingSelected(AdapterView<?> arg0){}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SelectBulbs();
rl = (RelativeLayout) findViewById(R.id.rl);
tv1 = new TextView (MainActivity.this);
tv1.setText("Dynamic TextView");
tv1.setTextColor(Color.RED);
tv1.setTextSize(20);
RelativeLayout.LayoutParams params1=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params1.leftMargin=115;
params1.topMargin=120;
rl.addView(tv1);
public class Unit {
private String _bulbNo;
public Unit(String bulbNo) {
_bulbNo = bulbNo;
}
public String getBulbNo(){
return _bulbNo;
}
}
// I created this class
private void addBulbsFans(String bulb)
{
Units.add(new Unit(bulb));
}
//我将此方法添加到 MainActivity.java。在此之后我需要知道如何从按钮单击事件传递微调器值。
您只需按照以下步骤将选定的字符串转换为整数
int value = Integer.valueOf(spiner.getselecteditem);
你使用Integer.valueOf()
,你的情况是这样的:
String[] bulbs = {"0","1","2","3","4","5"};
Integer[] bulbsConvert = new Integer[6];
//Array to int
for(String item : bulbs){
bulbsConvert.add(Integer.valueOf(item));
}
如果您想按垂直顺序创建动态文本视图,请在 xml 中创建 LinearLayout,并将 Orientation 设置为 Vertical,如下所示
<LinearLayout
android:id="@+id/dynamiclayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/registerlayout"
android:layout_below="@+id/registerlayout"
android:layout_marginLeft="16dp"
android:layout_marginTop="34dp"
android:orientation="vertical" >
</LinearLayout>
然后在您的 activity
中添加以下代码行private LinearLayout mdynamiclayout;
mdynamiclayout = (LinearLayout) findViewById(R.id.dynamiclayout);
mdynamiclayout.removeAllViews();//need to remove the view before it create
for (int i = 0; i < 6; i++) {//6 is your selected item from spinner it can be any nymber
TextView t = new TextView(MainActivity.this);
t.setText("value" + i);
mdynamiclayout.addView(t);
}
将一个 activity 传递给另一个 activity :
Intent intent_value = new Intent(Mainactivity.this,
yoursecondactivity.class);
intent_value.putExtra("key", value);
intent_value.putExtra("key2", value2);
startActivity(intent_value);
检索值:
Intent getValues = getIntent();
String value1 = getValues.getStringExtra("key");
如果您需要任何帮助,请告诉我