我开始使用Android,并将此课程列为成分列表。每个成分都有复选框和添加按钮。我希望将此数据传输到本地数据库,但不确定如何进行此操作。我需要数据库帮助程序类吗?请给我一些代码示例或如何在我的onclick方法中实现它。以下是我的代码......
Tab2Activity.java
public class Tab2Activity extends Fragment {
public ArrayList<String> shoppingList;
public ArrayList<CheckBox> checkboxes;
// public CheckBox cb;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_two, container, false);
shoppingList = new ArrayList<String>();
checkboxes = new ArrayList<CheckBox>();
LinearLayout l = (LinearLayout)inflater.inflate(R.layout.tab_two, container, false);
// RelativeLayout s = (RelativeLayout)inflater.inflate(R.layout.tab_two, container, false);
// RelativeLayout r = (RelativeLayout)s.findViewById(R.layout.text);
Bundle b = getArguments();
String ingredientsString = b.getString("ingredients");
System.out.println("string: " + ingredientsString);
String[] ingredients = ingredientsString.split(",");
System.out.println("size: " + ingredients.length);
ScrollView sv = new ScrollView(getActivity());
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(int i = 0; i <ingredients.length; i++) {
final CheckBox cb = new CheckBox(getActivity());
cb.setText(ingredients[i]);
cb.setTag("checkbox" + i);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.getTag();
shoppingList.add(cb.getText().toString());
System.out.println("ADDING: " + cb.getText().toString());
} else {
shoppingList.remove(cb.getText().toString());
System.out.println("REMOVING: " + cb.getText().toString());
}
}
});
ll.addView(cb);
}
Button button = new Button(getActivity());
button.setText("Add to list");
button.setPadding(0, 10, 0, 10);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String list = "";
for (int x=0; x<shoppingList.size(); x++)
list += shoppingList.get(x) + "\n";
new AlertDialog.Builder(getActivity())
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Save List")
.setMessage("Add following items to list?\n\n" + list)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("No", null)
.show();
}
});
ll.addView(button);
l.addView(sv);
return l;
// return v;
}
}
更新:我做了一个数据库课......你觉得我走在正确的轨道上吗?
public class DBhelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Ingredients_DB";
private static final int DATABASE_VERSION = 2;
private static final String TABLE_NAME = "INGREDIENTS";
public static final String Col_1 = "TITLE";
public DBhelper(Context context) {
super(context, DATABASE_NAME, null, 2);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + TABLE_NAME + "(TITLE STRING PRIMARY KEY AUTOINCREMENT, TITLE 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 Title) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Col_1, Title);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
}
else
return true;
}
}
答案 0 :(得分:0)
有几个options。将其存储在本地数据库中可能是也可能不是最好的方法。无论哪种方式,官方文档都可以指导您如何使用以下任何选项:
请参阅Android documentation "Storage Options"了解如何实施适合您情况的最佳方案。