listview長按事件 並擷取選中行的值

來源:互聯網
上載者:User

            學習android作為業餘興趣,邊學邊做了個小應用。昨天用到了listview長按事件,彈出下拉式功能表,把這作為個知識點,供以後學慣用。

 

            首先,假設listview已經有資料,不管是從sqlite中擷取還是程式內固定的值,下面直接給出長按事件的寫法。

 

 

       activiy中:

 

          //ListView lvjzw;// listview顯示列表

           lvjzw.setOnCreateContextMenuListener(lvjzwOnCreateContextMenuListener);

 

/**
  * 長按彈出下拉式功能表 ,此事件中可彈出下拉框
  */
 OnCreateContextMenuListener lvjzwOnCreateContextMenuListener = new OnCreateContextMenuListener()
 {

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
  {
   menu.setHeaderTitle("共用給連絡人");//標題
   menu.add(0, 1, 0, "使用簡訊發送給好友");//下拉式功能表
   // menu.add(0, 2, 0, "彈出式菜單2");
  }
 };

 /**
  * 長按菜單響應函數 並擷取選中的listview行內容
  */
 @Override
 public boolean onContextItemSelected(MenuItem item)
 {

  ContextMenuInfo info = item.getMenuInfo();
  AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterContextMenuInfo) info;
  // 擷取選中行位置
  int position = contextMenuInfo.position;
  // 擷取問題內容
  question = dataRecordList.get(position).getQuestion();
  Toast.makeText(JzwActivity.this,question+"===",Toast.LENGTH_SHORT).show();

/********************************到此為止,即可擷取到長按那一列listview中的值*****************************************************************************************/

  // 調用手機連絡人,傳送簡訊
  Intent intent = new Intent();
    intent.setAction(Intent.ACTION_PICK);
  intent.setData(Contacts.Phones.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);
  return super.onContextItemSelected(item);
 }

 

長按某一行,彈出下拉式功能表,

 

點擊後,在Toast中顯示選中內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.