我有一个listview,每行都有一个文本字段和edittext字段。我让他们都在屏幕上战斗。当我通过接听电话,返回等方式恢复活动时,edittext字段中的输入与最初输入的内容不匹配。我想知道如何设置onresume或保存的即时状态来防止这种情况,并确保正确的输入位于正确的edittext字段中。

这是我正在使用的代码。

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中的EditTexts是一个很大的痛苦,以防万一你开始遇到问题。其次,你似乎并没有拯救 inputValue 字符串在任何点。至少你应该序列化这些值 onSaveInstanceState() 把它们读回来 onCreate().您也不应该将它们存储在适配器中。你真的应该有一个适当的"模型"(带有标签和输入值的对象)支持适配器。SimpleCursorAdapter不太适合并行修改数据。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top