ArrayList 中每个对象的删除按钮

Delete button for each object in ArrayList

我需要从 SQLite 数据库中获取对象并在 ListView 中显示它们。此外,每个对象在视图中必须有 2 个按钮:编辑和删除。这是适配器的代码:

public class ReminderListAdapter extends ArrayAdapter<Reminder> {
private Context mContext;
private int mResource;


public ReminderListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Reminder> objects) {
    super(context, resource, objects);
    mContext = context;
    mResource = resource;

}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    final int id = getItem(position).getId();
    final String name = getItem(position).getName();
    String hour = getItem(position).getHour();
    String date = getItem(position).getDate();

    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource, parent, false);

    final TextView resId = convertView.findViewById(R.id.textId);
    final TextView resName = convertView.findViewById(R.id.textName);
    final TextView resHour = convertView.findViewById(R.id.textHour);
    final TextView resDate = convertView.findViewById(R.id.textDate);

    resId.setText(String.valueOf(id));
    resName.setText(name);
    resHour.setText(hour);
    resDate.setText(date);

    Button delbtn = (Button) convertView.findViewById(R.id.button4);
    Button editbtn = (Button) convertView.findViewById(R.id.button5); 


   delbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


   editbtn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {

       }
   });
    return convertView;
}

/*public boolean delete_Reminder(String name2){
    return database.delete(TABLE_REMINDERS, KEY_NAME + "=" + name2, null) > 0;
}*/}

这是 activity 的代码,我需要在其中 post 对象:

public class EditActivity extends AppCompatActivity {

DBHelper dbHelper;
SQLiteDatabase database;
private ListView listView;
ArrayList<Reminder> ReminderList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit);
    dbHelper = new DBHelper(this);
    database = dbHelper.getWritableDatabase();
    listView =(ListView) findViewById(R.id.ListOfReminders);


    Cursor cursor = database.query(TABLE_REMINDERS, null, null,null,null,null,null);
    if(cursor.moveToFirst())
    {

        do{
            int idIndex = cursor.getInt(cursor.getColumnIndex(DBHelper.KEY_ID));
            String nameIndex = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_NAME));
            String hourIndex = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_HOUR));
            String dateIndex = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_DATE));
            String name = nameIndex;
            String hour = hourIndex;
            String date = dateIndex;

            ReminderList.add(new Reminder(idIndex, name, hour, date));
            ReminderListAdapter adapter = new ReminderListAdapter(this, R.layout.reminder_view, ReminderList);
            listView.setAdapter(adapter);

        } while(cursor.moveToNext());
    } else
        Log.d("mLog", "0 rows in db");


    dbHelper.close();

}

public boolean delete_Reminder(String name2){
    return database.delete(TABLE_REMINDERS, KEY_NAME + "=" + name2, null) > 0;
}}

在 reminder_layout 我有这些按钮。

我想我需要在我的适配器中实现这两个按钮。在代码中可以看到delbtn和editbtn的onClickListener。但是我的问题是我不知道如何调用方法delete_Reminder(也在Adapter中),因为我不能在这里创建DBHelper,也不能连接到DB。对不起,如果这是愚蠢的问题,这是第一个应用程序。

使用 interface 进行回调。 Activity 和 Adapter 之间的桥接接口;一旦用户对 Adapter 的视图执行操作 - 回调 Activity.

界面

interface OnItemClickListener {
  void onItemClick(View view, int position, String name)
}

Activity

public class EditActivity extends AppCompatActivity implements
    OnItemClickListener {
 ...

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 ...
 ReminderListAdapter adapter = new ReminderListAdapter(this, R.layout.reminder_view, ReminderList);
 adapter.setListener(this);
 listView.setAdapter(adapter);
 ...
 }


 @Override
   public void onItemClick(final View view, final int position, final String name) {
    //Your callback
    delete_Reminder(name);
  }
}

适配器

public class ReminderListAdapter extends ArrayAdapter<Reminder> {
private Context mContext;
private int mResource;

  private final OnItemClickListener listener;

  public ReminderListAdapter(@NonNull Context context, int resource, 
  @NonNull ArrayList<Reminder> objects) {
  super(context, resource, objects);
  mContext = context;
  mResource = resource;
  }


 public setListener(final OnItemClickListener listener) {
   this.listener = listener;
 }
 ...

 @NonNull
 @Override
 public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  ...

  delbtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      listener.onItemClick(convertView, position, name);
    }
  });
  }

}
  1. 您在每次循环迭代时设置适配器,而它应该只在循环结束后设置一次。

  2. 我可以在您的代码中看到几个设计问题(如果这是您的应用程序,这是可以理解的。

    • ReminderList 以大写字母开头 - 按照惯例变量和方法名称以小写字母开头,常量名称全部大写并且 class 名称以大写字母开头
    • 我不确定你的应用程序要去哪里,但是从数据库加载数据时,你可能需要考虑将它们加载到 CursorAdapter 而不是 ArrayAdapter
    • 另请查看 Data Binding library 和 MVC 设计以获得更简洁的代码。