简体   繁体   中英

ListView update after any change

I have a listview in my android app which does two functions with the listview itself.

Fist, I can delete an item from the listview.

Second, I can rename something in the listview.

Anytime any of the above happens,

How do I display a different XML layout if the last item on the listview is deleted?

adapter = new SetRowsCustomAdapter(getActivity(), R.layout.customlist, rowsArray);
dataList = (ListView) mFrame3.findViewById(R.id.lvFiles); //lvfiles from xml layout
dataList.setAdapter(adapter);
dataList.setEmptyView(noFilesDisplayed); //not working

Second, once I rename anything within the listview, how do I update the listview to reflect the changes?

adapter.notifyDataSetChanged(); //not working

I have two choices in my ContextMenu, Delete and Rename .

If Delete is chosen, the following code executes:

if (menuItemIndex == 0) {
    if (folder.exists()) {
        //File flEachFile = new File(folder.toString() + "/" + currentFileName + ".tp");
        flEachFile = new File(folder.toString() + "/" + txt + ".tp");
        flEachFile2 = new File(folder.toString() + "/." + txt + ".tp");
        if (flEachFile.exists()) {
            flEachFile.delete();
        }
        if (flEachFile2.exists()) {
            flEachFile2.delete();
        }
        adapter.remove(adapter.getItem(info.position)); //updated the list
        //adapter.notifyDataSetChanged();
        dataList.invalidate();
        dataList.setEmptyView(noFilesDisplayed);//should display the noFilesDisplayed layout but it's not.
        //getActivity().getActionBar().setSelectedNavigationItem(1);
    }
}

This works fine as in the list updates itself as I delete a list. So if I have two lists in the view and I delete one the list updates to show one:

adapter.remove(adapter.getItem(info.position)); //updated the list

But if I delete the last item on the list and I want to display another xml layout as in this line:

dataList.setEmptyView(noFilesDisplayed);//should display the noFilesDisplayed layout but it's not.

It doesn't work.

======================================================

If Rename is chosen the following code executes:

if (menuItemIndex == 1) {
            // custom dialog
            final Dialog dialog = new Dialog(getActivity());
            dialog.setContentView(R.layout.renamefile);
            dialog.setTitle("Rename Existing Trip");

            currTrip = (TextView) dialog.findViewById(R.id.tvCurrentFileName);
            currTrip.setText(txt);

            etRT = (EditText) dialog.findViewById(R.id.etRenameTo);
            Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
            Button btnApply = (Button) dialog.findViewById(R.id.btnApply);
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

            btnApply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(folder.exists()) {
                        flEachFile = new File(folder.toString() + "/" + currTrip.getText().toString() + ".tp");
                        flEachFile2 = new File(folder.toString() + "/." + currTrip.getText().toString() + ".tp");
                        if (flEachFile.exists()) {
                            if (etRT.getText().toString().matches("")) {
                                rTo = (TextView) dialog.findViewById(R.id.tvRenameTo);
                                rTo.setTextColor(Color.parseColor("#A60000"));
                            }
                            else {
                                File toFile = new File(folder.toString() + "/" + etRT.getText().toString() + ".tp");
                                flEachFile.renameTo(toFile);
                                if (flEachFile2.exists()) {
                                    File toFile2 = new File(folder.toString() + "/." + etRT.getText().toString() + ".tp");
                                    flEachFile2.renameTo(toFile2);
                                }
                                //adapter.notifyDataSetChanged();
                                dataList.invalidate();
                                dialog.dismiss();
                                //getActivity().getActionBar().setSelectedNavigationItem(1);
                            }
                        }
                        else {
                            Toast.makeText(getActivity(), "File does not exist", Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            });
            // if cancel is clicked, close the custom dialog
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }

I can only see the changes if I switch tab and come back to this tab.

It's best to interact with a ListView through its associated adapter, in this case your SetRowCustomAdapter . To remove items you should do adapter.remove(position) , which will automatically handle the notifyDataSetChanged() function and internally remove it from whatever data structure it's in. As for renaming something, it depends on how your modeled the data behind the scenes in your adapter. Could you provide some more code for your customer adapter? Are you using an array internally?

UPDATE

Regarding setEmptyView(noFilesDisplayed) not working, is noFilesDisplayed in the same the layout hierarchy where the ListView is? In my experience, which has also been verified by other people, the empty view (in this case noFilesDisplayed ) must be in the same layout XML file and, more specifically, must be in the same hierarchy. I personally don't know why it has to be this way, but it seems like that's the only way it works. Here's the example of what I mean...

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<View
    android:id="@+id/empty_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone" />

Then in code, you can point noFilesDisplayed to the resource ID empty_view . I'm not entirely sure if setting the visibility to gone is necessary, try playing around with that. I hope that answers your question about the empty view.

call adpater.notifyDataSetChanged(); after you have made changes to your ListView, if notifyDataChanged() is still not working, you can set the adapter and pass the new updated data to the adapter constructor in onResume method. So whenever you delete/rename data in your listView, it will be updated automatically. (This second method will work only if you are starting a new activity for deleting or renaming listView items.)

@Override
protected void onResume() {
    super.onResume();

       SetRowsCustomAdapter adapter = new SetRowsCustomAdapter(getActivity(), R.layout.customlist, rowsArray);
       dataList.setAdapter(adapter);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM