Java SQLIte数据库无法删除条目

Java SQLIte数据库无法删除条目,java,android,sqlite,Java,Android,Sqlite,我有一个SQLdatabase,我想使用列表视图中每个条目旁边的按钮删除列表视图中显示的条目。但目前它不允许我删除 此问题位于selectionargs变量中,如下所示。如果我手动在selectionargs中输入一个数字,例如1,它会起作用,但我一直试图通过代表每个条目的变量来实现。这不会导致错误,只是直接转到toast消息“无法删除”。ac.ID是指适配器类和列表项条目的ID 书签类: public class Bookmark extends AppCompatActivity { my

我有一个SQLdatabase,我想使用列表视图中每个条目旁边的按钮删除列表视图中显示的条目。但目前它不允许我删除

此问题位于
selectionargs
变量中,如下所示。如果我手动在
selectionargs
中输入一个数字,例如1,它会起作用,但我一直试图通过代表每个条目的变量来实现。这不会导致错误,只是直接转到toast消息“无法删除”。ac.ID是指适配器类和列表项条目的ID

书签类:

public class Bookmark extends AppCompatActivity {

myAdapter myAdapter;
DBManager db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bookmark);
    db = new DBManager(this);

    onLoadAttraction();
   // onLoadTransport();


}

public void onLoadAttraction() {
    ArrayList<Adapter> listData = new ArrayList<Adapter>();
    listData.clear();
    Cursor cursor = db.Query("BookmarkAttraction",null, null, null, DBManager.ColId);

    if (cursor.moveToFirst()) {
        do {
            listData.add(new Adapter(
                    cursor.getString(cursor.getColumnIndex(DBManager.ColType))
                    , cursor.getString(cursor.getColumnIndex(DBManager.ColName))
                    , cursor.getString(cursor.getColumnIndex(DBManager.ColLocation))
                    , cursor.getString(cursor.getColumnIndex(DBManager.ColOpening))
                    , cursor.getString(cursor.getColumnIndex(DBManager.ColClosing))
                    , cursor.getString(cursor.getColumnIndex(DBManager.ColNearbyStop))
            ,null,null,null, null, null));
        } while (cursor.moveToNext());
    }

    myAdapter = new myAdapter(listData);
    ListView ls = (ListView) findViewById(R.id.listView);
    ls.setAdapter(myAdapter);
}

public void onLoadTransport(){
    ArrayList<Adapter> listData = new ArrayList<Adapter>();
    listData.clear();
    Cursor cursor = db.Query("BookmarkTransport",null, null, null, DBManager.ColId);

    if (cursor.moveToFirst()) {
        do {
            listData.add(new Adapter(
                    cursor.getString(cursor.getColumnIndex(DBManager.ColType))
                    , cursor.getString(cursor.getColumnIndex(DBManager.ColName))
                    , cursor.getString(cursor.getColumnIndex(DBManager.ColLocation))
                    , null
                    , null
                    , null
                    ,cursor.getString(cursor.getColumnIndex(DBManager.ColTime))
                    ,cursor.getString(cursor.getColumnIndex(DBManager.ColNextStop))
                    , cursor.getString(cursor.getColumnIndex(DBManager.ColPhoneNumber))
                    ,null,null));
        } while (cursor.moveToNext());
    }

    myAdapter = new myAdapter(listData);
    ListView ls = (ListView) findViewById(R.id.listView);
    ls.setAdapter(myAdapter);
}

  class myAdapter extends BaseAdapter {
    public ArrayList<Adapter> listItem;
    Adapter ac;
    public myAdapter(ArrayList<Adapter> listItem) {
        this.listItem = listItem;

    }

    @Override
    public int getCount() {
        return listItem.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        LayoutInflater myInflator = getLayoutInflater();
        final View myView = myInflator.inflate(R.layout.list_bookmark, null);

       ac = listItem.get(position);


        TextView Type = (TextView) myView.findViewById(R.id.BMType);
        Type.setText(ac.Type);

        TextView Name = (TextView) myView.findViewById(R.id.BMName);
        Name.setText(ac.Name);

        TextView Location = (TextView) myView.findViewById(R.id.BMLocation);
        Location.setText(ac.Location);

        TextView Opening = (TextView) myView.findViewById(R.id.BMOpen);
        Opening.setText(ac.Opening);


        TextView Closing = (TextView) myView.findViewById(R.id.BMClose);


            Closing.setText(ac.Closing);


        TextView NearbyStop1 = (TextView) myView.findViewById(R.id.BMNearbyStop);

            NearbyStop1.setText(ac.NearbyStop);


        Button buttonDelete = (Button)myView.findViewById(R.id.buttonDelete);
        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] selectionArgs = {ac.ID};
                int count = db.Delete("BookmarkAttraction","ID=? ",selectionArgs);
                if (count > 0) {
                    onLoadAttraction();
                    Toast.makeText(getApplicationContext(),"Data deleted", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(),"Cannnot delete", Toast.LENGTH_LONG).show();
                }

            }
        });



        return myView;
    }
}
}
}

适配器类:

 public class Adapter {

public String ID;
public String Type;
public String Name;
public String Location;
public String Opening;
public String Closing;
public String NearbyStop;
public String Time;
public String NextStop;
public String PhoneNumber;
public String Latitude;
public String Longitude;

public Adapter(String type, String name, String location, String opening, String closing,
               String nearbyStop, String time, String nextStop, String phoneNumber, String Latitude,
               String Longitude) {
    this.Type = type;
    this.Name = name;
    this.Location = location;
    this.Opening = opening;
    this.Closing = closing;
    this.NearbyStop = nearbyStop;
    this.Time = time;
    this.NextStop = nextStop;
    this.PhoneNumber = phoneNumber;
    this.Latitude = Latitude;
    this.Longitude = Longitude;
}
}

delete按钮的代码在mybookmark类中,DBManager持有实际的删除代码。如果有人能提供帮助,我们将不胜感激。

删除
myAdapter
类的
ac
字段,并将其用作
getView
方法中的方法变量


然后使用
ListView.setOnItemClickListener
方法添加具有位置感知的点击式侦听器。(在设置适配器后只执行一次)通过这种方式,您可以执行
listItem.get(position)
将项目置于该位置 public class Adapter { public String ID; public String Type; public String Name; public String Location; public String Opening; public String Closing; public String NearbyStop; public String Time; public String NextStop; public String PhoneNumber; public String Latitude; public String Longitude; public Adapter(String type, String name, String location, String opening, String closing, String nearbyStop, String time, String nextStop, String phoneNumber, String Latitude, String Longitude) { this.Type = type; this.Name = name; this.Location = location; this.Opening = opening; this.Closing = closing; this.NearbyStop = nearbyStop; this.Time = time; this.NextStop = nextStop; this.PhoneNumber = phoneNumber; this.Latitude = Latitude; this.Longitude = Longitude; } }