gpt4 book ai didi

android - 替换、更新和保存 ListView Android 的文本文件

转载 作者:行者123 更新时间:2023-11-29 17:54:26 25 4
gpt4 key购买 nike

我正在尝试更新我的 ListView 文本文件,方法是替换我刚刚从第二个 Activity 返回时编辑的单词。目前我得到的输出是我刚刚在 onActivityResult() 的变量 name 中编辑的词的 Toast

我的应用程序如何工作。

  1. 点击 ListView 中的项目
  2. 打开 Activity 2,点击 EditText 中的项目
  3. 编辑项目并按保存返回第一个 Activity
  4. 编辑的项目返回变量 name 并显示在弹出窗口中(toast)

我想用旧项目替换我刚刚编辑的项目并保存它,这样当我重新打开应用程序时,更新/编辑的项目就会取代旧项目。

我觉得好像我有完成这个的部分,但我才刚刚开始 android 开发,所以我花了很长时间才弄明白。我想知道是否有人可以引导我朝着正确的方向前进。

这是我的 Activity :

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();
}
}
}

如果需要,我也会发布我的第二个 Activity ,但我认为没有必要。不管怎样,你都会得到!

最佳答案

您提出的解决方案有点基础,如果要解决您提到的问题,确实会限制您。我的意思是 Adapter你正在为你的 ListView 使用仅由简单的 ArrayList<String> 组成,这使您无法知道哪个元素是 name您正在寻找。

更好的解决方案是创建您自己的 Adapter每个元素都有一个特殊的键或其他东西,但恐怕现在对你来说有点太复杂了。请记住,创建自定义 Adapter 是可能的,而且通常非常有用。不过。

我认为在你的情况下可能是一个小技巧,但只有当你完全确定 name 时它才有效。总是存储在第 n 个位置,比方说在第 10 个位置。

然后你可以这样做:

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, );
}

我没有测试过,所以你必须自己做。另外,如果您发现我的回答有点复杂,请随时询问任何问题。

关于android - 替换、更新和保存 ListView Android 的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20493488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com