Javascript 将已传递给新活动的数据保存到SQLite数据库中
我已经为此挣扎了好几天了。当我创建一个新联盟并打开BowlerActivity时,我可以看到传递给它的主键的值。例如,如果虚拟联盟=5,我可以在下一个活动中看到该值。我的问题是,我希望能够在创建该值5时将其与投球手关联。我似乎无法将传递的值输入到创建新投球手的对话框中;以便将其保存到我数据库中的bowler表中 使用从MainActivity传递主键Javascript 将已传递给新活动的数据保存到SQLite数据库中,javascript,android,android-sqlite,primary-key,Javascript,Android,Android Sqlite,Primary Key,我已经为此挣扎了好几天了。当我创建一个新联盟并打开BowlerActivity时,我可以看到传递给它的主键的值。例如,如果虚拟联盟=5,我可以在下一个活动中看到该值。我的问题是,我希望能够在创建该值5时将其与投球手关联。我似乎无法将传递的值输入到创建新投球手的对话框中;以便将其保存到我数据库中的bowler表中 使用从MainActivity传递主键 //On Long Click On The RecyclerView Item An Alert Dialog Is Opened With T
//On Long Click On The RecyclerView Item An Alert Dialog Is Opened With The Option To Choose Edit/Delete
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, final int position) {
int leagueId = leaguesList.get(position).getId();
Intent myIntent = new Intent(getBaseContext(), BowlerActivity.class);
myIntent.putExtra("leagueId", leagueId);
startActivity(myIntent);
}
保龄球活性
public class BowlerActivity extends AppCompatActivity {
private BowlerAdapter mAdapter;
private List<Bowler> bowlersList = new ArrayList<>();
private CoordinatorLayout coordinatorLayout;
private RecyclerView recyclerView;
private TextView noBowlersView;
private TextView bowlerLeagueId;
private DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bowler);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
String savedExtra = String.valueOf(getIntent().getIntExtra("leagueId",1));
TextView myText = (TextView) findViewById(R.id.tvLeagueId);
final String s = myText.toString();
myText.setText(savedExtra);
coordinatorLayout = findViewById(R.id.coordinator_layout);
recyclerView = findViewById(R.id.recycler_view);
noBowlersView = findViewById(R.id.empty_bowlers_view);
db = new DatabaseHelper(this);
bowlersList.addAll(db.getAllBowlers());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.add_bowler_fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showBowlerDialog(false, null, -1);
}
});
mAdapter = new BowlerAdapter(this, bowlersList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(mAdapter);
toggleEmptyBowlers();
//On Long Click On The RecyclerView Item An Alert Dialog Is Opened With The Option To Choose Edit/Delete
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, final int position) {
//Intent myIntent = new Intent(getApplicationContext(), SeriesActivity.class);
//startActivity(myIntent);
}
@Override
public void onLongClick(View view, int position) {
showActionsDialog(position);
}
}));
}
//Inserting New Bowler In The Database And Refreshing The List
private void createBowler(String bowler) {
//Inserting Bowler In The Database And Getting Newly Inserted Bowler Id
long id = db.insertBowler(bowler);
//Get The Newly Inserted Bowler From The Database
Bowler n = db.getBowler(id);
if (n != null) {
//Adding New Bowler To The Array List At Position 0
bowlersList.add(0, n);
//Refreshing The List
mAdapter.notifyDataSetChanged();
toggleEmptyBowlers();
}
}
//Updating Bowler In The Database And Updating The Item In The List By Its Position
private void updateBowler(String name, int position) {
Bowler n = bowlersList.get(position);
//Updating Bowler Text
n.setName(name);
//n.setLeagueId( );
//Updating The Bowler In The Database
db.updateBowler(n);
//Refreshing The List
bowlersList.set(position, n);
mAdapter.notifyItemChanged(position);
toggleEmptyBowlers();
}
//Deleting Bowler From SQLite Database And Removing The Bowler Item From The List By Its Position
private void deleteBowler(int position) {
// deleting the note from db
db.deleteBowler(bowlersList.get(position));
//Removing The Bowler From The List
bowlersList.remove(position);
mAdapter.notifyItemRemoved(position);
toggleEmptyBowlers();
}
//Opens Dialog With Edit/Delete Options
//Edit - 0
//Delete - 0
private void showActionsDialog(final int position) {
CharSequence colors[] = new CharSequence[]{"Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose option");
builder.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
showBowlerDialog(true, bowlersList.get(position), position);
} else {
deleteBowler(position);
}
}
});
builder.show();
}
//Show Alert Dialog With EditText Options to Enter/Edit A League
//When shouldUpdate = true, It Will Automatically Display Old Bowler Name And Change The Button Text To UPDATE
private void showBowlerDialog(final boolean shouldUpdate, final Bowler bowler, final int position) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
View view = layoutInflaterAndroid.inflate(R.layout.dialog_bowler, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(BowlerActivity.this);
alertDialogBuilderUserInput.setView(view);
final EditText inputBowler = view.findViewById(R.id.etBowlerNameInput);
//final EditText inputBowlerLeagueId = view.findViewById( R.id.tvLeagueId );
TextView dialogTitle = view.findViewById(R.id.dialog_title);
dialogTitle.setText(!shouldUpdate ? getString(R.string.lbl_new_bowler_title) : getString(R.string.lbl_edit_bowler_title));
if (shouldUpdate && bowler != null) {
inputBowler.setText(bowler.getName());
}
alertDialogBuilderUserInput
.setCancelable(false)
.setPositiveButton(shouldUpdate ? "update" : "save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
}
})
.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
dialogBox.cancel();
}
});
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Show Toast Message When No Text Is Entered
if (TextUtils.isEmpty(inputBowler.getText().toString())) {
Toast.makeText(BowlerActivity.this, "Enter Bowler!", Toast.LENGTH_SHORT).show();
return;
} else {
alertDialog.dismiss();
}
//Check If User Is Updating Bowler
if (shouldUpdate && bowler != null) {
//Updating Bowler By Its Id
updateBowler(inputBowler.getText().toString(), position);
} else {
//Creating New Bowler
createBowler(inputBowler.getText().toString());
}
}
});
}
//Toggling List And Empty Bowler View
private void toggleEmptyBowlers() {
//You Can Check bowlerList.size() > 0
if (db.getBowlersCount() > 0) {
noBowlersView.setVisibility( View.GONE);
} else {
noBowlersView.setVisibility( View.VISIBLE);
}
}
}
非常感谢您对此问题提供的任何帮助。我认为您需要为savedExtra变量提供更大的范围,然后使用它。因此:- 将savedExtra声明为类变量。 更改贴花位置并设置为仅设置值,而不是声明值。 使用savedExtra变量设置新保龄球手的id。
下面的代码请参见带有//的注释,我会将对话框设为a,并在其参数包中传递值。DialogFragment比裸AlertDialog有一些优势,包括在配置更改时自动重新创建。我发现重写片段的onCreateDialog以返回AlertDialog是最容易的。您可以在该方法中检索参数包,并在对话框按钮上的单击侦听器中使用它。记录savedExtra值时会得到什么?注意:在设置Intent myIntent=newintentgetbasecontext、BowlerActivity.class时,尝试使用MainActivity.this而不是getBaseContext;saveExtra显示了上一活动中选择的正确主键。我的问题是从saveExtra获取此值,以便可以访问“添加保龄球手”对话框,以便将联赛主键写入保龄球手表。哎呀,我的错误更改1已相应更改,请重新应用更改。我没有将getIntExtraleagueId中的默认值从1更改为2,1。但是,和以前一样,我建议使用0或-1。这是无法获取额外值时返回的int值。e、 g.如果使用getIntExtranot_an_extra_key,-666,则假设不存在具有非_an_extra_key的key的额外项,则返回的int将为-666。因此,如果您使用-1作为默认值,它可以用来表示由于某种原因,额外的不存在。您上面提到的三个更改仅在创建保龄球手后将主键插入保龄球手联盟ID列,然后更新它们。我尝试使用与您在更新创建投球手时相同的原则,我成功地为创建和更新投球手插入了联赛Id。一个问题,虽然在变更2中,您将1更改为2,但这究竟起到了什么作用,扩大了变量的范围?忘记我的最后一个问题,您在上面的评论中解释过。再次感谢你的帮助,MikeTIf你也检查了答案,我向上移动了3个,很抱歉应该说,而只是说重新应用更改,假设你也会更改3个。很好,已经修好了。
public class BowlerActivity extends AppCompatActivity {
private BowlerAdapter mAdapter;
private List<Bowler> bowlersList = new ArrayList<>();
private CoordinatorLayout coordinatorLayout;
private RecyclerView recyclerView;
private TextView noBowlersView;
private TextView bowlerLeagueId;
private DatabaseHelper db;
private String savedExtra; //<<<< Added change 1.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bowler);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
savedExtra = String.valueOf(getIntent().getIntExtra("leagueId",1)); //<<<< Saved Extra change 2.
TextView myText = (TextView) findViewById(R.id.tvLeagueId);
final String s = myText.toString();
myText.setText(savedExtra);
coordinatorLayout = findViewById(R.id.coordinator_layout);
recyclerView = findViewById(R.id.recycler_view);
noBowlersView = findViewById(R.id.empty_bowlers_view);
db = new DatabaseHelper(this);
bowlersList.addAll(db.getAllBowlers());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.add_bowler_fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showBowlerDialog(false, null, -1);
}
});
mAdapter = new BowlerAdapter(this, bowlersList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(mAdapter);
toggleEmptyBowlers();
//On Long Click On The RecyclerView Item An Alert Dialog Is Opened With The Option To Choose Edit/Delete
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
recyclerView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, final int position) {
//Intent myIntent = new Intent(getApplicationContext(), SeriesActivity.class);
//startActivity(myIntent);
}
@Override
public void onLongClick(View view, int position) {
showActionsDialog(position);
}
}));
}
//Inserting New Bowler In The Database And Refreshing The List
private void createBowler(String bowler) {
//Inserting Bowler In The Database And Getting Newly Inserted Bowler Id
bowler.setLeagueId(savedExtra); //<<<< ADDED change 3.
long id = db.insertBowler(bowler);
//Get The Newly Inserted Bowler From The Database
Bowler n = db.getBowler(id);
if (n != null) {
//Adding New Bowler To The Array List At Position 0
bowlersList.add(0, n);
//Refreshing The List
mAdapter.notifyDataSetChanged();
toggleEmptyBowlers();
}
}
//Updating Bowler In The Database And Updating The Item In The List By Its Position
private void updateBowler(String name, int position) {
Bowler n = bowlersList.get(position);
//Updating Bowler Text
n.setName(name);
//n.setLeagueId( );
//Updating The Bowler In The Database
db.updateBowler(n);
//Refreshing The List
bowlersList.set(position, n);
mAdapter.notifyItemChanged(position);
toggleEmptyBowlers();
}
//Deleting Bowler From SQLite Database And Removing The Bowler Item From The List By Its Position
private void deleteBowler(int position) {
// deleting the note from db
db.deleteBowler(bowlersList.get(position));
//Removing The Bowler From The List
bowlersList.remove(position);
mAdapter.notifyItemRemoved(position);
toggleEmptyBowlers();
}
//Opens Dialog With Edit/Delete Options
//Edit - 0
//Delete - 0
private void showActionsDialog(final int position) {
CharSequence colors[] = new CharSequence[]{"Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose option");
builder.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
showBowlerDialog(true, bowlersList.get(position), position);
} else {
deleteBowler(position);
}
}
});
builder.show();
}
//Show Alert Dialog With EditText Options to Enter/Edit A League
//When shouldUpdate = true, It Will Automatically Display Old Bowler Name And Change The Button Text To UPDATE
private void showBowlerDialog(final boolean shouldUpdate, final Bowler bowler, final int position) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
View view = layoutInflaterAndroid.inflate(R.layout.dialog_bowler, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(BowlerActivity.this);
alertDialogBuilderUserInput.setView(view);
final EditText inputBowler = view.findViewById(R.id.etBowlerNameInput);
//final EditText inputBowlerLeagueId = view.findViewById( R.id.tvLeagueId );
TextView dialogTitle = view.findViewById(R.id.dialog_title);
dialogTitle.setText(!shouldUpdate ? getString(R.string.lbl_new_bowler_title) : getString(R.string.lbl_edit_bowler_title));
if (shouldUpdate && bowler != null) {
inputBowler.setText(bowler.getName());
}
alertDialogBuilderUserInput
.setCancelable(false)
.setPositiveButton(shouldUpdate ? "update" : "save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
}
})
.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
dialogBox.cancel();
}
});
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Show Toast Message When No Text Is Entered
if (TextUtils.isEmpty(inputBowler.getText().toString())) {
Toast.makeText(BowlerActivity.this, "Enter Bowler!", Toast.LENGTH_SHORT).show();
return;
} else {
alertDialog.dismiss();
}
//Check If User Is Updating Bowler
if (shouldUpdate && bowler != null) {
//Updating Bowler By Its Id
updateBowler(inputBowler.getText().toString(), position);
} else {
//Creating New Bowler
createBowler(inputBowler.getText().toString());
}
}
});
}
//Toggling List And Empty Bowler View
private void toggleEmptyBowlers() {
//You Can Check bowlerList.size() > 0
if (db.getBowlersCount() > 0) {
noBowlersView.setVisibility( View.GONE);
} else {
noBowlersView.setVisibility( View.VISIBLE);
}
}
}