gpt4 book ai didi

java - 从 ListView 及其文本文件中删除一行

转载 作者:行者123 更新时间:2023-12-01 14:32:16 24 4
gpt4 key购买 nike

我将文本文件的内容加载到 ListView 中。文本文件 (his.his) 包含文本行。现在我想应用上下文菜单来从文本文件中删除列表行和等效的文本行。但我只成功从 ListView 中删除列表行,而无法从文本文件中删除等效的文本行。你能帮忙吗?预先非常感谢。

这是我的完整代码:

public class HistoryView extends Activity {
private static final String History_TAG = "[MyApp - HistoryView] ";
private ListView mLSTHistory = null;
private ArrayList<String> lstDict = null;
private ArrayList<Integer> lstId = null;
private ArrayAdapter<String> aptList = null;
private ArrayList<String> mWordHistory = null;
private String fileLocation= Environment.getExternalStorageDirectory().getPath()+"/his.his";
private SharedPreferences prefs;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (prefs.getBoolean("saveHistory", true))
{
String strHistory = null;
try {
strHistory = new Scanner(new File(fileLocation)).useDelimiter("\\z").next();
} catch (FileNotFoundException e) {
e.printStackTrace(); }

Log.i(History_TAG, "History loaded");
if (strHistory != null && !strHistory.equals(""))
{
mWordHistory = new ArrayList<String>(Arrays.asList(strHistory.split("\n")));
}
else
{
mWordHistory = new ArrayList<String>();
}
}
else
{
mWordHistory = new ArrayList<String>();
}
Log.d(History_TAG,"mWordHistory = " + mWordHistory.size());

mLSTHistory = (ListView) findViewById(R.id.lstHistory);
registerForContextMenu(mLSTHistory);

if (lstDict == null)
{
lstDict = new ArrayList<String>();
lstId = new ArrayList<Integer>();
aptList = new ArrayAdapter<String>(getApplicationContext(),R.layout.customlist);
}
lstDict.clear();
lstId.clear();
aptList.clear();
if (mWordHistory != null && mWordHistory.size() > 0)
{
try
{
for (int i=0; i < mWordHistory.size(); i++)
{
Log.i(History_TAG,"item = " + mWordHistory.get(i));
String arrPart[] = mWordHistory.get(i).split("::");
if (arrPart.length == 3)
{
lstDict.add(i,arrPart[0]);
lstId.add(i,Integer.parseInt(arrPart[1]));
aptList.add(arrPart[2]);
}
else
{
Log.i(History_TAG,"Wrong entry: " + mWordHistory.get(i));
}
}
}
catch (Exception ex)
{
Log.i(History_TAG,"Wrong entry found!");
}
}
mLSTHistory.setAdapter(aptList);

}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {

case R.id.delete:
String content = (String) mLSTHistory.getItemAtPosition(info.position);

aptList.remove(content);

aptList.notifyDataSetChanged();

deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
private void deleteNote(long id) {
//I have the problem here to remove the text line which is populated into the list view
PrintWriter writer = null;
try {
writer = new PrintWriter(fileLocation);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
writer.print("");
writer.close();
}
}

最佳答案

您可以打开文本文件进行写入并再次写入所有行,而不是尝试从文本文件中删除一行 - 除了已删除的记录。迭代您的记录并将每个记录写入文件。

关于java - 从 ListView 及其文本文件中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16777264/

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