我正在尝试在android的向导结束时创建一个数据库进纸器。
现在会有多种不同类型的标题,但是我只能调用一种,因为我会得到一个空指针异常。
String title = mWizardModel.findByKey("Surf:Post Title").getData().getString(Page.SIMPLE_DATA_KEY);
我需要做的是:
String title = mWizardModel.findByKey("Surf:Post Title" if no data try = "Snow:Post Title" = if no data try "Skate:Post Title").getData().getString(Page.SIMPLE_DATA_KEY);
但是我不确定如何实现这个工作我已经尝试了很多不同的if null语句但是它们似乎都不适用于我需要的东西,因为我的数据库行是这样创建的。
String title = mWizardModel.findByKey("Surf:Post Title").getData().getString(Page.SIMPLE_DATA_KEY);
String maincat = mWizardModel.findByKey("Main Category").getData().getString(Page.SIMPLE_DATA_KEY);
String price = mWizardModel.findByKey("Surf:Price").getData().getString(Page.SIMPLE_DATA_KEY);
myDb.insertRow(title, price, maincat);
以下是onClick
中的完整代码 mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
String title = mWizardModel.findByKey("Surf:Post Title").getData().getString(Page.SIMPLE_DATA_KEY);
String maincat = mWizardModel.findByKey("Main Category").getData().getString(Page.SIMPLE_DATA_KEY);
String price = mWizardModel.findByKey("Surf:Price").getData().getString(Page.SIMPLE_DATA_KEY);
myDb.insertRow(title, price, maincat);
DialogFragment dg = new DialogFragment() {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage(R.string.submit_confirm_message)
.setPositiveButton(R.string.submit_confirm_button, null)
.setNegativeButton(android.R.string.cancel, null)
.create();
}
};
dg.show(getSupportFragmentManager(), "place_order_dialog");
} else {
if (mEditingAfterReview) {
mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
} else {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
}
}
});
编辑:剧情扭曲!此代码仅返回其序列中的最后一次尝试(检查我的注释)
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
//开始序列
Bundle t = mWizardModel.findByKey("Surf:Post Title").getData();
if ( t != null )
{
t = mWizardModel.findByKey("Snow:Post Title").getData();
}
if ( t != null )
{
//只返回此密钥! (“Skate:Post Title”)没有其他人。
t = mWizardModel.findByKey("Skate:Post Title").getData();
}
String title = t.getString(Page.SIMPLE_DATA_KEY);
Bundle p = mWizardModel.findByKey("Surf:Price").getData();
if ( p != null )
{
p = mWizardModel.findByKey("Snow:Price").getData();
}
if ( p != null )
{
p = mWizardModel.findByKey("Skate:Price").getData();
}
String price = p.getString(Page.SIMPLE_DATA_KEY);
String maincat = mWizardModel.findByKey("Main Category").getData().getString(Page.SIMPLE_DATA_KEY);
myDb.insertRow(title, price, maincat);
DialogFragment dg = new DialogFragment() {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage(R.string.submit_confirm_message)
.setPositiveButton(R.string.submit_confirm_button, null)
.setNegativeButton(android.R.string.cancel, null)
.create();
}
};
dg.show(getSupportFragmentManager(), "place_order_dialog");
} else {
if (mEditingAfterReview) {
mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
} else {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
}
}
});
答案 0 :(得分:1)
如果将它分成不同的行,会更容易(也可能有效):
/* not sure what the type is, so let's go with Data... */
Data d = mWizardModel.findByKey("Surf:Post Title").getData();
if ( d == null )
{
d = mWizardModel.findByKey("Surf:Post Title").getData();
}
if ( d == null )
{
//...
}
String title = d.getString(Page.SIMPLE_DATA_KEY);