以下是我在测试的代码,当我给任何特定小部件充气时,我不能真正对它进行setText。它会覆盖文本并返回最后一个字符串。在输出屏幕中,我可以看到从xml添加的五个textview但是我试图设置的一个没有发生我只看到str []中的一个文本,即str [5],这是数组中的最后一个。如果我能够解释我的问题,请告诉我。
public class TestInflate extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View[] inflated = new View[5];
for(int i = 0; i<5; i++)
{
TableLayout myTableLayout = (TableLayout)findViewById(R.id.TableLayout);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflated[i] = inflater.inflate(R.layout.testbutton, myTableLayout);
TextView userName = (TextView)inflated[i].findViewById(R.id.myName);
userName.setText(str[i]);//here i should get name but not getting
}
}
String[] str = {"a","s","d","r","t"};
}
我的testButton.xml如下
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:text="Name :"
android:textStyle="bold"
android:layout_weight=".25"
></TextView>
<TextView
android:id="@+id/myName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_weight=".5"
></TextView>
</TableRow>
和main.xml是
<ScrollView
android:id="@+id/Scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout
android:id="@+id/TableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
</TableLayout>
</ScrollView>
编辑和输入就像这样
My output is like this
Name : t //str[5]
Name :
Name :
Name :
Name :
答案 0 :(得分:0)
这次它经过测试。 试试这个。
public class MainActivity extends Activity {
/** Called when the activity is first created. */
String[] str = {"a","s","d","r","t"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTableLayout = (TableLayout)findViewById(R.id.TableLayout);
LayoutInflatedView myClasses [] = new LayoutInflatedView[5];
for(int i = 0; i<5; i++)
{
myClasses[i] = new LayoutInflatedView(this, myTableLayout);
myTableLayout.addView(myClasses[i]);
myClasses[i].setUserName(str[i]);
}
}
}
您的LayoutInflatedView是:
public class LayoutInflatedView extends LinearLayout
{
TextView text;
public LayoutInflatedView(Context context, ViewGroup table)
{
super(context);
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.test_button,this);
text = (TextView)findViewById(R.id.myName);
}
public void setUserName(String text)
{
this.text.setText(text);
}
}
检查此项并告知我们是否出现任何问题。
答案 1 :(得分:0)
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
你也可以在你的for循环中尝试这个。 供参考:Look Here