سؤال

لدي عرض قائمة يحتوي كل صف على حقل نصي وحقل تحرير النص.لقد جعلهم جميعًا يتقاتلون على الشاشة.عندما أستأنف النشاط إما عن طريق تلقي مكالمة أو العودة وما إلى ذلك، فإن الإدخال في حقول تحرير النص لا يتطابق مع ما تم إدخاله في الأصل.كنت أتساءل كيف يمكنني إعداد حالة الاستئناف أو الحالة الفورية المحفوظة لمنع ذلك والتأكد من أن الإدخال الصحيح موجود في حقل تحرير النص الصحيح.

هذا هو الكود الذي أعمل معه.

public class editview extends ListActivity {
    private dbadapter mydbhelper;
    private PopupWindow pw;
    public static int editCount;
    public static ListView listView;
    public ItemAdapter adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mydbhelper = new dbadapter(this);
        mydbhelper.open();


        View footer = getLayoutInflater().inflate(R.layout.footer_layout, null);
        ListView listView = getListView();
        listView.addFooterView(footer);
        showResults();
        }

    //Populate view
    private void showResults (){
        Cursor cursor = mydbhelper.getUserWord();
        startManagingCursor(cursor);
        String[] from = new String[] {dbadapter.KEY_USERWORD};
         int[] to = new int[] {R.id.textType};
         adapter = new ItemAdapter(this, R.layout.edit_row, cursor,
                        from, to);
            adapter.notifyDataSetChanged();
            this.setListAdapter(adapter);
            editCount = adapter.getCount();
            adapter.notifyDataSetChanged();
    }


            //footer button
            public void onClick(View footer){
                    final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50);
                    editClickSound.start();
                    if (ItemAdapter.inputValues.containsValue("")){
                        Toast.makeText(this, "Please fill in all fields", 1000).show();
                          }else{
                          startActivity(new Intent("wanted.pro.madlibs.OUTPUT"));
                                };

                }
...

            }
            @Override
            protected void onResume() {
                super.onResume();
            }

            @Override
            protected void onPause() {

                super.onPause();

            }


        }
//custom cursor adapter
class ItemAdapter extends SimpleCursorAdapter {

    private LayoutInflater mInflater;
    private Cursor cursor;
    static Map<Integer, String> inputValues = new LinkedHashMap<Integer, String>();
    static String oldText;


    public ItemAdapter(Context context, int layout, Cursor cursor, String[] from,
            int[] to) {
        super(context, layout, cursor, from, to);
        this.cursor = cursor;
        mInflater = LayoutInflater.from(context);

    }


    static class ViewHolder implements TextWatcher {
        protected TextView text;
        protected EditText edittext;
        protected int position;

        public void afterTextChanged(Editable editable) {
            Log.e(String.valueOf(position), "Position in array");
            inputValues.put(position, editable.toString());

        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

        }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.edit_row, null);


             holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.textType);
            holder.edittext = (EditText) convertView.findViewById(R.id.editText);
            holder.edittext.addTextChangedListener(holder);
            holder.position = position;
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        cursor.moveToPosition(position);
        int label_index = cursor.getColumnIndex("userword"); 
        String label = cursor.getString(label_index);

        holder.text.setText(label);
        oldText =  inputValues.get(position);
        holder.edittext.setText(oldText == null ? "" : oldText);

        return convertView;

    }
}
هل كانت مفيدة؟

المحلول

أولاً، تعتبر نصوص التحرير في ListViews بمثابة ألم كبير، فقط في حالة بدء مواجهة المشكلات.ثانيا، يبدو أنك لا تقوم بحفظ inputValue سلاسل في أي لحظة.على الأقل يجب عليك إجراء تسلسل للقيم في onSaveInstanceState() وقراءتها مرة أخرى onCreate().لا ينبغي عليك تخزينها في المحول أيضًا.يجب أن يكون لديك بالفعل "نموذج" مناسب (كائن ذو تسمية وقيم إدخال) يدعم المحول.إن SimpleCursorAdapter ليس مناسبًا تمامًا لتعديل البيانات بالتوازي.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top