我无法写入 EditText,当我尝试写入内容时它会消失,因为当我修改数据时调用 getView()

发布于 2024-12-29 19:11:37 字数 3247 浏览 1 评论 0原文

编辑:

我发现原因是当我尝试时 getView() 被调用 编辑一些内容,以便加载来自 DataAdapter 的数据并进行编辑。我编辑的 更改消失。

编辑:

我观察到一件事,如果列表视图中的行很少,那么它的 好的,但是如果有很多行列表视图无法显示在 可见屏幕(滚动条出现滚动到其他记录),然后 问题出现了!!

我正在开发一个项目,我们已经实现了 使用ListView进行内联编辑,即可以在列表视图内编辑数据。

我为 ListView 的每个项目/行定义了一个 xml。我正在使用 Custom DataAdapter 将数据与 ListView 绑定。

当我第一次加载该活动时,ListView 已加载,我可以编辑数据和列表视图。效果很好。当编辑某些内容时,更改将保存到 SQLite 数据库中,我有一个用于此目的的按钮。

现在的问题是,在第一次保存数据后,列表视图再次加载,我无法再编辑数据。当我尝试编辑数据时,键盘会出现&然后自动消失&输入的数据也会消失。请查看屏幕截图。

有人可以帮我解决这个问题吗?

我的自定义适配器类:

public class QuestionAdapter extends ArrayAdapter<QuestionEntity> {
      private ArrayList<QuestionEntity> items;
      private Context CurrentContext;
      private QuestionEntity CurrentItem;
      private Cursor    OptionsCursor;


    public QuestionAdapter(Context context,  ArrayList<QuestionEntity> items, Cursor curOptions) 
    {
        super(context, R.layout.grid_item, items);
        this.CurrentContext = context;
        this.items          = items;
        this.OptionsCursor  = curOptions;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        //verify that the items list is still valid since
        //the list may have been cleared during an update
        if ((items == null) || ((position + 1) > items.size()))
                return convertView; //Can't extract item

        CurrentItem = items.get(position);    

        if(convertView == null) 
        {
            LayoutInflater inflater = LayoutInflater.from(CurrentContext);
            convertView = inflater.inflate(R.layout.grid_item, null);
        }

        if (convertView != null) 
        {

            TextView txtQuestion = (TextView) convertView.findViewById(R.id.txtQuestion);
            txtQuestion.setText(CurrentItem.getTitle());

            Spinner cmbOptions = (Spinner)convertView.findViewById(R.id.cmbOptions);

            /*
             * Load the options from OptionsCursor
             */

            LoadOptions(cmbOptions);

            /*
             * Attach onItemClick event with cmbOptions 
             * When the user change the option we will populate the comments based on the option
             */

            cmbOptions.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
            {
                try
                {
                    //If MyCondition is true show msg to user.

                }
                catch(Exception ex)
                {
                    ex.toString();
                }

            }
            });

        }
        return convertView;

    }

    private void LoadOptions(Spinner iacOptions)
    {
        //Load data in the spinner using the OptionsCursor

    }

}

在此处输入图像描述

EDIT:

I found the reason which is that the getView() is called when i try to
edit something, so the data from the DataAdapter is loaded & my edited
changes disappears.

EDIT:

i observed one thing, if there are few rows in the listview then its
OK, but if there are many rows which the listview can not show in the
visible screen (Scroll bar appears to scroll to other records), then
the issue arises!!

I am working on project where we have implemented an INLINE EDITING using ListView, i.e. the data can be edited inside the listview.

I have a defined an xml for each item/row of that ListView. I am using Custom DataAdapter to bind the data with ListView.

When i first time load that activity the ListView is loaded, i can edit the data & it works fine. When something is edited the changes are saved to the SQLite database, i have a button for this purpose.

Now the issue is that after the data is saved FOR THE VERY FIRST TIME & the listview is loaded again, i can not edit the data anymore. When i try to edit the data the keyboard appears & then disappears automatically & the ENTERED DATA also disappears. Please see the screen shots.

Can some one help me to resolve this issue?

my Custom Adapter class:

public class QuestionAdapter extends ArrayAdapter<QuestionEntity> {
      private ArrayList<QuestionEntity> items;
      private Context CurrentContext;
      private QuestionEntity CurrentItem;
      private Cursor    OptionsCursor;


    public QuestionAdapter(Context context,  ArrayList<QuestionEntity> items, Cursor curOptions) 
    {
        super(context, R.layout.grid_item, items);
        this.CurrentContext = context;
        this.items          = items;
        this.OptionsCursor  = curOptions;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        //verify that the items list is still valid since
        //the list may have been cleared during an update
        if ((items == null) || ((position + 1) > items.size()))
                return convertView; //Can't extract item

        CurrentItem = items.get(position);    

        if(convertView == null) 
        {
            LayoutInflater inflater = LayoutInflater.from(CurrentContext);
            convertView = inflater.inflate(R.layout.grid_item, null);
        }

        if (convertView != null) 
        {

            TextView txtQuestion = (TextView) convertView.findViewById(R.id.txtQuestion);
            txtQuestion.setText(CurrentItem.getTitle());

            Spinner cmbOptions = (Spinner)convertView.findViewById(R.id.cmbOptions);

            /*
             * Load the options from OptionsCursor
             */

            LoadOptions(cmbOptions);

            /*
             * Attach onItemClick event with cmbOptions 
             * When the user change the option we will populate the comments based on the option
             */

            cmbOptions.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
            {
                try
                {
                    //If MyCondition is true show msg to user.

                }
                catch(Exception ex)
                {
                    ex.toString();
                }

            }
            });

        }
        return convertView;

    }

    private void LoadOptions(Spinner iacOptions)
    {
        //Load data in the spinner using the OptionsCursor

    }

}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵性 2025-01-05 19:11:37

尝试修改您的代码并查看是否 Adapter.getView(..) 方法在不应该调用的时候被调用。这可能是由于冗余调用 notifyDataSetChanged( )

只需向这些方法添加日志记录,看看它们是否在正确的地点和时间被调用。

Try to revise your code and see if Adapter.getView(..) method is called when it shouldn't. This could happen because of redundant call of notifyDataSetChanged().

Just add logging to these methods and see if they are called at the right place and time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文