我有一个空LinearLayout
,我需要为其添加动态数量的TextView
。但是,当我使用下面的代码时,只会显示第一个TextView
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] listofnumbers = new String[1000];
for ( int i = 0 ; i < 1000 ; ++i ) {
listofnumbers[i] = "null";
}
Context context = getBaseContext();
String text = null;
Uri uri = Uri.parse("content://sms");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
String[] columnNames = cursor.getColumnNames();
LinearLayout lv = new LinearLayout(context);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, dip(48));
boolean v = true;
while (cursor.moveToNext())
{
String numberString = (cursor.getString( cursor.getColumnIndexOrThrow("address") ) ).replace(" ", "");
int i = 0;
boolean numberNotPresent = true;
for ( ; listofnumbers[i] != "null" ; ++i ) {
if ( numberString.equals(listofnumbers[i]) ) {
numberNotPresent = false;
}
}
if ( numberNotPresent == true ) {
text = (CharSequence) "From: " + cursor.getString(cursor.getColumnIndexOrThrow("address")) + ": " + cursor.getString(cursor.getColumnIndexOrThrow("body"));
listofnumbers[i] = numberString;
TextView tv = new TextView(this);
tv.setText(text);
tv.setLayoutParams(textViewParams);
lv.addView( tv );
}
}
setContentView(lv);
}
我哪里出错了?
答案 0 :(得分:3)
尝试更改这两行:
LinearLayout lv = new LinearLayout(context);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, dip(48));
用这些:
LinearLayout lv = new LinearLayout(context);
lv.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
基本上你需要将lv LinearLayout的方向设置为垂直,因为默认值是水平的。
答案 1 :(得分:1)
我不确定,但是你可以为每个textView设置fill_parent param吗?第一个textView显示在您的所有布局上。