Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

java - How to change text in second_xml from mainActivity

I am developing the following application in android Studio-
    **user on entering todo  and click add button, it Should add to linear layout with checkbox which 
    is helpful to delete todos.**

my idea: I created a new XML file with textView and CheckBox in single_item.xml  and inflate it to  
         linearlayout which is present in main xml  as shown in 
          below.............................................

problem:  when I clicked the add button , app crashes. 
           I found that this issue is because I added textview.setText(text)  in  java code.
            if I had not added this line , then sample single_element.xml is getting added 
                 LinearLayout.But I needed to content of textview to be set to input 
                 that is given by user in editText as shown.



     I had deleted Most of the code in xml file because Stackoverflow is showing me "It looks like your post is mostly code; please add some more details." ""...............................................
          ..................................................................................................................................................................................................................................................................................................................................................................................

[activity_main.xml][1]
[single_element.xml][2]


[1]: https://i.stack.imgur.com/UhtGJ.png
[2]: https://i.stack.imgur.com/Pcu1O.png

In activity_main.xml I had LinearLayout,editText View,Add Button and delete button.

LinearLayout  -> id="@+id/linearLayout"
    

EditText        -> android:id="@+id/edit"

<Button        ->android:id="@+id/add_button"
    

single_element.xml

TextView     ->id="@+id/textView"
   
CheckBox    ->id="@+id/checkBox"
    

MainActivity.java

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
            EditText editText = (EditText) findViewById(R.id.edit);
            String text = editText.getText().toString().trim();
            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText(text);



            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.single_element,null);
            linearLayout.addView(view);
        }
    });
   

Thanks in Advance.................................................................................................................................................................................................................................................................................................................................................................................................


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to put the textbox and the Checkbox, all in the activity_main.xml, no need to create another layout file (single_element.xml) to add them since you need to display the text and the checkbox in the MainActivity class. Otherwise, you'll have to create another activity for single_element.xml that can be called SingleElementActivity for example then you pass the data entered in the editText from MainActivity to this SingleElementActivity using an intent.

See here how you can pass data from an activity to another activity with an Intent: How do I pass data between Activities in Android application?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...