【发布时间】:2014-03-23 20:55:07
【问题描述】:
我将以下代码用于我的主要活动:
私有 TripsData 数据源; @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 数据源 = 新 TripsData(this); 数据源.open(); 列表值 = datasource.getAllTrips(); ArrayAdapter 适配器 = 新的 ArrayAdapter(这个, android.R.layout.simple_list_item_1,值); setListAdapter(适配器); } @覆盖 公共布尔 onCreateOptionsMenu(菜单菜单){ // 膨胀菜单;如果存在,这会将项目添加到操作栏。 getMenuInflater().inflate(R.menu.main, menu); 返回真; } // 将通过 onClick 属性调用 // main.xml 中的按钮 public void onClick(查看视图){ @SuppressWarnings("未选中") ArrayAdapter 适配器 = (ArrayAdapter) getListAdapter(); 行程行程=空; //行程trip_temp; 开关(view.getId()){ 案例 R.id.add: 旅行=新旅行(视图); //trip.setId(trip_temp.getId()); //trip.setName(trip_temp.getName()); 适配器.add(trip); 休息; 案例 R.id.delete: if (getListAdapter().getCount() > 0) { 行程 = (行程) getListAdapter().getItem(0); datasource.deleteTrip(行程); 适配器.remove(trip); } 休息; } 适配器.notifyDataSetChanged(); } @覆盖 受保护的无效 onResume() { 数据源.open(); 超级.onResume(); } @覆盖 受保护的无效 onPause() { 数据源.close(); 超级.onPause(); } public Trip newTrip(查看视图){ 最终行程行程=新行程(); //创建对话框 最终对话框对话框 = 新对话框(此); //在设置内容视图之前修改功能 //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.project_dialog); //为对话框创建编辑框 final EditText nameEdit=(EditText) dialog.findViewById(R.id.dialog_name_text); final EditText descEdit=(EditText) dialog.findViewById(R.id.dialog_type_text); //定义按钮的文本 查看 dialogButton=dialog.findViewById(R.id.dialog_button_create); TextView text=(TextView) 对话框按钮; text.setText("创建"); //按钮创建 按钮 createButton = (Button) 对话框按钮; // 如果按钮被点击,关闭自定义对话框 createButton.setOnClickListener(new View.OnClickListener() { @覆盖 公共无效 onClick(查看 v){ Trip trip_temp = datasource.createTrip(nameEdit.getText().toString()); //String[] trips = new String[] { "Cool", "Very nice", "Hate it" }; //int nextInt = new Random().nextInt(3); // 将新评论保存到数据库 trip.setId(trip_temp.getId()); trip.setName(trip_temp.getName()); //trip = datasource.createTrip(nameEdit.getText().toString()); dialog.dismiss(); } }); 对话框.show(); 回程; }用户应该能够在对话框中输入值,并且名称将显示在创建的行程列表中。但是,当 List 中只有一个值时似乎存在错误,因为该项目未显示。我花了好几个小时才弄明白。
编辑: 这是我的 TripsData 代码
公共类 TripsData {
private SQLiteDatabase database;
private TripsDB dbHelper;
private String[] allTrips = { TripsDB.TRIP_COLUMN_ID,
TripsDB.TRIP_COLUMN_TYPE};
public TripsData(Context context){
dbHelper = new TripsDB(context);
}
public void open() throws SQLException{
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
public Trip createTrip(String type){
ContentValues values = new ContentValues();
values.put(TripsDB.TRIP_COLUMN_TYPE, type);
long insertId = database.insert(TripsDB.TABLE_TRIPS, null, values);
Cursor cursor = database.query(TripsDB.TABLE_TRIPS,
allTrips, TripsDB.TRIP_COLUMN_ID + " = " + insertId, null, null, null, null);
cursor.moveToFirst();
Trip newTrip = cursorToTrip(cursor);
cursor.close();
return newTrip;
}
public void deleteTrip(Trip trip){
long id = trip.getId();
System.out.println("Project deleted with id: " + id);
database.delete(TripsDB.TABLE_TRIPS, TripsDB.TRIP_COLUMN_ID
+ " = " + id, null);
}
public List<Trip> getAllTrips(){
List<Trip> trips = new ArrayList<Trip>();
Cursor cursor = database.query(TripsDB.TABLE_TRIPS,
allTrips, null, null, null, null, null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
Trip trip = cursorToTrip(cursor);
trips.add(trip);
cursor.moveToNext();
}
cursor.close();
return trips;
}
private Trip cursorToTrip(Cursor cursor){
Trip trip = new Trip();
trip.setId(cursor.getLong(0));
trip.setName(cursor.getString(1));
return trip;
}
【问题讨论】:
标签: android android-activity android-arrayadapter android-alertdialog listactivity