在更新arrayAdapter / listView之前更新final List

时间:2016-04-14 05:33:24

标签: java android listview android-arrayadapter

我试图在用户删除条目后更新列表视图。我发现我需要使用notifyDataSetChanged()。但要使用它我需要先更新arrayList(正确吗?)。但我无法改变,因为该List被宣布为最终版。

这是最终的,因为我需要getView方法,否则Android Studio会抱怨无法通过内部类访问它。

相关代码:

        final List<String> arrayList = mydb.getAllAlarms();
        final List<String> timesList = mydb.getAllAlarmTimes();
        final ArrayList<Integer> arrayListID = mydb.getAllAlarmIDs();

        boolean isEmpty = false;
        if (arrayList.isEmpty()) { //default
            arrayList.add("You have no alarms");
            timesList.add("");
            isEmpty = true;
        }

        //initialise arrayadapter to show stuff in the listview
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_2, android.R.id.text1, arrayList) {
            public View getView(int position, View convertView, ViewGroup parent) {

                TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
                TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
                text1.setText(arrayList.get(position));
                text2.setText(timesList.get(position));
                return convertView;
            }
        };

        final ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(arrayAdapter); //set our custom adapter to the listview

        if (!isEmpty) { //only set clicklistener if there are alarms
            //set clicklistener for items in the listview
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                //...
            });
            listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                    //first, find the corresponding database id
                    final int idToDelete = arrayListID.get(position);

                    //show dialog
                    AlertDialog.Builder alert = new AlertDialog.Builder(
                            MainActivity.this);
                    alert.setTitle("Delete alarm");
                    alert.setMessage("Are you sure?");
                    alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mydb.deleteAlarm(idToDelete);
                            //cancel alarm as well
                            ShowAlarm.cancelAlarmIfExists(getApplicationContext(), idToDelete);

                            dialog.dismiss();

                        }
                    });

                    //...
                    alert.show();

                    //PROBLEM LIES HERE

                    //reload listview
//                    arrayList = mydb.getAllAlarms(); //not possible because final List

                    arrayAdapter.clear();
                    arrayAdapter.addAll(arrayList);

                    arrayAdapter.notifyDataSetChanged();
//                    listView.invalidateViews();
//                    listView.setAdapter(arrayAdapter);

                    return true;
                }
            });

        }

1 个答案:

答案 0 :(得分:1)

你可以使用envelop从你的arraylist中获取一个项目并执行操作以便正确反映