列表视图中的所选项目

时间:2013-02-26 10:41:43

标签: button drawable

在布局中我点击按钮显示res / drawable中所有drawable的列表当我点击其中一个时我必须显示相同的布局我的问题是如何访问用户点击之前的按钮< / p>

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

1 个答案:

答案 0 :(得分: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();

                                    }

                                }