从 DialogFragement 获取数据,将其放入 SQLiteDB 并在回收器视图中显示所有结果
Posted
技术标签:
【中文标题】从 DialogFragement 获取数据,将其放入 SQLiteDB 并在回收器视图中显示所有结果【英文标题】:Getting data from a DialogFragement, putting it in a SQLiteDB and displaying all results in a recycler view 【发布时间】:2017-12-15 19:02:02 【问题描述】:我需要将数据从 DialogFragment 发送到我的 FragmentActivity(基本上是 MainActivity)并将其添加到 SQLiteDB。然后查询所有要显示在recycler视图中的数据。这是 FragmentActivity:
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment);
.....
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
RecyclerView FCRecycler = (RecyclerView) this.findViewById(R.id.FixedCostsRV);
Cursor cursor = getAllFixedCosts();
FCRecycler.setLayoutManager(mLayoutManager);
mAdapter = new FixedCostsAdapter(this, cursor);
FCRecycler.setAdapter(mAdapter);
FixedCostsDbHelper dbHelper = new FixedCostsDbHelper(this);
mDB = dbHelper.getWritableDatabase();
readDB = dbHelper.getReadableDatabase();
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
private Cursor getAllCosts()
return mDB.query(
FixedCostsContract.FixedCostsEntry.TABLE_NAME,
null,
null,
null,
null,
null,
null);
public void addCost(String Item, String CostAmount)
if(Item.length() == 0 ||
CostAmount.length() == 0)
return;
Cost = Integer.parseInt(FixedCostAmount);
addFixedCostDB(Item, Cost);
mAdapter.swapCursor(getAllCosts());
private long addCostDB(String Item,int Cost)
ContentValues cv = new ContentValues();
cv.put(FixedCostsContract.FixedCostsEntry.COLUMN_COST_NAME, FixedCostItem);
cv.put(FixedCostsContract.FixedCostsEntry.COLUMN_COST_AMOUNT, FixedCostItemAmount);
return mDB.insert(FixedCostsContract.FixedCostsEntry.TABLE_NAME, null, cv);
我还将提供 fragmentClass 和 theDialogFragment 以防万一。 片段:
public class FixedCosts extends Fragment
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fixed_costs_fragment, container, false);
SharedPreferences sharedPrefs = this.getActivity().getSharedPreferences("MyPrefs", 0);
Button launchDialog = (Button) view.findViewById(R.id.AddNewFixedCostsButton);
launchDialog.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
DialogFragment dialog = new InfoDialogFragment();
dialog.show(getFragmentManager(), "dialog");
);
String amount = (sharedPrefs.getString("AMOUNTMONEY", "WhatsUP"));
String TextAmount = "$"+amount;
TextView TitletextView = (TextView) view.findViewById(R.id.amountTitle);
TitletextView.setText(TextAmount);
return view;
Fragment 有一个按钮,可让您在 DialogFragment 中输入信息。这是 DialogFragmentClass:
public class InfoDialogFragment extends DialogFragment
public InfoDialogFragment()
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
final AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.info_dialogfrag, null);
adb.setView(view);
final EditText itemET = (EditText)view.findViewById(R.id.getItem);
final EditText itemAmountET = (EditText)view.findViewById(R.id.getItemAmount);
adb
/*.setIcon()*/
.setTitle("Fixed Cost")
.setPositiveButton("Add", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String FixedCostItem = itemET.getText().toString();
String FixedCostItemAmount = itemAmountET.getText().toString();
if(FixedCostItem.length() == 0 || FixedCostItemAmount.length() == 0)
Toast.makeText(getActivity(),"Enter Item and Amount", Toast.LENGTH_SHORT).show();
else
FragmentActivity fragmentActivity = new FragmentActivity();
fragmentActivity.addFixedCost(FixedCostItem, FixedCostItemAmount);
dialog.dismiss();
)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
InfoDialogFragment.this.getDialog().cancel();
);
return adb.create();
SQLiteDB:
public class FixedCostsDbHelper extends SQLiteOpenHelper
private static final String DATABASE_NAME = "waitlist.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = FixedCostsContract.FixedCostsEntry.TABLE_NAME;
public static final String ID_COLUMN = FixedCostsContract.FixedCostsEntry._ID;
public static final String COLUMN_COST_NAME = FixedCostsContract.FixedCostsEntry.COLUMN_COST_NAME;
public static final String COLUMN_COST_AMOUNT = FixedCostsContract.FixedCostsEntry.COLUMN_COST_AMOUNT;
public FixedCostsDbHelper(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
final String SQL_CREATE_FIXEDCOSTS_DB = "CREATE TABLE " + TABLE_NAME + " (" +
ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_COST_NAME + " TEXT NOT NULL, " +
COLUMN_COST_AMOUNT + " INTEGER NOT NULL" +
")";
db.execSQL(SQL_CREATE_FIXEDCOSTS_DB);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL("DROP TABLE IF EXITS " + FixedCostsContract.FixedCostsEntry.TABLE_NAME);
onCreate(db);
非常感谢所有帮助。
【问题讨论】:
【参考方案1】:根据您的活动代码,您似乎在调用后正在初始化 mDB 光标光标 = getAllFixedCosts();。
getAllFiexedCosts() 正在使用仍未初始化的 mDB 变量。在这种情况下,您可能会得到 NullPointerException。
【讨论】:
谢谢你,我应该抓住这个。【参考方案2】:我认为这里可以避免很多双极代码!!!
使用Material Dialogs
制作优雅而简单的对话框
然后使用Activeandroid
保存数据。
您只需 20 到 30 行代码即可完成所有这些工作
MaterialDialogs
https://github.com/afollestad/material-dialogs
ActiveAndroid
https://github.com/pardom/ActiveAndroid
【讨论】:
以上是关于从 DialogFragement 获取数据,将其放入 SQLiteDB 并在回收器视图中显示所有结果的主要内容,如果未能解决你的问题,请参考以下文章
从 API 获取 JSON,将其添加到 sqlite3 数据库并自动获取下一页
如何从数据库中获取大量数据并通过输入输入将其显示到下拉列表过滤器中,但只能从数据库中选择
从android应用程序获取数据并使用php将其存储在sql数据库中
如何从 mongodb 获取数据并使用节点 js 将其显示在表中?