如何连接删除按钮以删除列表视图中的一个项目?

时间:2016-08-09 01:28:51

标签: android android-layout listview arraylist

我知道有很多主题或多或少相同的主题,但没有一个涵盖我的情况:

我正在尝试使用删除按钮删除列表视图中的一行,并且删除按钮出现在每个行中,但我无法使用此部分,但我无法在主要活动中使用它。每次我将这部分放在我的代码中时,代码都会破坏:

ImageButton removeButton = (ImageButton) findViewById(R.id.btnDel);
removeButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onRemoveButtonClick();
    }
    private void  onRemoveButtonClick() {
        ToDoItem item = (ToDoItem) v.getTag();
        notifyDataSetChanged();
}

我的MainActivity没有这段代码就可以正常工作。我不知道这是代码还是我把它放在我的MainActivity中,如果有人愿意告诉我会非常感激。

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG = "ToDoApp";
    private ToDoListManager listManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView todoList = (ListView) findViewById(R.id.todo_list);
        listManager = new ToDoListManager(getApplicationContext());
        ToDoItemAdapter adapter = new ToDoItemAdapter(this, listManager.getList());


        todoList.setAdapter(adapter);
        ImageButton addButton = (ImageButton) findViewById(R.id.add_item);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAddButtonClick();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        listManager.saveList();
    }

    private void onAddButtonClick() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.add_item);

        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

        builder.setPositiveButton(
            R.string.ok,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //Add item to list
                    ToDoItem item = new ToDoItem(
                            input.getText().toString(),
                            false
                    );
                    listManager.addItem(item);
                    Log.i(LOG_TAG, input.getText().toString());
                }
            });
        builder.setNegativeButton(
            R.string.cancel,
            new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int Which) {
                    dialog.cancel();
                }
            });

        builder.show();
    }

    private class ToDoItemAdapter extends ArrayAdapter<ToDoItem> {

        private  Context context;
        private List<ToDoItem> items;
        public ToDoItemAdapter(Context context, List<ToDoItem> items){
            super(context,-1,  items);

            this.context = context;
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.to_do_item_layout, parent, false);
            }

            TextView textView = (TextView) convertView.findViewById(R.id.item);
            CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);

            textView.setText(items.get(position).getDescription());
            checkBox.setChecked(items.get(position).isComplete());

            convertView.setTag(items.get(position));
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToDoItem item = (ToDoItem) v.getTag();
                    item.toggleComplete();
                    notifyDataSetChanged();
                }
            });
            return convertView;
        }
    }
}

ToDoListManager.java

public class ToDoListManager {


private static final String APP_PREFERENCES = "todoapp";
private static final String TODO_ITEMS = "itemslist";

private List<ToDoItem> items;
private SharedPreferences savedData;

public  ToDoListManager(Context context) {

     savedData = context.getSharedPreferences (
        APP_PREFERENCES,
        Context.MODE_PRIVATE
    );

    String json = savedData.getString(TODO_ITEMS, null);
    if (json == null) {
    items = new ArrayList<>();
  } else {

 Type type = new TypeToken<List<ToDoItem>>() {}.getType();
 items = new Gson().fromJson(json, type);
}


}
 public List<ToDoItem> getList() {
 return items;
 }
 public void  addItem(ToDoItem item) {
    items.add(item);
    saveList();

 }
 public  void saveList() {
    SharedPreferences.Editor edit =savedData.edit();
    edit.clear();
    String json = new Gson().toJson(items);

    edit.putString(TODO_ITEMS, json);
    edit.apply();
 }

}

ToDoItem.java

public class ToDoItem {
private String description;
private boolean isComplete;

public ToDoItem (String description,boolean isComplete) {
    this.description = description;
    this.isComplete = isComplete;
}

public String getDescription() {
    return  description;
}
public boolean isComplete() {
    return isComplete;
}
public void toggleComplete() {
    isComplete = !isComplete;
}
@Override
public String toString() {
    return description;
}
}

enter image description here

1 个答案:

答案 0 :(得分:1)

1。)首先在to_do_item_layout中添加一个按钮

2。)现在添加此代码,为每个项目添加一个按钮

Button btn = (Button) convertView.findViewById(R.id.my_btn);

3.)添加一个监听器

btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         items.remove(items.get(position));         
      }
});