0

在布局中,当我单击其中一个时,我单击一个按钮以显示 res /drawable 中所有可绘制对象的列表 我必须显示相同的布局 我的问题是如何访问用户之前单击的按钮

public void onItemClick(AdapterView<?> arg0, View arg1,
        int arg2, long arg3) {
    setContentView(layoutIds[position]);
    Button button=allButtonsInLayout.get(arg2);
    button.setBackgroundResource(R.drawable.images_sos);
}

如果我这样做,我可以改变背景:

Button button=(Button)findViewById(R.id.button);

但是如果我将该行更改为

Button button=allButtonsInLayout.get(arg2);
4

1 回答 1

0
setContentView(R.layout.otherview);
                        //matable is a table of all drawable it contains exactly thier id
                        //then i creat exactly the same number of buttons than the number of drawable's id
                        for (int k1=0;k1<matable.length;k1++){

                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                    LinearLayout.LayoutParams.MATCH_PARENT,
                                    LinearLayout.LayoutParams.WRAP_CONTENT);
                            Button btn = new Button(getBaseContext());
                            btn.setId(k1);
                            final int id_ = btn.getId();
                            LinearLayout linear = (LinearLayout) findViewById(R.id.linearscroll);
                            linear.addView(btn, params);
                            final Button btn1 = ((Button) findViewById(id_));
                            try{
                                btn1.setBackgroundResource(matable[k1]);
                                ht = new Hashtable<Integer, Integer>();
                                ht.put(id_, matable[k1]);
                                ht.put(3, 145);
                            }catch(Exception e){
                                e.printStackTrace();
                            }
                            btn1.setId(k1);
                            btn1.setOnClickListener(new View.OnClickListener() {
                                @SuppressWarnings("deprecation")
                                public void onClick( View view) {
                                    setContentView(lemien);
                                    int test=v.getId();
                                    test=test+3;
                                    //and it 's exactly here where i make the comparaison 
                                    if(btn1.getId()==view.getId()){

                                        int cpt=0;
                                        tabIdButton[cpt]=view.getId();
                                        d=btn1.getBackground();


                                        BitmapDrawable bmd = (BitmapDrawable) d;

                                        Bitmap bm = bmd.getBitmap();
                                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                        bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                                        byte[] bitmapdata = stream.toByteArray();

                                        try{
                                            Button btn3 = ((Button) findViewById(v.getId()));
                                            btn3.setBackgroundDrawable(d);
                                            SQLiteAdapter mySQLiteAdapter =new SQLiteAdapter(getBaseContext());
                                            mySQLiteAdapter.openToWrite();
                                            mySQLiteAdapter.insertintdrawableid(btn3.getId());

                                            mySQLiteAdapter.insertphotobouton(bitmapdata);

                                            mySQLiteAdapter.close();
                                            Log.v("btn2", "btn2 j'ai chnagé");
                                        }catch(Exception e){
                                            e.printStackTrace();

                                    }

                                }
于 2013-05-15T15:28:32.797 回答