如何长按listview并获取位置?并从sql中删除

时间:2016-07-25 09:54:25

标签: android sqlite listview

我无法从列表视图中获得长按的位置,但可以单击

请帮我看看我做错了什么?

我的代码:

adapter=new MoviesAdapter(this,R.layout.layout_row ,listOfmovies);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  //When user will click a row it will launch update Activity
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {

    //Identify the movie
    Movies newmovie=adapter.getItem(position);
    Intent updatemovie=new Intent(MainActivity.this,EditMovie.class);
    //will send the rowId number & movie id
    updatemovie.putExtra("rowId", newmovie.getMovieId());
    //will send the data + request code to updateActivity
    startActivityForResult(updatemovie, 0);
  }
});

//SET THE ONLONG CLICK EVENT
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  @Override
  public boolean onItemLongClick(AdapterView<?> adapterView, final View view, int i, final long l) {

    final AlertDialog.Builder adChooseAction = new AlertDialog.Builder(MainActivity.this) ;
    adChooseAction.setTitle("WHAT TO DO?");
    //GO TO EDIT SCREEN
    adChooseAction.setPositiveButton("EDIT", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialogInterface, int i) {
        Movies editmovie= adapter.getItem(i);
        Intent intentToEditScreen = new Intent(MainActivity.this,EditMovie.class);
        intentToEditScreen.putExtra("rowId" ,editmovie.getMovieId());
        startActivityForResult(intentToEditScreen,0);
      }
    });
    //DELETE THE ROW THE USER CHOOSED
    adChooseAction.setNeutralButton("DELETE", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialogInterface, int i) {
        //method to delete movies
        adapter.getItem(i);
        mds.open();
        //will access the MoviesDataSource class to deleteMoviesByRow method
        mds.deleteMovieByRow(i);
        mds.close();
      }
    });
    adChooseAction.show();

    return true;
  }
});

正如你在短片中看到的那样它可以工作并带我进入编辑界面,其中包含所点击的原始细节,但是当我在长按一下尝试它时,似乎我无法获得它指向的位置除了这个例外:

FATAL EXCEPTION: main
Process: com.cage.roll.midleprojectfinal, PID: 20175
java.lang.ArrayIndexOutOfBoundsException: length=27; index=-3
at java.util.ArrayList.get(ArrayList.java:310)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
at com.cage.roll.midleprojectfinal.MainActivity$2$2.onClick(MainActivity.java:85)

2 个答案:

答案 0 :(得分:4)

你应该使用onItemLongClick(AdapterView<?> parent, View view, int position, long id)

使用i重新定义position,它会正常工作

正如我从您的代码中看到的那样,您重新定义了函数i中的public void onClick(DialogInterface dialogInterface, int i)变量

然后,使用position索引变量来访问适配器数据

答案 1 :(得分:1)

试试这个:

listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                // do your work here

                return false;
            }
        });