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

1 回答 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).

于 2013-05-29T05:38:43.317 回答