Android - 删除视图时屏幕不更新

时间:2015-01-13 02:12:39

标签: android android-layout android-view

这是我的第一款让我的手湿润的Android应用。该应用程序的唯一目的是制作和存储列表。当我创建一个新列表并将其添加到屏幕时,我遇到了屏幕无法正确更新的问题。为了尽可能多地学习视图和布局,我忽略了所有的主题。我有一个线性布局,自定义按钮本质上是另一种线性布局。当你第一次启动它时,应用程序看起来像这样! (我需要10个代表发布图片,所以现在你得到这个)

***********************************************
* List Of Lists                  |  +  | | * |*
*---------------------------------------------*
*                                             *
*         Start Making Lists                  *
*                 +                           *
*---------------------------------------------*
*                                             *
*                                             *

单击“开始制作列表”并在弹出窗口中输入一个字符串后,我遇到此问题,其中第一个按钮被删除但屏幕重绘不正确。

***********************************************
* List Of Lists                  |  +  | | * |*
*---------------------------------------------*
*                 Test Label                  *
*---------------------------------------------*
*                 +                           *
*---------------------------------------------*

如果您无法从我的ascii艺术中辨别出原始按钮中有一个左侧部分。你无法点击它,所以它的行为就像它已经消失,但我的加号图像和阴影背景仍然显示。

守则,或者至少我认为你需要看到的

@Override
public void onCreate(Bundle savedInstanceState)
{
    try{
        super.onCreate(savedInstanceState);

        init();

        /* Start DB Gather data */
        listData = new ListDB(this); //extends SQLiteOpenHelper
        Cursor listNames = listData.getListNames(0);

        if (listNames.getCount() > 0)
        {
            /* Regular processing */
            createStandardView(listNames);
        }
        else
        {
            /* No lists yet, special view for new users */
            createNoListView();
        }

        /* Draw the view to the screen */
        setContentView(screen);
    }catch(Exception e)
    {
        TextView errMsg = new TextView(this);
        String err = e.toString() + "\n";
        StackTraceElement[] stack = e.getStackTrace();

        for(int i = 0; i < stack.length; ++i)
        {
            err += stack[i].toString() + "\n";
        }
        errMsg.setText(err.subSequence(0, err.length()));
        setContentView(errMsg);
    }
}

问题种类在调用createNoListView()时出现,所以这里是代码

/**
* Creates the initial view the user will see when they first install the app / have no lists yet. This layout could
* have been and should be in a seperate xml file like any other static android layout
*/
public void createNoListView()
{
    /* Default Fresh Install add new list button view */
    final WideButton btnAddNew = new WideButton(this, R.string.btnAddText, R.drawable.add_new);
    btnAddNew.setId(1);
    btnAddNew.setImageDimensions(35,35);
    btnAddNew.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            actionBar.addNewList();
        }
    });

    llMain.addView(btnAddNew);
}

WideButton类是我制作的udf类。它扩展了LinearLayout并包含textview和imageview。如果有人认为这会有所帮助,我会非常乐意发表这个课程。

所以这就是麻烦实际发生的地方。当用户点击并进入actionBar.addNewList()时。 FYI ActionBar是我提供的自定义类,尽管API提供了类似的功能。 ActionBar是当前正在执行的类的私有类。操作栏实现View.onCLickListener。这是addNewList()函数。 llMain也是linearLayout

/**
    * Starts the process of creating a new list.
    * */
    public void addNewList()
    {
        LayoutInflater inflater = ((Activity)action_bar.getContext()).getLayoutInflater();

        final View vwDiag = inflater.inflate(R.layout.add_list_diag, null);
        AlertDialog.Builder alert = new AlertDialog.Builder(ListOfLists.this);

        alert.setView(vwDiag).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface d, int id)
            {
                /* Add the list to the DB*/
                EditText txtNameVal = (EditText) vwDiag.findViewById(R.id.txtNameVal);
                String listName = txtNameVal.getText().toString();

                //Display new item
                if(listData.getListCount() == 0)
                {
                    ((LinearLayout)ListOfLists.this.llMain.findViewById(1)).removeAllViews();
                    //ListOfLists.this.llMain.removeViewAt(0);
                    //ListOfLists.this.llMain.refreshDrawableState();
                    ListOfLists.this.llMain.removeAllViews();
                }

                listData.createList(listName);
                addListToView(listName, null, null);
            }
        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){public void onClick(DialogInterface d, int id){
        }}).create().show();
    }
}

正如你所看到的,我尝试了很多不同的东西。我尝试过的其他事情都是。

//first attempt if I remember correctly
View btnAddNew = ListOfLists.this.llMain.findViewById(1);
ListOfLists.this.llMain.removeView(btnAddNew);

//Another one I'm sure I've done
View btnAddNew = ListOfLists.this.llMain.findViewById(1);
ListOfLists.this.llMain.removeView(btnAddNew);
ListOfLists.this.llMain.invalidate();

我也完成了我能想到的这些命令的每一个组合,这似乎至少在某种程度上是合乎逻辑的。如果您需要查看更多代码,请询问。我只是觉得到目前为止发布我的整个应用程序可能是不合适的。

1 个答案:

答案 0 :(得分:0)

这不是填充数据屏幕的方法。我并不是说听起来不礼貌,但你的基本方法在这里是错误的。您基本上可以完成所有工作 - 从与数据模型交互到直接操作视图层次结构 - 来自Activity本身。

这不是Android的做事方式。您需要的是Adapter - 您的数据与使用它的视图之间的中间组件。

您需要(可选)从ListActivity扩展(它可以自带ListView)但绑定ListAdapter(很可能是SimpleCursorAdapter,因为您'从数据库中获取数据)到ListView,您可以通过 layout 文件明确提供,或者当您时使用ListActivity提供的默认值从中扩展

只有Adapter会与您的数据模型类进行交互。当基础数据List发生变化时,对Adapter#notifyDataSetChanged()的调用会自动为您刷新ListView

查看Android Developers' List View教程,了解基础知识。如果你觉得CursorAdapter现在对你来说有点复杂,那么首先要先使用ArrayAdapter。这是一个很好的ListView example

看到这么多不同的课程浮出水面,不要害怕。以后会变得容易。