Я не могу заставить LongClick работать с ListActivity
Я уже спрашивал об этом, но не получил решение, которое работает, и с тех пор я исправил код.
Кто-нибудь может мне помочь и посоветовать, почему функция Long Click не работает, пожалуйста?:
public class InspectionActivity extends ListActivity {
private Button newInspection;
private static final int ACTIVITY_CREATE=0;
private RMDbAdapter rmDbHelper;
//private AlertDialog longClickOptionsDialog;
private AlertDialog clickOptionsDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inspection);
setUpViews();
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
// Get a Cursor for the list items
Cursor listCursor = rmDbHelper.fetchAllInspections();
startManagingCursor(listCursor);
// set the custom list adapter
setListAdapter(new MyListAdapter(this, listCursor));
}
private void setUpViews() {
newInspection = (Button)findViewById(R.id.new_inspection);
newInspection.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createInspection();
}
});
}
// In your ListActivity class, create a new inner class that extends ResourceCursorAdapter.
//This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item:
private class MyListAdapter extends ResourceCursorAdapter {
public MyListAdapter(Context context, Cursor cursor) {
super(context, R.layout.inspection_row, cursor);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.inspection_row_item_main_title);
title.setText(cursor.getString(
cursor.getColumnIndex(RMDbAdapter.INSPECTION_REF)));
TextView description = (TextView) view.findViewById(R.id.inspection_row_item_description);
String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
RMDbAdapter.INSPECTION_COMPANY)), "Company unknown");
String inspectionDate = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
RMDbAdapter.INSPECTION_DATE)), "date unknown");
description.setText("(" + companyName + ", " + inspectionDate + ")");
}
}
private void createInspection() {
Intent i = new Intent(this, InspectionEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
protected void onListItemClick(ListView l, View v, final int pos, final long id){
Cursor cursor = (Cursor) rmDbHelper.fetchInspection(id);
String inspectionRef = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
RMDbAdapter.INSPECTION_REF)), "Reference unknown");
String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
RMDbAdapter.INSPECTION_COMPANY)), "company unknown");
final String inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")";
super.onListItemClick(l, v, pos, id);
clickOptionsDialog = new AlertDialog.Builder(InspectionActivity.this)
.setTitle(inspectionDialogueText)
.setMessage(R.string.general_text_select_option)
.setPositiveButton(R.string.general_button_open, new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(InspectionActivity.this, AreaActivity.class);
i.putExtra("intentID", id);
startActivityForResult(i, ACTIVITY_CREATE);
}
})
.setNeutralButton(R.string.general_button_edit, new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Intent i = new Intent(InspectionActivity.this, InspectionEdit.class);
i.putExtra("intentID", id);
startActivityForResult(i, ACTIVITY_CREATE);
}
})
.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
clickOptionsDialog.cancel();
}
})
.create();
clickOptionsDialog.show();
}
protected void onListItemLongClick(ListView l, View v, final int pos, final long id){
Cursor cursor = (Cursor) rmDbHelper.fetchInspection(id);
String inspectionRef = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
RMDbAdapter.INSPECTION_REF)), "Reference unknown");
String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex(
RMDbAdapter.INSPECTION_COMPANY)), "company unknown");
final String inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")";
clickOptionsDialog = new AlertDialog.Builder(InspectionActivity.this)
.setTitle(inspectionDialogueText)
.setMessage(R.string.general_text_select_option)
.setPositiveButton(R.string.general_button_open, new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(InspectionActivity.this, AreaActivity.class);
i.putExtra("intentID", id);
startActivityForResult(i, ACTIVITY_CREATE);
}
})
.setNeutralButton(R.string.general_button_edit, new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Intent i = new Intent(InspectionActivity.this, InspectionEdit.class);
i.putExtra("intentID", id);
startActivityForResult(i, ACTIVITY_CREATE);
}
})
.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
clickOptionsDialog.cancel();
}
})
.create();
clickOptionsDialog.show();
}
Ваша помощь будет высоко ценится.
Спасибо,
Дейв
2 ответа
Решение
Вы не можете переопределить onListItemLongClick
потому что это не член ListActivity
http://developer.android.com/reference/android/app/ListActivity.html
Вы должны установить OnItemLongClickListener следующим образом:
ListView lv = getListView();
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {
// your code
}
});
Измените свой код на этот
newInspection.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});