データベースから取り込まれたリストビューがあり、onItemLongClick には、リストの選択したアイテムを編集および更新するために使用するコンテキスト メニューがあります。問題は、選択したアイテムの値を取得する方法がわからないため、編集テキストを含むフォームを持つ別のアクティビティでそれらを使用することです。新しいアクティビティが開始されたときに、これらの文字列値が適切な編集テキストにあることを望みます。onItemLongClick コードは次のとおりです。
**REMOVED AND ADDED CORRECT CODE BELOW**
カーソルを使用して、選択したアイテムの値を取得し、文字列 nameOfSong および artistOfSong に格納します。アクティビティがそれぞれの編集テキストで埋められ始めたら、どうすれば達成できますか?
==編集== 私はこのように動作するSharedPreferencesを使用しました:
最初のアクティビティの OnItemLongClick:
public boolean onItemLongClick(AdapterView<?> listview, View arg1,
final int position, final long arg3) {
//bind context menu to listview' s onIitemLongClick(touch and hold)
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listview.getItemAtPosition(position);
// Get the state's capital from this row in the database.
registerForContextMenu(listView);
openContextMenu(listView);
String nameOfSong =
cursor.getString(cursor.getColumnIndexOrThrow("songname"));
String artistOfSong =
cursor.getString(cursor.getColumnIndexOrThrow("artist"));
SharedPreferences.Editor editor = prefs.edit();
editor.putString("key1", nameOfSong);
editor.putString("key2", artistOfSong);
editor.commit();
2 番目のアクティビティ コード:
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.update_item);
etEditSong = (EditText) findViewById(R.id.etEditSong);
etEditArtist = (EditText) findViewById(R.id.etEditArtist);
sEditDance = (Spinner) findViewById(R.id.sUpdDanceList);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
etEditSong.setText(prefs.getString("key1", null));
etEditArtist.setText(prefs.getString("key2", null));
}
今、2 番目のアクティビティで文字列を取得しています。上記の editTexts 値からデータベース行を更新する方法を見つける必要があります。