Search code examples
androidonitemclicklistener

listView.setOnItemClickListener header item disable


I'm using this code for selecting the items in a list. I want to disable the item0 - header, but in vain. Tried placing different methods like disable etc. Can anyone suggest a method to disable clicking on the header item0.

Code in JSON response:

HashMap<String, String> item0 = new HashMap<>();
        
        item0.put("empid", "Employee ID");
        item0.put("empName", "Employee Name");
        ...

        list.add(item0);

        for (int i = 0; i < jarray.length(); i++) {

            JSONObject jo = jarray.getJSONObject(i);

            String lempid = jo.getString("empid");
            String lempName = jo.getString("empName");
            ....
         
            list.add(leave);
        }

Code in OnItemClickListener:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                
                listView.getChildAt(0).setEnabled(false);
                //listView.setSelectionAfterHeaderView();
                /*
                  if ((position)!="your item"){
                    view.setEnabled(true);
                    view.setClickable(true);
                  } else{
                    view.setEnabled(false);
                    view.setClickable(false);
                  }
                */

                Intent intent = new Intent(ShowLeaves.this, ListLeave.class);
                HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
                String mempid = map.get("empid").toString();
                String mempName = map.get("empName").toString();
                ...
              
                intent.putExtra("empid", mempid);
                intent.putExtra("empName", mempName);
                ...

                startActivity(intent);
      }

Solution

  • Finally I was able to resolve by doing research - Override the default behaviour with Overriding public function and enclose my code with a if condition.

    public boolean isEnabled(int position) {
        // returns false based on the condition
        Boolean result = true;
        if(position==0){
            result =  false;
        }
        return  result;
    }
    

    and modify the main code as below:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                
                listView.getChildAt(0).setEnabled(false);
                
                if(isEnabled(position)){
                  Intent intent = new Intent(ShowLeaves.this, ListLeave.class);
                  HashMap<String, String> map =(HashMap)parent.getItemAtPosition(position);
                  String mempid = map.get("empid").toString();
                  String mempName = map.get("empName").toString();
                  ...
              
                  intent.putExtra("empid", mempid);
                  intent.putExtra("empName", mempName);
                  ...
    
                  startActivity(intent);
               }
             }
    
           });