开发者

Add edittext dynamically and retrieve values in android

i added Edit Text dynamically but开发者_JAVA百科 not get values ,get values for last Edit Text . please help me...


public class EnterText extends Activity {

    Button btnMyLine,btnSave;
    LinearLayout LLEnterText;       
    int _intMyLineCount;

     private List<EditText> editTextList = new ArrayList<EditText>();
     private List<TextView> textviewList=new ArrayList<TextView>();
     private List<LinearLayout> linearlayoutList=new ArrayList<LinearLayout>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entertext);



        LLEnterText=(LinearLayout) findViewById(R.id.LlTitle);

        //LLEnterText.setOrientation(LinearLayout.VERTICAL);
        btnMyLine=(Button) findViewById(R.id.btnMyLines);       
        btnSave=(Button) findViewById(R.id.btnSave);


        btnMyLine.setOnClickListener(new OnClickListener() {        
            @Override
            public void onClick(View v) {                       
                    LLEnterText.addView(linearlayout(_intMyLineCount));
                    _intMyLineCount++;
                }
        });     

    btnSave.setOnClickListener(new OnClickListener() {      
            @Override
            public void onClick(View v) {   
                if (ETTitleEnterText.getText().length() == 0)
                {
                    Toast.makeText(EnterText.this, "Please Enter Full Details", Toast.LENGTH_LONG).show();
                }else{

                        for (EditText editText : editTextList) {                            
                            StartTabHost.VARClass._ArrLinesDetails.add(editText.getText().toString());
                        }   
                        for(TextView textview:textviewList){
                            StartTabHost.VARClass._ArrLinesTitle.add(textview.getText().toString());
                        }
                        for(int i=0;i<StartTabHost.VARClass._ArrLinesTitle.size();i++)
                    {
                    Log.d("LinesTitle",StartTabHost.VARClass._ArrLinesTitle.get(i));
                    Log.d("LinesDetails",StartTabHost.VARClass._ArrLinesDetails.get(i));
                    }               

                }

            }
        });
    }

private EditText editText(int _intID) {
            EditText editText = new EditText(this);
            editText.setId(_intID);
            editText.setHint("My lines");
            editText.setWidth(180);         
            editText.setBackgroundColor(Color.WHITE);
            editTextList.add(editText);
            return editText;
        }
    private TextView textView(int _intID)
    {
        TextView txtviewAll=new TextView(this);
        txtviewAll.setId(_intID);
        txtviewAll.setText("My lines:");        
        txtviewAll.setTextColor(Color.RED);
        txtviewAll.setTypeface(Typeface.DEFAULT_BOLD);
        textviewList.add(txtviewAll);
        return txtviewAll;
    }
    private LinearLayout linearlayout(int _intID)
    {
        LinearLayout LLMain=new LinearLayout(this);
        LLMain.setId(_intID);       
        LLMain.addView(textView(_intID));
        LLMain.addView(editText(_intID));
        LLMain.setOrientation(LinearLayout.HORIZONTAL);
        linearlayoutList.add(LLMain);
        return LLMain;

    }   

}


Here is the answer :

  btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < editTextList.size(); i++) {
                    System.out.println("Content"+editTextList.get(i).getText().toString());
                }

            }
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜