我想顯示在一個TextView一個數組列表的數據的數據,但我有問題..
用戶添加上的EditText,然後列出數據,這些數據必須在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]));
}
}
});
}
}
是人誰可以幫我嗎?謝謝!!
你是想自己重新實現ListView控件的功能? – Gudin
你的'ListView'在哪裏? –
對不起...數組列表我想說! –