0

我正在尝试显示 tab_s 但我唯一得到的只是 LogCat 显示的变量“s”但是当我尝试将此变量放入 String [] 时它只会显示黑屏我如何将这个变量放入一个字符串数组

private void allbuttons()
{       Class cls = R.drawable.class;
        fieldlist = cls.getDeclaredFields();
        setContentView(layoutIds[position]);
        String[] tab_s = null;
        ListView listView = (ListView) findViewById(R.id.listViewallbuttons);

        allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);

        Toast.makeText(this, "" + allButtonsInLayout.size(), Toast.LENGTH_SHORT).show();


        for(  int i=0;i<allButtonsInLayout.size();i++){






            cls = R.drawable.class;
            fieldlist = cls.getDeclaredFields();

            String fieldName = null ;

            for (java.lang.reflect.Field fld1 : fieldlist) {



                try {
                    setContentView(R.layout.listallbuttons);
                    ListView listView1 = (ListView) findViewById(R.id.listViewallbuttons);



                    drawableId = fld1 .getInt(null);
                    String s=getResources().getResourceEntryName(drawableId);
                    Log.v("tagllll", "msg" + s);

                    tab_s[i]=s;



                    Log.v("tagtab_s", "msg" + tab_s[i]);



                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1, android.R.id.text1,tab_s);
                    listView1.setAdapter(adapter);
4

1 回答 1

0

我找到了一个解决方案,问题是 tab_s 的大小,所以我的想法是在 tab_s 的大小很小时创建另一个数组,然后将 tab_s 复制到另一个数组 temp 中,最终 tab_s 将包含我想要的任何内容!

Class cls = R.drawable.class;
fieldlist = cls.getDeclaredFields();
setContentView(layoutIds[position]);
java.lang.reflect.Field fld1;
ListView listView = (ListView) findViewById(R.id.listViewallbuttons);

allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
//allButtonsInLayout = getViewsFromViewGroup(findViewById(android.R.id.content), Button.class);
//String tabname=allButtonsInLayout.get(position).getResources().getResourceEntryName(position);
Toast.makeText(this, "" + allButtonsInLayout.size(), Toast.LENGTH_SHORT).show();
int size=allButtonsInLayout.size();

String[] tab_s = new String [size];
int k = 0;
String s;

    // Bitmap bm = null;
    // SQLiteAdapter mySQLiteAdapter =new SQLiteAdapter(getApplicationContext());
    //mySQLiteAdapter.openToRead();
    // bm=mySQLiteAdapter.queueAllphoto();
    //mySQLiteAdapter.close();
    //allButtonsInLayout.get(i).setBackgroundDrawable(d);

    cls = R.drawable.class;
    fieldlist = cls.getDeclaredFields();

    String fieldName = null ;

    for (java.lang.reflect.Field fld : fieldlist) {

        for(  int i=0;i<allButtonsInLayout.size();i++){

        try {


            setContentView(R.layout.listallbuttons);
            ListView listView1 = (ListView) findViewById(R.id.listViewallbuttons);



            drawableId = fld .getInt(null);

             s=getResources().getResourceEntryName(drawableId);

            Log.v("tagllll", "msg" + s);

            if((k<=i)) {

             tab_s[k++]=s;
            }
            else{
                size++;

                String[]temp=new String[size];

                for (int f=0;f<tab_s.length;f++){
                    temp[f]=tab_s[f];

                }
                tab_s=temp;
                /*for (int f=0;f<tab_s.length;f++){
                    if (tab_s[f]==tab_s[f--]){
                        tab_s[f]=null;
                    }
                }*/
                if(tab_s[k]!=s)
                tab_s[k++]=s;

            }



            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, android.R.id.text1,tab_s);

                listView1.setAdapter(adapter);





            //MyAdapter mine=new MyAdapter(getBaseContext(),s);

            /*Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("application_name");
            builder .setMessage("Vous avez cliqué sur : " + s);
            builder.show();*/






            //Log.v("tagtab_s", "msg" + tab_s[i]);

            //layoutButtons[k]=fieldName;


            drawableId--;
            allButtonsInLayout.get(i).setBackgroundResource(drawableId);
            //int k=getResources().getInteger(drawableId);





        }
        catch(Exception e){

            e.getMessage();
            Toast.makeText(DvpList2.this,
                    "plus d'images", Toast.LENGTH_SHORT).show();
        }
        allButtonsInLayout.get(i).setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    try{
                        drawableId--;
                        //allButtonsInLayout.get(i).setBackgroundResource(drawableId);
                    }
                    catch(Exception e){

                        e.getMessage();
                        Toast.makeText(DvpList2.this,
                                "plus d'images", Toast.LENGTH_SHORT).show();
                    }
                }








            });




        // do whatever with the drawable

    }


}





    //Bitmap bmp = BitmapFactory.decodeByteArray(content,0,content.length);

    //image =  new BitmapDrawable(BitmapFactory.decodeByteArray(content, 0, content.length));


    Toast.makeText(DvpList2.this,
            "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
于 2013-02-15T12:42:11.417 回答