-1

現在、私はカスタムインプットに表示するインプットインテントのインテントデータを送信しようとしています。 detailspage.javaクラスからのユーザー入力のインテント・データは、カスタムArrayAdapterを使用してリスト・ビュー・ページに送られます。しかし、私は新しい項目を追加するたびに、前の項目を上書きします。いくつかの種類の魂が誰かをアンドロイドの新しい人を助けることを願っています。新しいarraylistアイテムが以前のリストビューアイテムを上書きします

以下は私のコードのスニペットです。

// Set a click listener on that button 
    btnAddData.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      saveExercise(exerciseName); 
     } 
    }); 
} 

private void saveExercise(String name) { 
    // Read from input fields 
    // Use trim to eliminate leading or trailing white space 
    String setString = mSetEditText.getText().toString().trim(); 
    String repString = mRepEditText.getText().toString().trim(); 
    String nameString = name; 


    Intent intent = new Intent(ExerciseDetailActivity.this, WorkoutSetActivity.class); 

    intent.putExtra("name", nameString); 
    intent.putExtra("set", setString); 
    intent.putExtra("rep", repString); 

    startActivity(intent); 

} 

インテントデータがアクティビティページに送信され、カスタムArrayAdapterが開始されます。

public class WorkoutSetActivity extends AppCompatActivity { 

ArrayList<WorkoutSet> workout = new ArrayList<WorkoutSet>(); 

/** Adapter for the ListView */ 
WorkoutSetAdapter adapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.exercise_list); 

    adapter = new WorkoutSetAdapter(this, workout); 
    ListView listView = (ListView) findViewById(R.id.list); 
    listView.setAdapter(adapter); 

    Intent intent = this.getIntent(); 
    String name = intent.getExtras().getString("name"); 
    String set = intent.getExtras().getString("set"); 
    String rep = intent.getExtras().getString("rep"); 

    adapter.add(new WorkoutSet(name,set,rep)); 
    Toast.makeText(WorkoutSetActivity.this, "Workout added.", Toast.LENGTH_SHORT).show(); 
    adapter.notifyDataSetChanged(); 
} 
} 

最後にカスタムArrayAdapterコード。ここで私はカスタムリストビューを設定します。

public class WorkoutSetAdapter extends ArrayAdapter<WorkoutSet> { 


public WorkoutSetAdapter(Activity context, ArrayList<WorkoutSet> workout) { 
    super(context, 0, workout); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    // Check if the existing view is being reused, otherwise inflate the view 
    View listItemView = convertView; 
    if (listItemView == null) { 
     listItemView = LayoutInflater.from(getContext()).inflate(
       R.layout.activity_workout_set, parent, false); 
    } 

    WorkoutSet currentWorkout = getItem(position); 

    // Find individual views that we want to modify in the list item layout 
    TextView nameTextView = (TextView) listItemView.findViewById(R.id.workout_name); 
    TextView setTextView = (TextView) listItemView.findViewById(R.id.workout_set); 
    TextView repTextView = (TextView) listItemView.findViewById(R.id.workout_rep); 

    nameTextView.setText(currentWorkout.getName()); 
    setTextView.setText("Sets: "+ currentWorkout.getSet()); 
    repTextView.setText("Reps: "+ currentWorkout.getRep()); 

    return listItemView; 
} 
} 
+0

いくつかの永続的なデータストレージを使用する必要があります。 – Selvin

答えて

0

私は新しい項目を追加するたびに、それには、「以前のエントリは」ありません、私の以前のエントリ

を上書きします。 startActivityは毎回空のアダプターをロードし、1つの要素だけを追加します。

SQLite database(または任意の永続ストレージレイヤーですが、SQLiteはライブラリなしで提供されます)。


プラス側では、意図とアダプタコードが正常に表示されます。

+0

しばらくお待ちいただき、ありがとうございます。週にデータベースを実装しようとしましたが、いくつか問題がありました。あなたは時間を持っている場合それを見てください。本当に助けを感謝します:) http://stackoverflow.com/questions/42329936/inserting-data-into-sqlite-and-displaying-into-listview?noredirect=1#comment71812487_42329936 –

0

startActivity()を呼び出すたびに新しいWorkoutSetActivityを開きます。これは新しいWorkoutSetActivityオブジェクトインスタンスです。したがって、arrayAdapterも新しいものです。今回送信したデータのみが表示されます。

すべてのデータを表示する場合は、以前のデータをデータベースまたはファイルに保存する必要があります。 WorkoutSetActivity onCreate()では、以前のデータを取得し、それらのデータと現在のデータをアダプタに渡す必要があります。

関連する問題