2012-05-25 65 views
0

此代碼有什麼問題?
我嘗試長時間點擊監聽器添加到列表視圖的,但不能讓它適合某些原因:ListView上的setOnLongClickListener不會觸發

package ch.futurecom.adaptertest; 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnLongClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class AdapterTestActivity extends ListActivity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     ListView lv = getListView(); 

     lv.setLongClickable(true); 
     lv.setOnLongClickListener(new OnLongClickListener() 
     { 
      @Override 
      public boolean onLongClick(View v) 
      { 
       Toast.makeText(AdapterTestActivity.this, "long clicked", Toast.LENGTH_SHORT).show(); 
       return true;  
      } 
     }); 

     lv.setAdapter(new ArrayAdapter<String>(this,R.layout.row,getResources().getStringArray(R.array.items))); 
    } 
} 

引擎收錄鏈接:
AdapterTestActivity.java - >http://pastebin.com/Pw30EkEz
row.xml - >http://pastebin.com/6FajncHT

謝謝!

回答

11

嘗試AdapterView.OnItemLongClickListener作爲

ListView lv = getListView(); 
      lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){ 
        @Override 
        public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) 
        { 
         Toast.makeText(AdapterTestActivity .this, "LongClicked", Toast.LENGTH_LONG).show(); 
        } 
      }); 
+1

的感謝!這解決了這個問題。 – marcin

+0

謝謝!它幫助了我!只是一個小紙條,你需要添加一個回報; – Ricardo

3

使用setOnItemLongClickListener insted的的setOnLongClickListener

lv.setOnItemLongClickListener (new OnItemLongClickListener() { 
    public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { 
    //do your stuff here 
    } 
}); 
相關問題