这是我的代码,我不知道列表视图中有什么错误。当我点击按钮,列出所有它没有记录
以及如何在保存记录后清除所有字段
public class ProjectDetail extends Activity implements View.OnClickListener{
Button btnsave, btndelete, btnclose;
EditText ettitle, etType, etprio, ettf;
EditText etsd, eted,etcost,etstat;
private int _Project_Id=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_detail);
btnsave = (Button) findViewById(R.id.btnSave);
btndelete = (Button) findViewById(R.id.btnClose);
btnclose = (Button) findViewById(R.id.btnClose);
ettitle = (EditText) findViewById(R.id.eTtitle);
etType = (EditText) findViewById(R.id.eTtype);
etprio = (EditText) findViewById(R.id.eTprio);
ettf = (EditText) findViewById(R.id.eTtf);
etsd = (EditText) findViewById(R.id.eTsd);
eted = (EditText) findViewById(R.id.eTed);
etcost = (EditText) findViewById(R.id.eTcost);
etstat = (EditText) findViewById(R.id.eTstat);
**_Project_Id=0;
Intent intent = getIntent();
_Project_Id = intent.getIntExtra("project_Id", 0);
ProjectCrud pcrud = new ProjectCrud(this);
Project project = new Project();
project = pcrud.getProjectById(_Project_Id);
ettitle.setText(project.title);
etType.setText(project.type);
etprio.setText(String.valueOf(project.priority));
ettf.setText(project.timeframe);
etsd.setText(project.start);
eted.setText(project.end);
etcost.setText(String.valueOf(project.cost));
etstat.setText(project.status);
}**
@Override
**public void onClick(View view) {
if (view == findViewById(R.id.btnSave)){
ProjectCrud pcrud = new ProjectCrud(this);
Project project = new Project();
project.title=ettitle.getText().toString();
project.type=etType.getText().toString();
project.priority= Integer.parseInt(etprio.getText().toString());
project.timeframe=ettf.getText().toString();
project.start=etsd.getText().toString();
project.end=eted.getText().toString();
project.cost=Integer.parseInt(etcost.getText().toString());
project.status=etstat.getText().toString();
project.project_ID=_Project_Id;
if(_Project_Id==0){
_Project_Id=pcrud.insert(project);
Toast.makeText(this,"New Project Created", Toast.LENGTH_SHORT).show();
}else {
pcrud.update(project);
Toast.makeText(this,"Project Updated", Toast.LENGTH_SHORT).show();
}
}else if (view == findViewById(R.id.btnDelete)){
ProjectCrud pcrud = new ProjectCrud(this);
pcrud.delete(_Project_Id);
Toast.makeText(this,"Project Deleted", Toast.LENGTH_SHORT).show();
finish();
}else if (view == findViewById(R.id.btnClose)){
finish();
}**
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_project_detail, 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);
}
}
这是我的crud操作代码
public class ProjectCrud {
private DBHelper dbHelper;
public ProjectCrud(Context context){
dbHelper = new DBHelper(context);
}
public int insert(Project project){
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Project.KEY_title, project.title);
values.put(Project.KEY_type, project.type);
values.put(Project.KEY_priority, project.priority);
values.put(Project.KEY_timeframe, project.timeframe);
values.put(Project.KEY_start, project.start);
values.put(Project.KEY_end, project.end);
values.put(Project.KEY_cost, project.cost);
values.put(Project.KEY_status, project.status);
long project_Id = db.insert(Project.TABLE, null, values);
db.close();
return (int) project_Id;
}
public void delete(int project_Id) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(Project.TABLE, Project.KEY_ID + "=?", new String[]{ String.valueOf(project_Id) });
db.close();
}
public void update(Project project){
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Project.KEY_title, project.title);
values.put(Project.KEY_type, project.type);
values.put(Project.KEY_priority, project.priority);
values.put(Project.KEY_timeframe, project.timeframe);
values.put(Project.KEY_start, project.start);
values.put(Project.KEY_end, project.end);
values.put(Project.KEY_cost, project.cost);
values.put(Project.KEY_status, project.status);
db.update(Project.TABLE, values, Project.KEY_ID + "= ?", new String[]{String.valueOf(project.project_ID)});
db.close();
}
public ArrayList<HashMap<String, String>> getProjectList (){
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Project.KEY_ID + "," +
Project.KEY_title + "," +
Project.KEY_type + "," +
Project.KEY_priority + "," +
Project.KEY_timeframe + "," +
Project.KEY_start + "," +
Project.KEY_end + "," +
Project.KEY_cost + "," +
Project.KEY_status +
" FROM " + Project.TABLE;
**ArrayList<HashMap<String, String>> projectList = new ArrayList<HashMap<String, String>>();**
Cursor cursor= db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do {
HashMap<String , String > project = new HashMap<String, String >();
project.put("id", cursor.getString(cursor.getColumnIndex(Project.KEY_ID)));
project.put("title", cursor.getString(cursor.getColumnIndex(Project.KEY_title)));
projectList.add(project);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return projectList;
}
public Project getProjectById(int Id){
SQLiteDatabase db = dbHelper.getReadableDatabase();
String selectQuery = "SELECT " +
Project.KEY_ID + "," +
Project.KEY_title + "," +
Project.KEY_type + "," +
Project.KEY_priority + "," +
Project.KEY_timeframe + "," +
Project.KEY_start + "," +
Project.KEY_end + "," +
Project.KEY_cost + "," +
Project.KEY_status +
" FROM " + Project.TABLE
+ " WHERE " +
Project.KEY_ID + "=?";
int iCount =0;
Project project = new Project();
Cursor cursor = db.rawQuery(selectQuery, new String[]{ String.valueOf(Id)});
if (cursor.moveToFirst()){
do{
project.project_ID = cursor.getInt(cursor.getColumnIndex(Project.KEY_ID));
project.title = cursor.getString(cursor.getColumnIndex(Project.KEY_title));
project.type = cursor.getString(cursor.getColumnIndex(Project.KEY_type));
project.priority = cursor.getInt(cursor.getColumnIndex(Project.KEY_priority));
project.timeframe = cursor.getString(cursor.getColumnIndex(Project.KEY_timeframe));
project.start = cursor.getString(cursor.getColumnIndex(Project.KEY_start));
project.end = cursor.getString(cursor.getColumnIndex(Project.KEY_end));
project.cost = cursor.getInt(cursor.getColumnIndex(Project.KEY_cost));
project.status = cursor.getString(cursor.getColumnIndex(Project.KEY_status));
}while (cursor.moveToNext());
}
cursor.close();
db.close();
return project;
}
}
我的代码有问题吗?
答案 0 :(得分:2)
在onCreate()中注册您的点击次数:
btnsave.setOnClickListener(this);
btndelete.setOnClickListener(this);
btnclose.setOnClickListener(this);
答案 1 :(得分:1)
您可以在xml中定义onClick事件:
android:onClick="onClick"
请参阅此主题以获取解释:How exactly does the android:onClick XML attribute differ from setOnClickListener?
使用:
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnDelete"
android:id="@+id/btnDelete"
android:layout_alignTop="@+id/btnSave"
android:layout_toLeftOf="@+id/btnClose"
android:layout_toStartOf="@+id/btnClose"
android:onClick="onClick"/>
答案 2 :(得分:1)
您可以在xml中定义onClick事件:
android:onClick="onClick"
或在
中注册您的点击次数onCreate() :
//on click listener
btnName.setOnClickListener(this);
答案 3 :(得分:0)
添加到xml中的视图
android:onClick="onClick"
but better btnsave = (Button) findViewById(R.id.btnSave);
btndelete = (Button) findViewById(R.id.btnClose);
btnclose = (Button) findViewById(R.id.btnClose);
btnsave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ProjectCrud pcrud = new ProjectCrud(this);
Project project = new Project();
project.title=ettitle.getText().toString();
project.type=etType.getText().toString();
project.priority= Integer.parseInt(etprio.getText().toString());
project.timeframe=ettf.getText().toString();
project.start=etsd.getText().toString();
project.end=eted.getText().toString();
project.cost=Integer.parseInt(etcost.getText().toString());
project.status=etstat.getText().toString();
project.project_ID=_Project_Id;
if(_Project_Id==0){
_Project_Id=pcrud.insert(project);
Toast.makeText(this,"New Project Created", Toast.LENGTH_SHORT).show();
}else {
pcrud.update(project);
Toast.makeText(this,"Project Updated", Toast.LENGTH_SHORT).show();
}
}
})
btndelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ProjectCrud pcrud = new ProjectCrud(this);
pcrud.delete(_Project_Id);
Toast.makeText(this,"Project Deleted", Toast.LENGTH_SHORT).show();
finish();
}
});
btnclose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
}));