我正在尝试更新我的ListView
文本文件,方法是将我刚编辑的单词替换为从第二个活动返回。目前,我得到的输出是我刚刚在Toast
的变量name
中编辑的单词的onActivityResult()
。
我的应用如何运作。
name
中返回,并以弹出式窗口(toast)显示我想用旧项目替换刚编辑的项目并保存,以便当我重新打开应用程序时,更新/编辑的项目将代替旧项目。
我觉得好像我有完成这个的部分,但我刚刚开始Android开发,所以我花了很长时间来弄清楚这一点。我想知道是否有人能引导我朝着正确的方向前进。
这是我的活动:
public class ToDoActivity extends Activity {
private ArrayList<String> todoItems;
private ArrayAdapter<String> todoAdapter; // declare array adapter which will translate the piece of data to teh view
private ListView lvItems; // attach to list view
private EditText etNewItem;
private final int REQUEST_CODE = 20;
//private Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
//populateArrayItems(); // call function
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
//todoAdapter.add("item 4");
setupListViewListener();
setupEditItemListener();
//onActivityResult(REQUEST_CODE, RESULT_OK, );
}
private void launchEditItem(String item) {
Intent i = new Intent(this, EditItemActivity.class);
i.putExtra("itemOnList", item); // list item into edit text
startActivityForResult(i, REQUEST_CODE);
//startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
String text = (String) lvItems.getItemAtPosition(pos);
launchEditItem(text);
}
});
}
private void setupListViewListener() {
lvItems.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View item, int pos, long id) {
todoItems.remove(pos);
todoAdapter.notifyDataSetChanged(); // has adapter look back at the array list and refresh it's data and repopulate the view
writeItems();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.to_do, menu);
return true;
}
public void onAddedItem(View v) {
String itemText = etNewItem.getText().toString();
todoAdapter.add(itemText); // add to adapter
etNewItem.setText(""); //clear edit text
writeItems(); //each time to add item, you want to write to file to memorize
}
private void readItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
todoItems = new ArrayList<String>(FileUtils.readLines(todoFile)); //populate with read
}catch (IOException e) { // if files doesn't exist
todoItems = new ArrayList<String>();
}
}
private void writeItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
FileUtils.writeLines(todoFile, todoItems); // pass todoItems to todoFile
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String name = data.getExtras().getString("EditedItem");
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
//writeItems();
}
}
}
如果需要,我也会发布我的第二个活动,但我认为没有必要。不过请问,你会收到!
答案 0 :(得分:3)
您提出的解决方案有点基础,如果它解决了您提到的问题,那么它真的会限制您。我的意思是,您Adapter
使用的ListView
仅包含简单的ArrayList<String>
,这可以防止您知道您正在寻找哪个元素name
。
更好的解决方案是创建自己的Adapter
,其中每个元素都有一个特殊的键或者其他东西,但是我担心它现在对你来说有点太复杂了。请记住,创建自定义Adapter
是可能的,而且通常非常有用。
我认为在你的情况下可能有点可能,但只有当你完全确定name
始终存储在第n个位置时,它才有效,让我们说在第10个位置。< / p>
然后你可以这样做:
private final int NAME_POSITION = 10;
现在你有了这个位置,你应该在文件中找到第10行,删除它并存储新值。我不会写这段代码,因为它与这个问题没有关系。在Stackoverflow上有很多关于Java文件读/写的问题,如果你还不知道怎么做,你应该很容易找到解决方案。基本上你必须把它放在这个地方:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String name = data.getExtras().getString("EditedItem");
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
// Write your name to file now
}
}
第二种方法是忘记文件并使用SharedPreferences
获取更新的名称:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String name = data.getExtras().getString("EditedItem");
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
SharedPrefrences sp = getSharedPreferences("MySavedValues", 0); // Open SharedPreferences with name MySavedValues
Editor editor = sp.edit();
editor.putString("Name", name); // Store name with key "Name". This key will be then used to retrieve data.
editor.commit();
}
}
和onCreate()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
//populateArrayItems(); // call function
readItems(); // read items from file
SharedPreferences sp = getSharedPreferences("MySavedValues", 0); // Open SharedPreferences with name MySavedValues
String name = sp.getString("Name", ""); // If there isn't any string stored with key "Name", it will return empty string
if(!name.isEmpty()) {
todoItems.set(NAME_POSITION, name);
}
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
//todoAdapter.add("item 4");
setupListViewListener();
setupEditItemListener();
//onActivityResult(REQUEST_CODE, RESULT_OK, );
}
我没有测试过,所以你必须自己做。此外,如果您觉得我的答案有点复杂,请随时询问任何事情。