从listView中删除项目仅删除最后一项未选择的元素android

Posted

技术标签:

【中文标题】从listView中删除项目仅删除最后一项未选择的元素android【英文标题】:Deleting item from listView only delete last item not selected element android 【发布时间】:2021-08-24 18:55:04 【问题描述】:

我有一个列表视图,我可以根据单击的按钮填充列表视图并显示适当的布局。一切正常。

但是我现在要做的是通过警报窗口从列表视图中删除选择元素:

ArrayList.remove(position);

ArrayListAdapter.notifyDataSetChanged();

这似乎应该起作用,我在谷歌搜索时看到了其他建议,认为这是应该调用的,但不是删除选定的元素,而是总是从 ListView 中删除最后一个元素,我不知道为什么?

我唯一的想法是我需要在扩展 baseAdapter 的 ArrayListAdapter 中的 getView() 方法中处理更多内容,但我终生无法找到/弄清楚这可能是什么?

我欢迎任何建议或想法?

这是我的代码:


import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;

public class ChatRoomActivity extends AppCompatActivity 

    private ArrayList<Message> messageArrayList = new ArrayList<>(Arrays.asList());
    private ArrayListAdapter messageArrayListAdapter;
    private Message storeMsgButtonPressedObj;


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


        ListView listView = findViewById(R.id.messageListView);
        EditText chatMsgEditTextView = findViewById(R.id.msgBox);
        listView.setAdapter(messageArrayListAdapter = new ArrayListAdapter());

        Button sendButton = findViewById(R.id.sendBtn);
        Button receivedButton = findViewById(R.id.receivedBtn);


        sendButton.setOnClickListener(Click -> 

            storeMsgButtonPressedObj = new Message(chatMsgEditTextView.getText().toString(), 1);
            messageArrayList.add(storeMsgButtonPressedObj);
            messageArrayListAdapter.notifyDataSetChanged();
            chatMsgEditTextView.getText().clear();

        );

        receivedButton.setOnClickListener(Click -> 
            storeMsgButtonPressedObj = new Message(chatMsgEditTextView.getText().toString(), 2);
            messageArrayList.add(storeMsgButtonPressedObj);
            messageArrayListAdapter.notifyDataSetChanged();
            chatMsgEditTextView.getText().clear();

        );


        listView.setOnItemLongClickListener( (AdapterView, View, indexOfViewElement, databaseID) -> 

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

            alertDialogBuilder.setTitle("Do you want to delete this?");


            alertDialogBuilder.setMessage(
                    "The position of your item is: " + indexOfViewElement + "\n" 
                  + "The database id id: " + databaseID
            );


            alertDialogBuilder.setPositiveButton("Yes", (click, arg) -> 

                messageArrayList.remove(indexOfViewElement);
                messageArrayListAdapter.notifyDataSetChanged();

            );

            alertDialogBuilder.setNegativeButton("No", (click, arg) ->  );

            alertDialogBuilder.create().show();
            return true;
        );



    
    // onCreate() END*****


    private class ArrayListAdapter extends BaseAdapter 

        //gets current size of array list
        @Override
        public int getCount()
            return messageArrayList.size();
        

        @Override
        public Object getItem(int indexValueOfElement)
            return storeMsgButtonPressedObj.getMsgValue()  /* + indexValueOfElement*/;
        

        @Override
        public long getItemId(int indexValueOfElement)
            return (long) indexValueOfElement;
        

        @Override
        public View getView(int indexValueOfElement, View currentView, ViewGroup parent)

            View updateView = currentView;
            LayoutInflater inflater = getLayoutInflater();


            if(updateView == null) 

                if(storeMsgButtonPressedObj.getButtonPressed() == 1) 

                    updateView = inflater.inflate(R.layout.row_sent, parent, false);

                    TextView tView = updateView.findViewById(R.id.testViewSent);

                    tView.setText( getItem(indexValueOfElement).toString() );
                

                if(storeMsgButtonPressedObj.getButtonPressed() == 2)

                    updateView = inflater.inflate(R.layout.row_received, parent, false);

                    TextView tView = updateView.findViewById(R.id.textViewReceived);

                    tView.setText( getItem(indexValueOfElement).toString() );
                
            


            //return the updated view object
            return updateView;
        
    




这是我编写的消息类,用于存储 String 和一个 int,以确定按下了哪个按钮,以便可以填充正确的布局。


public class Message 

private String msgValue;
private int buttonPressed;

   Message()

   

   Message(String msg, int buttonPressed)

       this.msgValue = msg;
       this.buttonPressed = buttonPressed;
   

    public String getMsgValue() 
        return msgValue;
    

    public int getButtonPressed() 
        return buttonPressed;
    

    public void setMsgValue(String msgValue) 
        this.msgValue = msgValue;
    

    public void setButtonPressed(int sendReceive) 
        this.buttonPressed = sendReceive;
    



【问题讨论】:

【参考方案1】:

请尝试下面的适配器类。

我已经更新了 getItemgetView 一点点。 正如我所看到的,您的getItemgetView 直接使用storeMsgButtonPressedObj,它根据您的实现保存最后一个发送或接收消息,它不应该用于创建视图或getItem,而是它应该从保存这些值的 List 中提取出来。

private class ArrayListAdapter extends BaseAdapter 

//gets current size of array list
@Override
public int getCount() 
    return messageArrayList.size();


@Override
public Object getItem(int indexValueOfElement) 
    return messageArrayList.get(indexValueOfElement);


@Override
public long getItemId(int indexValueOfElement) 
    return (long) indexValueOfElement;


@Override
public View getView(int indexValueOfElement, View currentView, ViewGroup parent) 

    View updateView = currentView;
    LayoutInflater inflater = getLayoutInflater();


    if (updateView == null) 

        if (messageArrayList.get(indexValueOfElement).getButtonPressed() == 1) 

            updateView = inflater.inflate(R.layout.row_sent, parent, false);

            TextView tView = updateView.findViewById(R.id.testViewSent);

            tView.setText(getItem(indexValueOfElement).toString());
        

        if (messageArrayList.get(indexValueOfElement).getButtonPressed() == 2) 

            updateView = inflater.inflate(R.layout.row_received, parent, false);

            TextView tView = updateView.findViewById(R.id.textViewReceived);

            tView.setText(getItem(indexValueOfElement).toString());
        
    


    //return the updated view object
    return updateView;


【讨论】:

以上是关于从listView中删除项目仅删除最后一项未选择的元素android的主要内容,如果未能解决你的问题,请参考以下文章

Spannable String 仅适用于 ListView 中的最后一项

android从listview中删除任何项目删除最后一个元素

我想从列表视图中删除一个项目

如何仅操作 ListView.builder 的一个元素

使用自定义arrayadapter从listview android中删除复选框选择的多个项目

CMFCPropertyGridCtrl 最后一项未绘制?