我正在查看其他列表的帖子,但在我的情况下,似乎没有任何一个看起来有用或可用(也许我只是愚蠢)。无论如何,我正在构建一个应用程序,需要登录到一个列表,您可以在其中添加一个项目,如果选择该项目,将显示一个新的空列表,其中可以添加项目。
例如,假设您登录,第一个列表是锻炼列表列表。该列表包括具有字段的项目:
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布局。当我创建锻炼列表并保存它时,它会被添加到我的sqlitedatabase中,但它不会显示在我的ListView中。
如何更改ListView结构和适配器以显示数据? 我想摆脱
WorkoutList extends ListActivity
并使其仅扩展活动,但我不确定如何实现此目标。
你能帮助我吗?
这是第一个List的代码:
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();
}
}
答案 0 :(得分:0)
我似乎通过另一篇文章找到了自己的答案。我在错误的地方找到了一个实现ListView
而没有extends ListViewActivity
Android how to use adapter for listView without extending listActivity
答案 1 :(得分:0)
您的代码看起来很乱,并且很难根据您的要求进行维护。
只有当屏幕上的唯一内容是您的列表时,才应该使用ListActivity。所以首先摆脱ListActivity。您可以遵循以下几个步骤:
您的活动的主要xml布局,其中包含您要显示的列表视图和其他项目。 这样的事情会起作用:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000" />
</RelativeLayout>
为第一个(主页)listView创建项目列表(数组,列表等)。使用这些项创建适配器并将其设置为列表适配器。