我正在查看其他列表帖子列表,但它们似乎对我的情况没有帮助或可用(也许我只是愚蠢)。无论如何,我正在构建一个需要登录到列表的应用程序,您可以在其中添加一个项目,如果选择该项目,将显示一个新的空列表,可以在其中添加项目。
例如,假设您登录,第一个列表是锻炼列表。该列表包括具有以下字段的项目:
workout_list_name_ ,
workout_list_type, and
workout_list_date.
如果您选择该项目,它会将您带到一个空列表(新活动),您可以在其中添加一个带有字段的项目:
item_name ,
item_sets,item_reps,
item_weight,
item_completed(boolean value user can change if they have already completed it).
这就是我的问题所在:我有这两个 ListView 都带有android:id="@+id/android:list"
. 两个 ListView 都使用SimpleCursorAdapter来显示我要添加的行的 xml 布局。当我创建锻炼列表并保存它时,它会添加到我的 sqlite 数据库中,但它没有显示在我的 ListView 中。
如何更改我的 ListView 结构和适配器以显示数据? 我想摆脱
WorkoutList extends ListActivity
并使其仅扩展Activity,但我只是不确定如何实现这一点。
你能帮助我吗?
这是我的第一个列表的代码:
public class WorkoutList extends ListActivity {
Button addNewWorkout;
WorkoutDbAdapter mDbHelper;
public static final int CREATE_WORKOUT = 1;
//public static final int EDIT_WORKOUT = 2;
public static final int SET_WORKOUT = 2;
String dateCreated = null;
Calendar now = null;
SimpleDateFormat format = null;
ListView myList;
Intent prevIntent ;
String woName,userName;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_list);
addNewWorkout = (Button) findViewById(R.id.btnNewWorkout);
prevIntent = getIntent();
userName = prevIntent.getStringExtra("userName");
// do the work for getting the current time and formatting it
now = Calendar.getInstance();
format = new SimpleDateFormat("EEE MMM dd hh:mm aaa");
dateCreated = format.format(now.getTime());
mDbHelper = new WorkoutDbAdapter(this);
mDbHelper.open();
myList = (ListView)findViewById(R.id.workout_list);
registerForContextMenu(myList);
myList.setDivider(getResources().getDrawable(R.color.mainDivider));
myList.setDividerHeight(1);
addNewWorkout.setOnClickListener(NewWorkout);
fillData();
}
OnClickListener NewWorkout = new OnClickListener(){
public void onClick(View arg0) {
Intent newWorkout = new Intent();
newWorkout.setClass(getApplicationContext(), AddWorkout.class);
newWorkout.putExtra("dateCreated", dateCreated );
newWorkout.putExtra("userName", userName);
startActivityForResult(newWorkout, CREATE_WORKOUT);
}
};
//===============================================================================
//
@Override
public void onPause(){
super.onPause();
}
//===============================================================================
//
@Override
public void onResume(){
super.onResume();
mDbHelper.open();
}
@Override
public void onDestroy(){
super.onDestroy();
mDbHelper.close();
}
//================================================================================
//
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
woName = data.getStringExtra("workoutName");
fillData();
}
//================================================================================
//
// Fill the data for UI rebuilds
private void fillData(){
Cursor workoutCursor = mDbHelper.fetchAllWorkouts(userName);
startManagingCursor(workoutCursor);
String [] from = new String [] {WorkoutDbAdapter.KEY_WORKOUT_NAME,WorkoutDbAdapter.KEY_WORKOUT_CREATED_DATE ,
WorkoutDbAdapter.KEY_WORKOUT_TYPE};
int [] to = new int [] {R.id.dateCreatedLabel, R.id.nameLabel, R.id.typeLabel};
SimpleCursorAdapter workouts = new SimpleCursorAdapter(this, R.layout.workout_row, workoutCursor, from, to);
myList.setAdapter(workouts);
}
//===============================================================================
//
@Override
public void onCreateContextMenu(ContextMenu menu , View v, ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
menu.setHeaderTitle("Options");
menu.setHeaderIcon(R.drawable.ic_launcher);
inflater.inflate(R.menu.list_item_longpress, menu);
}
//===============================================================================
//
@Override
public boolean onContextItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menu_delete:
final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
AlertDialog.Builder confirmAlert = new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Are you Sure?")
.setMessage("This will permanently delete the workout and all subsequent exercises from your workout list. " +
"Are you sure you want to continue?")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mDbHelper.deleteWorkout(info.id);
fillData();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
confirmAlert.show();
return true;
case R.id.menu_cancel:
return false;
}
return super.onContextItemSelected(item);
}
//================================================================================
protected void onListItemClick(ListView myList, View v, int position, final long id){
super.onListItemClick(myList, v, position, id);
AlertDialog.Builder dialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.edit)
.setTitle("Update Selected Workout")
.setMessage("Would you like to update the current Workout? Click continue to proceed.")
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
final Intent i = new Intent(getBaseContext(), ExerciseList.class);
i.putExtra(WorkoutDbAdapter.KEY_ROW_ID, id);
i.putExtra("workoutName", woName);
startActivityForResult(i, SET_WORKOUT);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
}