2012-05-27 65 views
0

我目前正在嘗試從onItemLongClick方法傳遞數據到AlertDialog,我試圖找到最佳/正確的做法,我如何做到這一點。正確的方式來傳遞數據showOptionsDialog

雖然我現在正在做的工作,它感覺不對,我希望這裏有人能夠提供給我正確的解決方案,並解釋爲什麼它是正確的解決方案。

我的代碼如下,並在長onItemLongClick內我設置屬性的列表視圖中已被點擊,然後從AlertDialog.Builder中訪問該屬性的行項目。

public class ListViewExample { 

    private long clickedRowId; 
    .... 

    mListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long rowId) { 
      clickedRowId = rowId; 
      /* here is my issue, surely it is better to pass rowId into showOptionsDialog as an argument? */ 
      showOptionsDialog(); 
      return true; 
     } 
    }); 


    private void showOptionsDialog() { 

     new AlertDialog.Builder(this.context) 
      .setTitle(R.string.stack_dialog_title) 
      .setItems(R.array.stack_options, new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int selected) { 

        switch (selected) { 
        case 0: 
         //perform selection #1 
         break; 
        case 1: 
         //perform selection #2 
         break; 
        case 2: 
         deleteRowItem(clickedRowId); 
         break; 
        } 

       } 

     }).show(); 
    } 


} 

回答

0
public class ListViewExample { 

.... 

mListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long rowId) { 
     /* here is my issue, surely it is better to pass rowId into showOptionsDialog as an argument? */ 
     showOptionsDialog(rowId); 
     return true; 
    } 
}); 


private void showOptionsDialog(long rowId) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); 
    builder.setTitle(R.string.stack_dialog_title); 
    builder.setItems(R.array.stack_options, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int selected) { 
     //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
     switch (selected) { 
      case 0: 
       //perform selection #1 
       break; 
      case 1: 
       //perform selection #2 
       break; 
      case 2: 
       deleteRowItem(rowId); 
       break; 
      } 
     } 
    }); 
     AlertDialog alert = builder.create(); 
     alert.show(): 


    } 


} 
0

我認爲ü可以做到這樣。希望它可以幫助ü

公共類ListViewExample {

private long clickedRowId; 
    .... 

    mListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<YourObj> list, View arg1, int position, long rowId) { 
      clickedRowId = rowId; 
      YourObj clickObj list.getItemAtPosition(position) 
      /* here is my issue, surely it is better to pass rowId into showOptionsDialog as an argument? */ 

      showOptionsDialog(clickObj); 
      return true; 
     } 
    }); 


    private void showOptionsDialog(YourObj clickedObj) { 

     new AlertDialog.Builder(this.context) 
      .setTitle(R.string.stack_dialog_title) 
      .setItems(R.array.stack_options, new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int selected) { 

        switch (selected) { 
        case 0: 
         //perform selection #1 
         //do some thing about clickedObj 
         break; 
        case 1: 
         //perform selection #2 
         //do some thing about clickedObj 
         break; 
        case 2: 
         deleteRowItem(clickedRowId); 
         break; 
        } 

       } 

     }).show(); 
    } 


} 
相關問題