Android如何在单击ListView项时隐藏和显示ListView中的项目

时间:2013-09-17 11:32:14

标签: android android-listview

嘿我正在使用和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);

1 个答案:

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

        }

    });