我知道此网站已多次询问此问题,但没有任何答案有任何帮助。
以下是我的活动中的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_claim);
ListView list = ((ListView) findViewById(R.id.claim_list));
list.setAdapter(adapter);
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
PopupMenu popup = new PopupMenu(view.getContext(), view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.claim_item_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.delete_claim_menu_item) {
deleteFile(adapter.getClaims().remove(position).getId());
adapter.notifyDataSetChanged();
}
return true;
}
});
popup.show();
return true;
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivityForResult(new Intent(SelectClaimActivity.this, ViewClaimActivity.class)
.putExtra(OLD_CLAIM_KEY, (Parcelable) adapter.getClaims().get(position)),
EDIT_CLAIM_CODE
);
}
});
}
我的XML列表视图:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/claim_list"
>
</ListView>
列表项的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/claim_id"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/claim_created_date"/>
</LinearLayout>
最后,膨胀项目视图的代码:
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.claim_list_item, parent, false);
}
Claim claim = claims.get(position);
TextView idView = (TextView) convertView.findViewById(R.id.claim_id);
TextView dateView = (TextView) convertView.findViewById(R.id.claim_created_date);
idView.setText(claim.getId());
dateView.setText(FORMAT.format(claim.getCreationDate()));
return convertView;
}
onItemLongClickListener工作正常,但onItemClickListener永远不会触发(通过调试器确认)。
正如您所看到的,我的代码非常简单。
我尝试的事情(基于其他SO答案)没有奏效: