0

这段代码有什么问题?
我尝试向列表视图添加一个长点击监听器,但由于某种原因无法正确处理:

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)));
    }
}

pastebin 链接:
AdapterTestActivity.java -> http://pastebin.com/Pw30EkEz
row.xml -> http://pastebin.com/6FajncHT

谢谢!

4

2 回答 2

12

尝试 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();
                  } 
             }); 
于 2012-05-25T10:47:32.170 回答
3

使用setOnItemLongClickListenerinsted ofsetOnLongClickListener

lv.setOnItemLongClickListener (new OnItemLongClickListener() {
  public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
    //do your stuff here
  }
});
于 2012-05-25T10:50:29.703 回答