未显示数组列表的数据

时间:2017-01-13 10:26:58

标签: android arraylist android-edittext


我试图在TextView上显示数组列表的数据,但我有问题..
用户使用EditText在List上添加数据,然后,这些数据必须显示在TextView上,但它不起作用。
我不知道我是否需要设立基地,但我认为不需要它。

我的xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/add"
    android:layout_toRightOf="@+id/textView"
    android:layout_toEndOf="@+id/textView">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">

    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/add"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:text=""

    android:background="@android:drawable/ic_menu_add"
    android:tint="#91d1be"

    android:layout_below="@+id/textView"
    android:layout_toRightOf="@+id/textView"
    android:layout_toEndOf="@+id/textView" />

</RelativeLayout>

java代码是:

public class MainActivity extends Activity {

private LinearLayout mainLinearLayout;
int counter=0, i=0;

TextView mTextView;
Button add;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = (TextView)findViewById(R.id.textView);

    add = (Button) findViewById(R.id.add);
    mainLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LinearLayout linearLayout = new LinearLayout(MainActivity.this);
            TableLayout.LayoutParams lp = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
            linearLayout.setLayoutParams(lp);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);

            EditText ed;
            List<EditText> allEds = new ArrayList<EditText>();

            ed = new EditText(MainActivity.this);
            allEds.add(ed);
            ed.setId(counter);
            ed.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f));
            linearLayout.addView(ed);
            ed.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL);

            mainLinearLayout.addView(linearLayout);

            Double[] doubles = new Double[allEds.size()];
            String[] strings = new String[allEds.size()];

            for(i=0;i<allEds.size();i++){

                strings[i]= allEds.get(i).getText().toString();

                if(strings[i].length()>0){
                    doubles[i] = Double.parseDouble(strings[i]);
                }

                mTextView.setText(String.valueOf(strings[i]));
            }
        }
    });
}
}

有谁可以帮助我?谢谢!!

2 个答案:

答案 0 :(得分:1)

在onClick()方法之外初始化allEds并连接字符串数组的所有值以在textview中显示它。希望这会有所帮助 -

private LinearLayout mainLinearLayout;
int counter = 0, i = 0;

TextView mTextView;
Button add;
List<EditText> allEds;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView) findViewById(R.id.textView);

    add = (Button) findViewById(R.id.add);
    mainLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    allEds = new ArrayList<EditText>();

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LinearLayout linearLayout = new LinearLayout(MainActivity.this);
            TableLayout.LayoutParams lp = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
            linearLayout.setLayoutParams(lp);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);

            EditText ed;

            ed = new EditText(MainActivity.this);
            allEds.add(ed);
            ed.setId(counter);
            ed.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f));
            linearLayout.addView(ed);
            ed.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

            mainLinearLayout.addView(linearLayout);

            Double[] doubles = new Double[allEds.size()];
            String[] strings = new String[allEds.size()];


            for (i = 0; i < allEds.size(); i++) {

                strings[i] = allEds.get(i).getText().toString();

                if (strings[i].length() > 0) {
                    doubles[i] = Double.parseDouble(strings[i]);
                }
            }

            mTextView.setText(TextUtils.join(" ",strings));
        }
    });
}

答案 1 :(得分:0)

public class MainActivity extends Activity {

private LinearLayout mainLinearLayout;
int counter=0, i=0;

TextView mTextView;
Button add;
List<EditText> allEds;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = (TextView)findViewById(R.id.textView);

    add = (Button) findViewById(R.id.add);
    mainLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           LayoutParams linearLayout = new LayoutParams(
   LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);


            EditText ed;
allEds = new ArrayList<EditText>();

            ed = new EditText(MainActivity.this);
            allEds.add(ed);
            ed.setId(counter);
            ed.setLayoutParams(linearLayout);
 ed.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL);


            mainLinearLayout.addView(ed);

            Double[] doubles = new Double[allEds.size()];
            String[] strings = new String[allEds.size()];

            for(i=0;i<allEds.size();i++){

                strings[i]= allEds.get(i).getText().toString();

                if(strings[i].length()>0){
                    doubles[i] = Double.parseDouble(strings[i]);
                }

                mTextView.setText(String.valueOf(strings[i]));
            }
        }
    });
}
}