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

Android-删除视图时屏幕不更新,android,android-layout,android-view,Android,Android Layout,Android View,这是我的第一个Android应用程序,让我的手湿了。该应用程序的唯一目的是制作和存储列表。当我创建一个新列表并将其添加到屏幕时,我遇到了屏幕无法正确更新自身的问题。为了尽可能多地了解视图和布局,我忽略了所有主题。无论如何,我有一个带有自定义按钮的线性布局,它本质上是另一个线性布局。当你第一次启动应用程序时,它看起来像这样!(我需要10个代表来发布图片,所以现在你可以得到这个) WideButton类是我制作的一个udf类。它扩展了LinearLayout并包含textview和imageview

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

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

这就是问题实际发生的地方。当用户单击并进入actionBar.addNewList()时。仅供参考ActionBar是我定制的类,尽管API提供了类似的特性。ActionBar是当前正在执行的类的私有类。操作栏实现View.onCLickListener。下面是addNewList()函数。此外,llMain是一个线性布局

/**
    * 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();

我还完成了我所能想到的这些命令的每一个组合,它们看起来至少有点合乎逻辑。如果您需要查看更多代码,请询问。我只是觉得现在在这里发布我的整个应用程序可能不合适。

这不是填充数据屏幕的方式。我不是想说你不礼貌,但你的基本方法在这里是错误的。从与数据模型交互到直接操纵视图层次结构,基本上都是在
活动本身中进行的

这不是安卓的做事方式。您需要的是一个
适配器
——数据和使用它的视图之间的中间组件

您需要(可选地)从
ListActivity
(它将附带自己的
ListView
)扩展到
ListAdapter
(最有可能是
SimpleCursorAdapter
,因为您正在从数据库获取数据)到您通过布局文件显式提供的
ListView
,或者在扩展时使用
ListActivity
提供的默认值

只有
适配器
会与数据模型类交互。当基础数据
列表
更改时,调用
适配器#notifyDataSetChanged()
将自动刷新
列表视图

看一下教程,了解基本知识。如果您现在觉得
CursorAdapter
对您来说有点复杂,请首先使用
ArrayAdapter
。这是一个很好的例子


不要害怕看到这么多不同的班级在流动。以后会变得更容易。

在createNoListView中,我将id设置为1,当数据库中没有任何内容时,如何创建视图?我在为这个应用程序做研究时发现了这个选项,但由于这个原因没有实现它。我认为一个直接的答案是使用框架视图,但也许你有一个更好的建议。实际上这很简单。只需调用
ListView#setEmptyView()
并传入一个用适当消息初始化的
TextView
。如果支持
ListAdapter
的数据
List
为空,则在第一次布局过程(如果
List
开始时为空)或在适配器上调用
notifyDataSetChanged()
后(清除列表),您的
TextView
将自动显示给用户。很抱歉,此答案迟迟未被接受。我已经有一点没法做这件事了。谢谢你的帮助。哦,没问题。很高兴我的回答有帮助。
@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);
    }
}
/**
* 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);
}
/**
    * 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();