TextView name = (TextView) findViewById(R.id.addName); LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout); Cursor cursor1 = db.rawQuery(queryText, null); cursor1.moveToFirst(); for (int i = 0; i < cursor1.getColumnCount(); i++) { TextView nameCol = new TextView(this); EditText inner = new EditText(this); nameCol.setText(" " + cursor1.getColumnName(i) + ": "); nameCol.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); nameCol.setTextSize(23); nameCol.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); layout.addView(nameCol); inner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); layout.addView(inner,i); } cursor1.close(); 

According to the code, H must be added to the TextView and EditText pairs, but first all TextViews are added, and then all the EditTexts.

    2 answers 2

    Right here

      layout.addView(inner,i); 

    EditText is added at the position 0,1,2 ... n, and TextEdit will be in the tail.

    • But can I somehow ask each edit id to later retrieve the data? - SerGon146
    • setId(int) via setId(int) , according to the documentation it should be a positive number. You are responsible for the uniqueness, the system will not swear in the case of duplicates. - Eugene Krivenja
    • Reading API17 there is a method View.generateViewId() , if you are too lazy to invent an id. - Eugene Krivenja pm

    Do so without an index for editText

      nameCol.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); layout.addView(nameCol); inner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); layout.addView(inner);