【问题标题】:listView.setOnItemClickListener header item disablelistView.setOnItemClickListener 标头项禁用
【发布时间】:2020-12-17 22:48:45
【问题描述】:

我正在使用此代码来选择列表中的项目。我想禁用 item0 - 标题,但徒劳无功。尝试放置不同的方法,如禁用等。任何人都可以建议一种方法来禁用单击标题 item0。

JSON 响应中的代码:

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

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

【问题讨论】:

  • 请帮忙

标签: android onitemclicklistener


【解决方案1】:

最后我能够通过研究来解决 - 使用 Overriding public 函数覆盖默认行为并用 if 条件包围我的代码。

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

并修改主代码如下:

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

       });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多