【问题标题】:I can't write into the EditText, it disappears when i try to write something, its because the getView() is called when i modify the data我无法写入 EditText,当我尝试写东西时它消失了,这是因为当我修改数据时调用了 getView()
【发布时间】:2012-02-23 07:11:18
【问题描述】:

编辑:

我找到了当我尝试调用 getView() 的原因 编辑一些东西,所以来自 DataAdapter 的数据被加载 & 我编辑 更改消失。

编辑:

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

我正在做一个我们已经实现INLINE EDITING using ListView 的项目,即可以在列表视图中编辑数据。

我为该 ListView 的每个项目/行定义了一个 xml。我正在使用自定义 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

    }

}

【问题讨论】:

标签: android listview android-edittext android-softkeyboard inline-editing


【解决方案1】:

尝试修改您的代码,看看是否在不应该调用Adapter.getView(..) 方法时调用。这可能是由于对notifyDataSetChanged() 的冗余调用而发生的。

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

【讨论】:

  • 根据您的问题更新,很明显问题出在您对Adapter.getView(..) 的实现中。您在编辑视图时使用存储的数据刷新视图。如果在调用Adapter.getView(..) 时正在输入EditText,则应检查此方法,如果是,则不要刷新它。
  • 你是正确的问题是当我尝试修改数据时调用了 Adapter.getView() 。你能帮我解决这个问题吗?谢谢
  • 然后发布您的代码。 Adapter 的实现和调用notifyDataSetChanged()(或notifyDataSetInvalidated())的代码特别有趣。
猜你喜欢
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 2021-05-17
  • 1970-01-01
相关资源
最近更新 更多