2

嘿,我正在使用和 android ListView,当我单击 ListView 项目时,我想显示一些项目,同时在同一个 ListView 中添加其他项目。我的意思是当我点击一个项目时,一些图像会显示在同一行。当我单击另一个项目时,以前的图像会消失,而我只剩下新行中的图像,请帮助这是我的代码

inventoryList.setAdapter(mAdapter);
    inventoryList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub                  
            LinearLayout lButtons;

            // Hides all items in the listView adapter  
            for (int i = 0; i < inventoryList.getAdapter().getCount(); i++) {


                   View v1 = inventoryList.getChildAt(i);  
                   lButtons = (LinearLayout) v1.findViewById(R.id.layout_update_buttons);
                   lButtons.setVisibility(View.GONE);


            }

            //Display images of currently clicked item
            lButtons = (LinearLayout) arg1.findViewById(R.id.layout_update_buttons);
            lButtons.setVisibility(View.VISIBLE);

        }

    });

它目前在这个特定的行中给了我一个 java.lang.nullPointerException

lButtons = (LinearLayout) v1.findViewById(R.id.layout_update_buttons);
4

1 回答 1

1

这是我在一些帮助后得到的答案

inventoryList.setAdapter(mAdapter);
    inventoryList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub                  
            LinearLayout lButtons;

            // Hides all items in the listView adapter  
            for (int i = 0; i < inventoryList.getAdapter().getCount(); i++) {


                   View v1 = inventoryList.getChildAt(i);  
                   if(v1 != null){
                   lButtons = (LinearLayout) v1.findViewById(R.id.layout_update_buttons);
                   lButtons.setVisibility(View.GONE);
                   }


            }

            //Display images of currently clicked item
            lButtons = (LinearLayout) arg1.findViewById(R.id.layout_update_buttons);
            lButtons.setVisibility(View.VISIBLE);

        }

    });
于 2013-09-17T11:42:48.133 回答