-4

I am trying to fetch data from json array and display in edittext but i want to represent each element in seperate edittest,when the json array contains more than one element all the elements are single eddittext,how can i dynamicallly add edittext based on number of elements in the array

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]);

}
2

2 Answers 2

0

dont create ui for editext use like this.

        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;
    }
   }
Sign up to request clarification or add additional context in comments.

Comments

0

Use below Code to add EditText Dynamically into your parent Layout ,Pass Parent Layout Object in below method I have taken LinearLayout.

final String[] mList = assetName.split("\\|");
final String[] mListnumber = assetNumber.split("\\|");

make sure your mList Array and mListnumber Array length should be same other wise it may cause for 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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.