listView.setOnItemClickListener header 项禁用
listView.setOnItemClickListener header item disable
我正在使用此代码来选择列表中的项目。我想禁用 item0 - header,但没有用。尝试放置不同的方法,如禁用等。任何人都可以建议一种方法来禁用单击 header 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);
}
最后,我通过研究解决了问题 - 使用覆盖 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);
}
}
});
我正在使用此代码来选择列表中的项目。我想禁用 item0 - header,但没有用。尝试放置不同的方法,如禁用等。任何人都可以建议一种方法来禁用单击 header 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);
}
最后,我通过研究解决了问题 - 使用覆盖 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);
}
}
});