android – 如何更新自定义对话框中的数据
作者:互联网
用户此时单击包含数据的行,并显示带有文本字段的对话框.
我希望用户使用此对话框更新字符串.
我怎样才能做到这一点?
我的数据库类中已有一个更新方法,但我不确定如何在Dialog中实现更新
数据库类
package ie.example.artur.projectrepeat;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
/**
* Created by family on 06/07/2016.
*/
public class DatabaseClass extends SQLiteOpenHelper {
public static final String DATABASE_Name = "Product.db2";
public static final String Table_Name = "product_table2";
public static final String COL_1 = "ID";
public static final String COL_2 = "Name";
public static final String COL_3 = "Quantity";
public static final String COL_4 = "Category";
public static final String COL_5 = "Importance";
Context myContext;
public DatabaseClass(Context context) {
super(context, DATABASE_Name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + Table_Name + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,Quantity TEXT,Category INTEGER,Importance TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop Table If Exists" + Table_Name);
onCreate(db);
}
public boolean insertData(String name, String quantity, String category,String importance) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, name);
contentValues.put(COL_3, quantity);
contentValues.put(COL_4, category);
contentValues.put(COL_5, importance);
long result = db.insert(Table_Name, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + Table_Name, null);
return res;
}
public boolean updateData(String id,String name,String quantity,String category,String importance ) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, quantity);
contentValues.put(COL_4, category);
contentValues.put(COL_5, importance);
db.update(Table_Name,contentValues,"id =?",new String[]{id});
return true;
}
/* public Cursor getCursor(){
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setR
}
*/
public Integer DeleteData (String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(Table_Name,"ID = ?",new String[]{id});
}
public static void DeleteInformation(String item_name, SQLiteDatabase sqLiteDatabase){
String selection = COL_1+" LIKE ?";
String [] selection_args = {item_name};
sqLiteDatabase.delete(Table_Name,selection,selection_args);
}
public Cursor getInformation(SQLiteDatabase sqLiteDatabase)
{
Cursor cursor;
String [] Projections = {COL_1,COL_2,COL_4};
cursor = sqLiteDatabase.query(Table_Name,Projections,null,null,null,null,null);
return cursor;
}
public Cursor getItem(String item_name ,SQLiteDatabase sqLiteDatabase){
String [] Projections = {COL_1,COL_2,COL_3,COL_4,COL_5};
String selection = COL_1+" LIKE ?";
String [] selection_args = {item_name};
Cursor cursor = sqLiteDatabase.query(Table_Name,Projections,selection,selection_args,null,null,null);
return cursor;
}
}
EditActivity
package ie.example.artur.projectrepeat;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Edit_Activity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView listView;
SQLiteDatabase sqLiteDatabase;
DatabaseClass database;
Cursor cursor;
ListDataAdapter listDataAdapter;
Dialog d;
EditText editText_name,editText_Quantity,editText_Category,editTextId,editText_Number;
Button updateBtn;
EditText nameEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
listView = (ListView) findViewById(R.id.list_view);
listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
listView.setAdapter(listDataAdapter);
listView.setOnItemClickListener(this);
database = new DatabaseClass(getApplicationContext());
editText_name = (EditText) findViewById(R.id.editText_name);
editText_Quantity = (EditText) findViewById(R.id.editText_Quantity);
editText_Category = (EditText) findViewById(R.id.editText_Category);
editText_Number = (EditText)findViewById(R.id.editText_Number);
editTextId = (EditText) findViewById(R.id.editText_id);
sqLiteDatabase = database.getReadableDatabase();
Cursor cursor=database.getInformation(sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
String id, product_name, category;
id = cursor.getString(0);
product_name = cursor.getString(1);
category = cursor.getString(2);
DataProvider dataProvider = new DataProvider(id, product_name, category);
listDataAdapter.add(dataProvider);
} while (cursor.moveToNext()
);
}
}
public void loginMethod() {
// Create an instance of the dialog fragment and show it
MyDialog dialog = new MyDialog();
dialog.show(getFragmentManager(),"my_dialog");
}
/*
private void showDialog(){
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.input);
dialog.setTitle("Here Goes the Title");
Button updateBtn = (Button) d.findViewById(R.id.updateBtn);
dialog.show();
}
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position , long id) {
TextView tv = (TextView) view.findViewById(R.id.product_id);
sqLiteDatabase = database.getReadableDatabase();
loginMethod();
/*
listView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.input);
dialog.setTitle("Title...");
final EditText nameEditText = (EditText) d.findViewById(R.id.product_name);
Button updateBtn = (Button) d.findViewById(R.id.updateBtn);
/* if (position == -1) {
updateBtn.setEnabled(false);
} else
updateBtn.setEnabled(true);
updateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
dialog.show();
}
});
*/
/*private void displayInputDialog(final int pos) {
d = new Dialog(this);
d.setTitle("List View");
d.setContentView(R.layout.input);
final EditText nameEditText = (EditText) d.findViewById(R.id.product_name);
Button updateBtn = (Button) d.findViewById(R.id.updateBtn);
if (pos == -1) {
updateBtn.setEnabled(false);
} else
updateBtn.setEnabled(true);
updateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});*
}*/
}
}
MyDialog类
package ie.example.artur.projectrepeat;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
/**
* Created by family on 12/08/2016.
*/
public class MyDialog extends DialogFragment{
LayoutInflater inflater;
View v;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
v= inflater.inflate(R.layout.input,null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
解决方法:
这是一个处理自定义对话框类的简洁方法!
您可以使MyDialog类包含Builder类,以便它处理buttonOnClick方法和文本数据.
my_dialog_layout.xml应包含4个editTexts(名称,类别,数量,重要性)和3个按钮(更新,取消,确定),如第二张图片所示.我不会发布xml代码,因为它不是关键部分.
因此,在EditActivity中listView的OnItemClickListener中,您可以
>构建对话框
>在编辑文本上设置默认文本
>为按钮设置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
在listview OnItemClickListner中构建和显示MyDialog.
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();
}
});
希望能帮助到你.如果有错误或更好的方式,请告诉我.
标签:android,sqlite,android-sqlite,android-dialog,customdialog 来源: https://codeday.me/bug/20191002/1842206.html