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

南楼画角 提交于 2019-12-19 07:36:15

问题


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

    }

}


回答1:


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.



来源:https://stackoverflow.com/questions/9081700/i-cant-write-into-the-edittext-it-disappears-when-i-try-to-write-something-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!