我试图从json数组中获取数据并在edittext中显示但是我想在单独的edittest中表示每个元素,当json数组包含多个元素时,所有元素都是单个eddittext,我怎样才能动态地添加edittext关于数组中元素的数量
final String[] mList = assetName.split("\\|");
final String[] mListnumber = assetNumber.split("\\|");
for(i=0;i<mList.length;i++){
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View child=inflater.inflate(R.layout.childlayout,null);
EditText title1=(EditText)child.findViewById(R.id.et_armoryname);
EditText title2=(EditText)child.findViewById(R.id.et_armorynumber);
//EditText title3 = (EditText)child.findViewById(R.id.et_qr);
title1.setText(mList[i]);
title2.setText(mListnumber[i]);
}
答案 0 :(得分:0)
不要像这样使用editext创建ui。
public class MainActivity extends Activity {
private static final int VISIBLE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for json data in spouse it is in arraylist otherwisse parse data and store in arrylist or any other variable.
iterator itr=al.iterator();
while(itr.hasNext()){
EditText editText = new EditText(this);
editText.setText(itr.next());
editText.setVisibility(VISIBLE);
}}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 1 :(得分:0)
使用以下代码将EditText
动态添加到您的父布局中,通过以下方法传递父布局对象LinearLayout
。
final String[] mList = assetName.split("\\|");
final String[] mListnumber = assetNumber.split("\\|");
确保您的mList
数组和mListnumber
数组长度应该相同,否则可能导致ArrayIndexOutOfException。
private void addEditTextDynamically(LinearLayout mParentLayout, String[] myList)
{
for (int i = 0; i < myList.length; i++)
{
EditText myEditText = new EditText (mParentLayout.getContext()); //Context
myEditText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
myEditText.setId(i);
myEditText.setTag(myList[i]);
myEditText.setText(myList[i] + " number" + mListnumber [i]);
myEditText.setTextColor(Color.parseColor("#785412"));
mParentLayout.addView(myEditText);
}
}