【问题标题】:How to set up LongClick in ListActivity to create AlertDialogueListActivity中如何设置LongClick来创建AlertDialogue
【发布时间】:2012-08-21 15:31:18
【问题描述】:

我无意中尝试在我的 ListActivity 中的 onLongClick 之后设置 AlertDialogue。如下所示,我已经设置了 onListItemClick 以毫无问题地打开 AlertDialogue 并且它工作正常,但是我找不到在此之上调用 LongClick 的正确方法:

public class InspectionActivity extends ListActivity {
private Button newInspection;
private static final int ACTIVITY_CREATE=0;

private RMDbAdapter rmDbHelper;
private AlertDialog clickOptionsDialog;
private String inspectionDialogueText;


@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 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"); 

    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) {
            Toast.makeText(getApplicationContext(), inspectionDialogueText + " (Area activity to be created)", 
                    Toast.LENGTH_LONG).show(); 
            clickOptionsDialog.cancel();
            }
        })
    .setNeutralButton(R.string.general_button_export, new AlertDialog.OnClickListener(){
        public void onClick(DialogInterface dialog, int which){

            Toast.makeText(getApplicationContext(), inspectionDialogueText + " (Export activity to be created)", 
                    Toast.LENGTH_LONG).show(); 
            clickOptionsDialog.cancel();
            }
        })

    .setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener(){
        public void onClick(DialogInterface dialog, int which){
            clickOptionsDialog.cancel();
            }
        })
    .create();
    clickOptionsDialog.show();

感谢您的帮助!!

戴夫

【问题讨论】:

    标签: android android-alertdialog long-click


    【解决方案1】:

    您可以使用 OnItemLongClickListener 实现 onListItemLongClick 事件来处理对列表视图项的长按。

    【讨论】:

    • 感谢您的快速响应,但我应该把 OnItemLongClickListener 放在哪里?有没有你知道的例子 - 请记住我想调用 AlertDialogue 而不是 ContextMenu ..
    • 我想这是正确的(但我现在无法测试)受保护的 void onListItemLongClick(ListView l, View v, final int pos, final long id){} 作为 Activity 类的方法.在这个方法中你可以放任何你想要的代码(事件一个 AlertDialog)
    猜你喜欢
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    相关资源
    最近更新 更多