我正在开展一项学校作业,即创建一个包含2个活动的书签应用程序,一个名为BookNote的ListActivity和一个名为ManageActivity的活动。
所有输入必须在ManageActivity中完成,并传递回BookNote以进行列表更改,读取和写入数据。
我的问题是当我在BookNote活动中调用函数“addBookmark”时。我想要发生的是在列表的末尾添加一个新的列表项,其中包含书签对象信息。
正在发生的事情是,列表末尾的任何项目都会被覆盖而不是正在创建的新项目。
我的代码在下面,注释表明了我对每个功能的意图。
BookNote活动
public class BookNote extends ListActivity{
private ArrayAdapter<Bookmark> adapter;
private final String FILENAME = "bookmarks.txt";
public String title = "EMPTY", url = "EMPTY", note = "EMPTY";
public String bookmarkClicked ="";
public int listViewID = 0;
public boolean intentListener = false;
public int intentReturnCounter = 0;
public boolean addBookmarkClicked = false;
public ArrayList<Bookmark> bookmarkList;
/*When activity is called, list is populated by readData(), addBookmarkClicked is reset to false, intentLister is changed to true if returning from ManageActivity,
if intentListener is true, addBookmarkClicked will be changed to true if AddBookmark is clicked from the BookNote menubar, a bookmark object will be created with information
passed back from ManageActivity, and if addBookmarkClicked is true, the bookmark object will be added to the end of the list, otherwise it will be inserted in the same
list element from which was chosen to edit.*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bookmarkList = readData();
adapter = new ArrayAdapter<Bookmark>(this, android.R.layout.simple_list_item_1, bookmarkList);
setListAdapter(adapter);
addBookmarkClicked = false;
intentListener = false;
intentListener = getIntent().getBooleanExtra("listen", false);
if (intentListener == true) {
addBookmarkClicked = getIntent().getExtras().getBoolean("addBookmarkClicked", false);
title = getIntent().getExtras().getString("title");
url = getIntent().getExtras().getString("url");
note = getIntent().getExtras().getString("note");
listViewID = getIntent().getExtras().getInt("listViewID");
Bookmark bookmark = new Bookmark(title, url, note);
Toast.makeText(getApplicationContext(), "Intent return: True", Toast.LENGTH_SHORT).show();
intentListener = false;
if (addBookmarkClicked == true) {
addBookmark(bookmark);
Toast.makeText(getApplicationContext(), "Bookmark Added", Toast.LENGTH_SHORT).show();
addBookmarkClicked = false;
} else {
insertBookmark(bookmark, listViewID);
Toast.makeText(getApplicationContext(), "Bookmark Edited", Toast.LENGTH_SHORT).show();
}
//writeData();
}
}
//reads data when app runs and fills arrayadapter and list with items saved to bookmarks.txt
private ArrayList<Bookmark> readData(){
ArrayList<Bookmark> bookmarks = new ArrayList<>();
try {
FileInputStream fis = openFileInput(FILENAME);
Scanner scanner = new Scanner(fis);
if (scanner.hasNext()){
String titleScan = scanner.nextLine();
String urlScan = scanner.nextLine();
String noteScan = scanner.nextLine();
Bookmark bookmark = new Bookmark(titleScan, urlScan, noteScan);
bookmarks.add(bookmark);
}else{
Bookmark bookmark = new Bookmark("Example Title", "Example URL", "Example Note");
bookmarks.add(bookmark);
}
scanner.close();
} catch (FileNotFoundException e) {
}
return bookmarks;
}
private void writeData(){
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw);
for(int i = 0; i < adapter.getCount(); i++){
Bookmark bookmark = adapter.getItem(i);
pw.println(bookmark.getTitle() + "\n" + bookmark.getUrl() + "\n" + bookmark.getNote());
}
pw.close();
} catch (FileNotFoundException e) {
Log.e("Write ERR", "Cannot save: " + e.getMessage());
e.printStackTrace();
Toast.makeText(BookNote.this, "Error saving", Toast.LENGTH_SHORT).show();
}
}
//If addBookmark menu item is clicked, this will be called to add a bookmark to the end of the ArrayAdapter.
private void addBookmark(Bookmark bookmark){
adapter.add(bookmark);
writeData();
}
//Calls ManageActivity and reports information about app.
public void gotoManageActivity(boolean addBookmarkClicked){
Intent manageIntent = new Intent(this, ManageActivity.class);
Bundle extras = new Bundle();
extras.putString("bookmark", bookmarkClicked);
extras.putBoolean("addBookmarkClicked", addBookmarkClicked);
extras.putInt("listViewID", listViewID);
manageIntent.putExtras(extras);
startActivity(manageIntent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
addBookmarkClicked = true;
gotoManageActivity(addBookmarkClicked);
return true;
}
return super.onOptionsItemSelected(item);
}
}
ManageActivity
public class ManageActivity extends AppCompatActivity {
private String title = "EMPTY", url = "EMPTY", note = "EMPTY";
private boolean listener = true;
private boolean addBookmarkClicked = false;
/* When activity starts, check for addBookmarkClicked status, create book */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manage_layout);
addBookmarkClicked = getIntent().getExtras().getBoolean("addBookmarkClicked", false);
String bookmarkString = "";
bookmarkString = getIntent().getExtras().getString("bookmark");
if(bookmarkString != null && bookmarkString.length() > 0) {
if (bookmarkString.length() != 0) {
String[] stringArray = bookmarkString.split("\\n");
title = stringArray[0];
url = stringArray[1];
note = stringArray[2];
updateTextViews();
}
}
else { updateTextViews();
}
}
@Override
public void onBackPressed(){
Intent bookNoteIntent = new Intent(this, BookNote.class);
Bundle extras = new Bundle();
if(title.length() == 0 || title == null){title= "Empty";}
if(url.length() == 0 || url == null){ url = "Empty";}
if(note.length() == 0 || url == null) { note = "Empty";}
extras.putString("title", title);
extras.putString("url", url);
extras.putString("note", note);
extras.putBoolean("listen", listener);
extras.putBoolean("addBookmarkClicked", addBookmarkClicked);
bookNoteIntent.putExtras(extras);
startActivity(bookNoteIntent);
}
答案 0 :(得分:0)
好问题!几周前我也必须自己处理这个问题。
主要的是您需要重新实例化您的ArrayAdapter
或adapter
。
现在你正确地完成了第一步:
bookmarkList = readData();
adapter = new ArrayAdapter<Bookmark>(this, android.R.layout.simple_list_item_1, bookmarkList);
setListAdapter(adapter);
但是在尝试更新列表时你会再次这样做:我相信你正确地添加了新书签,但是你需要为列表视图重新创建ArrayAdapter。
编辑: 我相信我发现了危险。 就在这里,你需要重新实例化ArrayAdapter,正如我前面提到的那样。
private void addBookmark(Bookmark bookmark){
adapter.add(bookmark);
writeData();
}
基本上,每当您更新要在列表中显示的数据时,您都需要执行此步骤...