1

I am trying to add views(TextView) in linearlayout prgramatically. I am able to add textview in linearlayout programatically.

Here is my code to add textview:

public void setSelectedContactTextView(final ArrayList<Object> list){
        //Constants.progressDialog=ProgressDialog.show(this, "", Constants.MSG_PROGESSDIALOG);
        /*  new Thread(new Runnable() {

            @Override
            public void run() {*/
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                while(i<list.size()){
                    ContactBean contactBean=(ContactBean)list.get(i);
                    if(contactBean.isSelected()==true){
                        View line = new View(NewEventShowDetails.this);
                        line.setLayoutParams(new LayoutParams(1, LayoutParams.WRAP_CONTENT));
                        final TextView contactTextView=new TextView(NewEventShowDetails.this);
                        contactTextView.setText(contactBean.getEmailId().toString());
                        contactTextView.setPadding(3,3, 3, 3);
                        contactTextView.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(NewEventShowDetails.this, contactTextView.getText(), Toast.LENGTH_SHORT).show();
                            }
                        });
                        fbContactTextLinearLayout.addView(contactTextView);
                        fbContactTextLinearLayout.addView(line);
                        count++;
                    }
                    i++;
                }
            }
        });

        /*}
        });*/
    }

I am calling setSelectedContactTextView() from OnResume() in activty. It adds textviews in activity first time but after calling removeAllTextViewsFromLayout() it won't add textviews again.

Here is the code to remove textview:

public void removeAllTextViewsFromLayout(){
        final int childcount = fbContactTextLinearLayout.getChildCount();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                for (int i=0; i < childcount; i++){
                    fbContactTextLinearLayout.removeViewInLayout(fbContactTextLinearLayout.getChildAt(i));
                }
            }
        });
    }

This is defined in OnCreate()

fbContactTextLinearLayout=(LinearLayout)findViewById(R.id.fbcontact_text_layout);
7
  • What about your removeViewInLayout(fbContactTextLinearLayout.getChildAt(i)); method code? Commented May 29, 2013 at 5:03
  • Why don't you just call fbContactTextLinearLayout.removeAllViews(); Commented May 29, 2013 at 5:06
  • do you only call setSelectedContactTextView() from onResume()? When did You call this after removing views? If you only delete the views without doin anything else like leave the activity or pause or something like this, onResume won´t be called again. Commented May 29, 2013 at 5:08
  • @Neil I used that it will remove all textviews but after removing textviews Theses are not adding again. Commented May 29, 2013 at 5:09
  • @Opiatefuchs Sorry but my onResume() will be called everytime. Commented May 29, 2013 at 5:12

1 Answer 1

1

Is your i counter being reset? Based on the code you showed i is not a local variable.

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            while(i<list.size()){

If you're not resetting i your loop will not run the second time (assuming the list hasn't had anything added to it).

Sign up to request clarification or add additional context in comments.

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.