从列表中删除项目后刷新arraylist

时间:2015-09-08 09:15:16

标签: android arraylist android-linearlayout horizontalscrollview

我在水平ScrollView中使用线性布局来实现水平幻灯片视图。这里当我从自定义arraylist中删除特定项目时,它不会在arraylist中更新。这是我的代码供您参考:

protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.send_enquiry);

    sharedpreferences = getSharedPreferences("MyPrefs",
            Context.MODE_PRIVATE);


    message_editText = (EditText) findViewById(R.id.message_editText);
    close_button = (ImageView) findViewById(R.id.close_button);
    senders_list_row_layout = (LinearLayout)   findViewById(R.id.senders_list_row_layout);
    messageSize_textView = (TextView) findViewById(R.id.messageSize_textView);
    recepients_horizontalScrollView = (HorizontalScrollView) findViewById(R.id.recepients_horizontalScrollView);
    attachmentIcon_imageView = (ImageView) findViewById(R.id.attachmentIcon_imageView);
    send_button = (Button) findViewById(R.id.send_button);
    recepients_relLayout = (RelativeLayout) findViewById(R.id.recepients_relLayout);
    successMessage_textView = (TextView) findViewById(R.id.successMessage_textView);
    creditsEarned_textView = (TextView) findViewById(R.id.creditsEarned_textView);
    messageSent_imageView = (ImageView) findViewById(R.id.messageSent_imageView);


private void sendersList() {

    for (int i = 0; i < sendEnquiry.size(); i++) {

        senders_list_row_layout.addView(viewSendersList(sendEnquiry.get(i)
                .getCpsName(), i));
    }

}


private View viewSendersList(String cpsName, final int position) {

    LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    final View view = mInflater.inflate(R.layout.send_enquiry_repeat_items,
            null);

    final TextView sendersName = (TextView) view
            .findViewById(R.id.sendersName);
    ImageView closeButton_imageView = (ImageView) view
            .findViewById(R.id.closeButton_imageView);

    sendersName.setText(cpsName);
closeButton_imageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(Q2_SendEnquiryActivity.this,
                    "close button clicked: " + position, Toast.LENGTH_SHORT)
                    .show();
            if (sendEnquiry.size() > 1) {

                    sendEnquiry.remove(position);
                    senders_list_row_layout.removeViewAt(position);

                    Log.i("sendEnquiry list size",
                            "sendEnquiry list size: " + sendEnquiry.size());



            } else {
                sendEnquiry.clear();
                finish();
            }
        }
    });

    return view;
}

}

在这个closeButton_imageView.setOnClickListener中,我正在从自定义arrayList中删除一个项目。它第一次工作正常,但在此之后,项目的位置在从arraylist中删除后不会更新。它还会抛出StringIndexOutOfBoundsException。

有人可以帮助我从中删除项目后如何更新customArrayList吗?

1 个答案:

答案 0 :(得分:1)

嗨尝试删除像

这样的视图
     senders_list_row_layout.removeView(view);

private View viewSendersList(String cpsName,final int position){

LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View view = mInflater.inflate(R.layout.send_enquiry_repeat_items,
        null);

final TextView sendersName = (TextView) view
        .findViewById(R.id.sendersName);
ImageView closeButton_imageView = (ImageView) view
        .findViewById(R.id.closeButton_imageView);

sendersName.setText(cpsName);
closeButton_imageView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(Q2_SendEnquiryActivity.this,
                "close button clicked: " + position, Toast.LENGTH_SHORT)
                .show();
        if (sendEnquiry.size() > 1) {

                sendEnquiry.remove(position);
               //not like that
               //senders_list_row_layout.removeViewAt(position);

               //like that
                senders_list_row_layout.removeView(view);

                Log.i("sendEnquiry list size",
                        "sendEnquiry list size: " + sendEnquiry.size());



        } else {
            sendEnquiry.clear();
            finish();
        }
    }
});

return view;

}