sql >> データベース >  >> RDS >> SQLite

カスタムダイアログでデータを更新する方法

    これがカスタムダイアログクラスを処理するためのきちんとした方法です!

    MyDialogを作成できます buttonOnClickメソッドとテキストデータを処理するようにBuilderクラスを含むクラス。

    my_dialog_layout.xml 2番目の図に示すように、4つのeditTexts(名前、カテゴリ、数量、重要度)と3つのボタン(更新、キャンセル、OK)が含まれている必要があります。重要な部分ではないので、xmlコードは投稿しません。

    したがって、 EditActivityのlistViewのOnItemClickListenerで 、できます

    1. ダイアログを作成する
    2. 編集テキストにデフォルトのテキストを設定する
    3. ボタンのonClickListenerを設定

    MyDialogクラス

    public class MyDialog extends DialogFragment {
    
       public static final String SimpleName = MyDialog.class.getSimpleName();
       private EditText name,category, quantity, importance;
       private Button update, positive, negative;
       private Builder builder;
    
        private static MyDialog instance = new MyDialog();
    
        public static MyDialog getInstance(){
            return instance;
        }
    
       @Override
        public void onCreate(Bundle savedInstanceState) {
            this.setCancelable(true);
    
            if (savedInstanceState != null) {
                if (builder != null) {
                    builder = savedInstanceState.getParcelable(Builder.class.getSimpleName());
                }
            }
            setRetainInstance(true);
            super.onCreate(savedInstanceState);
        }
    
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Dialog dialog = super.onCreateDialog(savedInstanceState);
            // make the dialog's default background transparent so that you can customize the window
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            return dialog;
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.my_dialog_layout, container, false);
        }
    
        @Override
        public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            initViews(view);
            if (builder != null) {
    
                if (builder.getTextName() != null) {
                    name.setText(builder.getTextName());
                }
                if (builder.getTextCategory() != null) {
                    category.setText(builder.getTextCategory());
                }
                if (builder.getTextQuantity() != null) {
                    quantity.setText(builder.getTextQuantity());
                }
                if (builder.getTextImportance() != null) {
                    importance.setText(builder.getTextImportance());
                }
    
    
    
                update.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        builder.getOnUpdateClicked().OnClick(view, getDialog());
                    }
                });
    
                positive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       builder.getOnPositiveClicked().OnClick(view, getDialog());
                    }
                });
    
                negative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      builder.getOnNegativeClicked().OnClick(view, getDialog());
                    }
                });
    
            }
        }
    
        private void initViews(View view) {       
            name = (EditText) view.findViewById(R.id.editText_name);
            category = (EditText) view.findViewById(R.id. editText_category);
            quantity = (EditText) view.findViewById(R.id. editText_quantity);
            importance = (EditText) view.findViewById(R.id.editText_importance);
            update = (Button) view.findViewById(R.id.update);
            positive = (Button) view.findViewById(R.id.positive);
            negative = (Button) view.findViewById(R.id.negative);
    
        }
    
        private Dialog show(Activity activity, Builder builder) {             
             this.builder = builder;
             if (!isAdded()){
                 show(((AppCompatActivity) activity).getSupportFragmentManager(), SimpleName);
             }
             return getDialog();
         }
    
        public static class Builder implements Parcelable  {
    
            private OnPositiveClicked onPositiveClicked;
            private OnNegativeClicked onNegativeClicked;
            private OnUpdateClicked onUpdateClicked;
    
            private textName;
            private textCategory;
            private textQuantity;
            private textImportance;
    
            private Context context;
    
            protected Builder(Parcel in) {
                textName = in.readString();
                textCategory = in.readString();
                textQuantity = in.readString();
                textImportance = in.readString();
            }
    
            public static final Creator<Builder> CREATOR = new Creator<Builder>() {
                @Override
                public Builder createFromParcel(Parcel in) {
                    return new Builder(in);
                }
    
                @Override
                public Builder[] newArray(int size) {
                    return new Builder[size];
                }
            };
    
            public Context getContext() {
                return context;
            }
    
            public Builder setActivity(Context context) {
                this.context = context;
                return this;
            }
    
            public Builder(Context context) {
                this.context = context;
            }
    
            public Builder setTextName(String textName) {
                this.textName = textName;
                return this;
            }
    
            public String getTextName() {
                return textName;
            }
    
            public Builder setTextCategory(String textCategory) {
                this.textCategory = textCategory;
                return this;
            }
    
            public String getTextCategory() {
                return textCategory;
            }
    
            public Builder setTextQuantity(String textQuantity) {
                this.textQuantity = textQuantity;
                return this;
            }
    
            public String getTextQuantity() {
                return textQuantity;
            }
    
            public Builder setTextImportance(String textImportance) {
                this.textImportance = textImportance;
                return this;
            }
    
            public String getTextImportance() {
                return textImportance;
            }
    
    
            public OnPositiveClicked getOnPositiveClicked() {
                return onPositiveClicked;
            }
    
            public Builder setOnPositiveClicked(OnPositiveClicked onPositiveClicked) {
                this.onPositiveClicked = onPositiveClicked;
                return this;
            }
    
            public OnNegativeClicked getOnNegativeClicked() {
                return onNegativeClicked;
            }
    
            public Builder setOnNegativeClicked(OnNegativeClicked onNegativeClicked) {
                this.onNegativeClicked = onNegativeClicked;
                return this;
            }
    
            public OnUpdateClicked getOnUpdateClicked() {
                return onUpdateClicked;
            }
    
            public Builder setOnUpdateClicked(OnUpdateClicked onUpdateClicked) {
                this.onUpdateClicked = onUpdateClicked;
                return this;
            }
    
            public Builder build() {
                return this;
            }
    
            public Dialog show() {
                return getInstance().show(((Activity) context), this);
            }
    
            @Override
            public int describeContents() {
                return 0;
            }
    
            @Override
            public void writeToParcel(Parcel parcel, int i) {
                parcel.writeString(textName);
                parcel.writeString(textCategory);
                parcel.writeString(textQuantity);
                parcel.writeString(textImportance);
            }
    
        }
    
        public interface OnPositiveClicked {
            void OnClick(View view, Dialog dialog);
        }
    
        public interface OnNegativeClicked {
            void OnClick(View view, Dialog dialog);
        }
    
    
    }
    

    EditActivity

    MyDialogをビルドしてリストビューOnItemClickListnerに表示します。

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                MyDialog.Builder dialog=null;
    
                // TODO get the strings from database at the position i
                String name = 
                String category = 
                String quantity = 
                String importance = 
    
                dialog.setTextName(name)
                    .setTextCategory(category)
                    .setTextQuantity(quantity)
                    .setTextImportance(importance)
                    .setOnPositiveClicked(new MyDialog.OnPositiveClicked() { 
                                 @Override
                                public void OnClick(View view, Dialog dialog) {
    
                                }
                            })
                    .setOnNegativeClicked(new MyDialog.OnNegativeClicked() { 
                                @Override
                                public void OnClick(View view, Dialog dialog) {
    
                                }
                            })
                    .setOnUpdateClicked(new MyDialog.OnUpdateClicked() { 
                                @Override
                                public void OnClick(View view, Dialog dialog) {
                                    // TODO update database here
                                }
                            })
    
                    .build();
                    dialog.show();
            }
        });
    

    それが役に立てば幸い。間違いやより良い方法があるかどうか教えてください。




    1. SQL Server(T-SQL)でデータ型のリストを返す方法

    2. psycopg2のホイールの構築に失敗しました-virtualenvとpipを使用したMacOSX

    3. MySQLで行ジェネレータを作成するにはどうすればよいですか?

    4. LoaderManagerを使用して自動的に再クエリする方法