单击listview中每个项目的侦听器

时间:2017-12-12 13:21:24

标签: android listview onclicklistener

您好我想听一下列表视图中每个项目的点击我有这个代码部分完成工作但是如果我有5个项目并且我有一个删除其中一个的方法它总是删除最后一个不是我点击的那个。我已经读过我应该使用 onClick 方法进行接口,该方法将 view position 纳入其构造函数中,但之后我会有一个监听器列表中的整个项目,我希望在这种情况下有4个监听器,这里是代码示例:

public class FlashCardsDecksArrayAdapter extends ArrayAdapter<FileManager> {
interface Callback {
    void onFileDeleted(String fileName);
}

private final Callback mCallback;

private FileManager manager;

public FlashCardsDecksArrayAdapter(Context context, ArrayList<FileManager> files, final Callback callback) {
    super(context, 0, files);
    mCallback = callback;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    manager = getItem(position);

    if (convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_item, parent, false);
    }


    TextView deckNameTextView = convertView.findViewById(R.id.deck_name_id);

    Button addCardButton = convertView.findViewById(R.id.action_add_card);

    Button deleteDeckButton = convertView.findViewById(R.id.action_delete);

    Button settingsButton = convertView.findViewById(R.id.action_deck_settings);

    deckNameTextView.setText(manager.getFileName());

    deckNameTextView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            if(manager.length() == 0){
                Toast toast = Toast.makeText(getContext(), "deck is empty", Toast.LENGTH_SHORT);
                toast.show();
            } else{
                Intent intent = new Intent(getContext(), FlashCardActivity.class);
                intent.putExtra("fileName", manager.getFileName());
                getContext().startActivity(intent);
            }
        }
    });

    addCardButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            Intent intent = new Intent(getContext(), AddCardActivity.class);
            intent.putExtra("fileName", manager.getFileName());
            getContext().startActivity(intent);
        }
    });

    deleteDeckButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buildAlertDialog();
        }
    });

    settingsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            if(manager.length() == 0){
                Toast toast = Toast.makeText(getContext(), "deck is empty", Toast.LENGTH_SHORT);
                toast.show();
            } else {
                Intent intent = new Intent(getContext(), FlashCardsDeckSettingsActivity.class);
                intent.putExtra("fileName", manager.getFileName());
                getContext().startActivity(intent);
            }
        }
    });

    return convertView;
}
private AlertDialog buildAlertDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();
    return builder.create();
}

DialogInterface.OnClickListener dialogClickListener = new 
DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                manager.deleteFile();
                mCallback.onFileDeleted(manager.getFileName());
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked
                break;
        }
    }
};
}

2 个答案:

答案 0 :(得分:0)

在删除项目的同时传递项目的位置

deleteDeckButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        buildAlertDialog(position);
    }
});

点击“肯定”按钮,从经理中删除该特定位置的项目。

DialogInterface.OnClickListener dialogClickListener = new 
DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                //Delete particular position item from manager
                manager.deleteFile(position);
                mCallback.onFileDeleted(manager.getFileName());
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked
                break;
        }
    }
};

答案 1 :(得分:0)

setTag(position)然后是int pos view.getTag()解决了问题