2

I need to creat a linearLayout containing some views when clicking on a button , thsi button is triggerd by an action made in another activity ,so i used a performClick , but it seems that doesn't work ; here is my code :

Button click = new Button(rootView.getContext());
            SharedPreferences participant;
            Editor editor;

            SharedPreferences visible;
            Editor vis;
            participant = rootView.getContext().getSharedPreferences("participant", rootView.getContext().MODE_PRIVATE);
            visible = rootView.getContext().getSharedPreferences("visible", rootView.getContext().MODE_PRIVATE);

            editor = participant.edit();
            final String name= participant.getString("key", "toto");
            final String view = participant.getString("view","non");
            if(view.equalsIgnoreCase("yes")) click.performClick();

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


                    // Creating a new LinearLayout
                    final LinearLayout ln = new LinearLayout(v.getContext());

                    // Setting the orientation to horizontal
                    ln.setOrientation(LinearLayout.HORIZONTAL);


                    formbis.addView(ln);

                    TextView tv1 = new TextView(v.getContext());
                    tv1.setText(name);
                    tv1.setTextSize(14);
                    tv1.setTypeface(null, Typeface.BOLD);
                    tv1.setPadding(0, 15, 0, 10);
                    tv1.setLayoutParams(new LayoutParams(
                            500,
                            LayoutParams.WRAP_CONTENT));
                    ln.addView(tv1);

                    final ImageButton edit = new ImageButton(v.getContext());
                    Drawable checked = getResources().getDrawable( R.drawable.content_edit );
                    edit.setImageDrawable(checked);
                    edit.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    edit.setBackgroundColor(Color.WHITE);
                    edit.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {


                           i = new Intent(v.getContext(), EditParticipantActivity.class);
                            startActivity(i);

                        }

                    });
                    ln.addView(edit);

                    final ImageButton delete = new ImageButton(v.getContext());
                    Drawable deleted = getResources().getDrawable( R.drawable.content_discard );
                    delete.setImageDrawable(deleted);
                    delete.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    delete.setBackgroundColor(Color.WHITE);
                    delete.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                             Toast.makeText(v.getContext(),
                                        "Deleted ", Toast.LENGTH_LONG).show();

                           formbis.removeView(ln);

                        }

                    });
                    ln.addView(delete);
                    ln.setVisibility(View.GONE);
                    if(view.equalsIgnoreCase("yes"))ln.setVisibility(View.VISIBLE);

                }
            });

EDIT

Here (another Activity ) I set the value to yes so that the linearLayout gets added to the view of the first activity

Button members = (Button) findViewById(R.id.submit);
        members.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                name = firstname.getText().toString();

                editor.putString("key", name);
                editor.commit();

                editor.putString("view", "yes");
                editor.commit();

                i = new Intent(v.getContext(), ManageActivity.class);
                startActivity(i);
                //finish();

            }

});

4

1 回答 1

7

定义第onClickListener一个。

if(view.equalsIgnoreCase("yes")) click.performClick();然后在定义click.onClickListner代码后调用该行。

于 2013-06-13T16:04:05.213 回答