我正在尝试使用AsyncTask将对象添加到我的SQLite数据库中。我有4个类:MainActivity,DBHelper,ToDo_Item和名为DatabaseAsyncAdd的AsyncTask内部类
class ToDo_Item {
//MEMBER ATTRIBUTES
private int _id;
private String description;
private int is_done;
public ToDo_Item() {
}
public ToDo_Item(String desc, int done) {
description = desc;
is_done = done;
}
public int getId() {
return _id;
}
public void setId(int id) {
_id = id;
}
public String getDescription () {
return description;
}
public void setDescription (String desc) {
description = desc;
}
public int getIs_done() {
return is_done;
}
public void setIs_done(int done) {
is_done = done;
}
}
以下是我尝试使用的DBHelper的方法:
public void addToDoItem(ToDo_Item task) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//ADD KEY-VALUE PAIR INFORMATION FOR THE TASK DESCRIPTION
values.put(KEY_DESCRIPTION, task.getDescription()); // task name
//ADD KEY-VALUE PAIR INFORMATION FOR
//IS_DONE VALUE: 0- NOT DONE, 1 - IS DONE
values.put(KEY_IS_DONE, task.getIs_done());
// INSERT THE ROW IN THE TABLE
db.insert(DATABASE_TABLE, null, values);
taskCount++;
// CLOSE THE DATABASE CONNECTION
db.close();
}
我的主要包括AsyncTask
public class MainActivity extends Activity {
protected DBHelper mDBHelper;
private List<ToDo_Item> list;
private MyAdapter adapt;
private EditText myTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TASK 1: LAUNCH THE LAYOUT REPRESENTING THE MAIN ACTIVITY
setContentView(R.layout.activity_main);
// TASK 2: ESTABLISH REFERENCES TO THE UI
// ELEMENTS LOCATED ON THE LAYOUT
myTask = (EditText) findViewById(R.id.editText1);
// TASK 3: SET UP THE DATABASE
mDBHelper = new DBHelper(this);
/*
list = mDBHelper.getAllTasks();
adapt = new MyAdapter(this, R.layout.todo_item, list);
ListView listTask = (ListView) findViewById(R.id.listView1);
listTask.setAdapter(adapt);
*/
}
@Override
protected void onResume(){
super.onResume();
list = mDBHelper.getAllTasks();
adapt = new MyAdapter(this, R.layout.todo_item, list);
ListView listTask = (ListView) findViewById(R.id.listView1);
listTask.setAdapter(adapt);
}
//BUTTON CLICK EVENT FOR ADDING A TODO TASK
public void addTaskNow(View view) {
String s = myTask.getText().toString();
if (s.isEmpty()) {
Toast.makeText(getApplicationContext(), "A TODO task must be entered.", Toast.LENGTH_SHORT).show();
} else {
//BUILD A NEW TASK ITEM AND ADD IT TO THE DATABASE
ToDo_Item task = new ToDo_Item(s, 0);
mDBHelper.addToDoItem(task);
new DatabaseAsyncAdd().execute(task);
// CLEAR OUT THE TASK EDITVIEW
myTask.setText("");
// ADD THE TASK AND SET A NOTIFICATION OF CHANGES
adapt.add(task);
adapt.notifyDataSetChanged();
}
}
//BUTTON CLICK EVENT FOR DELETING ALL TODO TASKS
public void clearTasks(View view) {
mDBHelper.clearAll(list);
adapt.notifyDataSetChanged();
}
private class DatabaseAsyncAdd extends AsyncTask<ToDo_Item, String, Void> {
int progress_status;
@Override
protected void onPreExecute() {
// UPDATE THE UI IMMEDIATELY BEFORE BACKGROUND WORK IS PERFORMED
super.onPreExecute();
Toast.makeText(MainActivity.this, "Accessing Database",
Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(ToDo_Item... task) {
String s = myTask.getText().toString();
mDBHelper.addToDoItem(task);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
//******************* ADAPTER ******************************
private class MyAdapter extends ArrayAdapter<ToDo_Item> {
Context context;
List<ToDo_Item> taskList = new ArrayList<ToDo_Item>();
public MyAdapter(Context c, int rId, List<ToDo_Item> objects) {
super(c, rId, objects);
taskList = objects;
context = c;
}
//******************* TODO TASK ITEM VIEW ******************************
/**
* THIS METHOD DEFINES THE TODO ITEM THAT WILL BE PLACED
* INSIDE THE LIST VIEW.
*
* THE CHECKBOX STATE IS THE IS_DONE STATUS OF THE TODO TASK
* AND THE CHECKBOX TEXT IS THE TODO_ITEM TASK DESCRIPTION.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckBox isDoneChBx = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.todo_item, parent, false);
isDoneChBx = (CheckBox) convertView.findViewById(R.id.chkStatus);
convertView.setTag(isDoneChBx);
isDoneChBx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
ToDo_Item changeTask = (ToDo_Item) cb.getTag();
changeTask.setIs_done(cb.isChecked() == true ? 1 : 0);
mDBHelper.updateTask(changeTask);
}
});
} else {
isDoneChBx = (CheckBox) convertView.getTag();
}
ToDo_Item current = taskList.get(position);
isDoneChBx.setText(current.getDescription());
isDoneChBx.setChecked(current.getIs_done() == 1 ? true : false);
isDoneChBx.setTag(current);
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我正在尝试在addtasknow方法上创建一个ToDo_Item,这是一个按钮单击方法。然后将该对象传递给DatabaseASyncAdd中的execute方法。 mDBHelper.addToDoItem(task)说&#34; ToDo_Item不能应用于ToDo_Item []&#34;虽然它在AsyncTask类之外工作。我没有正确地调用执行吗?
答案 0 :(得分:0)
将mDBHelper.addToDoItem(task)
更改为mDBHelper.addToDoItem(task[0])
private class DatabaseAsyncAdd extends AsyncTask<ToDo_Item, String, Void> {
int progress_status;
@Override
protected void onPreExecute() {
// UPDATE THE UI IMMEDIATELY BEFORE BACKGROUND WORK IS PERFORMED
super.onPreExecute();
Toast.makeText(MainActivity.this, "Accessing Database",
Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(ToDo_Item... task) {
//String s = myTask.getText().toString();
mDBHelper.addToDoItem(task[0]);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
答案 1 :(得分:0)
在
mDBHelper.addToDoItem(task);
task
是ToDo_Item[]
。这就是ToDo_Item...
在签名中的含义。它被称为varargs,用于“变量参数”。