1

私はリストの投稿の他のリストを調べていましたが、私の状況では役に立たなかったり、使用できたりするものはありませんでした (おそらく私は愚かです)。とにかく、アイテムを追加できるリストへのログインを必要とするアプリを構築しています。リストを選択すると、アイテムを追加できる新しい空のリストが表示されます。

たとえば、ログインして、最初のリストがワークアウト リストのリストであるとします。リストには、次のフィールドを持つ項目が含まれます。

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).

ここに私の問題があります:私はこれら2つのListViewsを両方とも持っていandroid:id="@+id/android:list"ます。両方の ListViews は、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();
}


}
4

2 に答える 2

0

あなたのコードは乱雑に見え、あなたが持っている種類の要件で維持するのは本当に難しいです。
ListActivityは、画面上の唯一のものがリストである場合にのみ使用する必要があります。それ以外の場合は使用しないでください。したがって、最初に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のアイテム(配列、リストなど)のリストを作成します。これらの項目を使用してアダプターを作成し、これをリストアダプターとして設定します。

  • このリストのさまざまなアイテムに対して、さまざまな空のリストのセット(配列、リストなど)を作成します。
  • このListViewのクリックリスナーを設定し、そのonClickメソッドを実装します。
  • アイテムの位置に基づくonClickメソッドで、対応する空のリスト(array、listなど)を使用してアダプターを作成し、それを現在のリストビューのアダプターとして設定します。
  • 次に、このリストにアイテムを追加し、リストアダプタでnotifyDataSetChanged()を呼び出して、現在追加されているアイテムでリストを更新します。
于 2012-08-31T17:12:08.103 に答える
0

別の投稿で自分の答えを見つけたようです。私は間違った場所を探していました。これは、ListViewなしで実装する方法へのリンクですextends ListViewActivity

Android listActivity を拡張せずに listView のアダプターを使用する方法

于 2012-08-31T16:49:03.163 に答える