gpt4 book ai didi

Android notifyDataSetChanged 删除后不刷新项目

转载 作者:行者123 更新时间:2023-11-29 01:02:32 25 4
gpt4 key购买 nike

所以我试图删除 itempending 但是它不会在单击时立即将其从 ListView 中删除。我必须返回并返回此屏幕,它将被删除。但是我在同一屏幕上的其他 ListView 立即更新(提交的配置文件方法)。我试图创建一个方法来执行我需要的相同功能,并在 notifyDataSetChanged 之前调用它,但没有任何效果。任何关于为什么我的 notifyDataSetChanged 不工作的建议将不胜感激。

ArrayAdapter<String> adapterSubmit, adapterPending;
ArrayList<String> itemsSubmit, itemsPending;
ListView lstSubmitPro, lstPendingPro;
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/CaptureLogs";
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_pro_list);
btnSubmit = (Button) findViewById(R.id.btnTest);
lstSubmitPro = (ListView) findViewById(R.id.lstSubmitPro);
lstPendingPro = (ListView) findViewById(R.id.lstPendingPro);
itemsSubmit = new ArrayList<String>();
adapterSubmit = new ArrayAdapter(this, R.layout.prolist, R.id.tvRows, itemsSubmit);
lstSubmitPro.setAdapter(adapterSubmit);
itemsPending = new ArrayList<String>();
adapterPending = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, itemsPending);
lstPendingPro.setAdapter(adapterPending);
lstPendingPro.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
pendingSubmitProfile();
SubmittedProfile();
btnSubmit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
SparseBooleanArray sp = lstPendingPro.getCheckedItemPositions();
if (sp!= null) {
for (int i = 0; i < sp.size(); i++) {
if (sp.valueAt(i) == true) {

SubmittedProfile();
adapterSubmit.notifyDataSetChanged();

itemsPending.remove(sp.get(i));
adapterPending.notifyDataSetChanged();
Toast.makeText(ProList.this, "Your profiles have been submitted successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ProList.this, "Please choose a profile to be submitted.", Toast.LENGTH_LONG).show();
}
}
}
}

});

public void pendingSubmitProfile()
{
File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = "--PENDING SUBMIT--";
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
String filen = f.getName().substring(0, f.getName().lastIndexOf("."));
itemsPending.add(filen);

}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}




public void SubmittedProfile()
{
File dir = new File(path);
File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(f));
String lineToRead = "--SUBMITTED--";
String CurrentLine;
while ((CurrentLine = inputStream.readLine()) != null) {
if (CurrentLine.equals(lineToRead)) {
String filen = f.getName().substring(0, f.getName().lastIndexOf("."));
itemsSubmit.add(filen);

}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

日志

之前,2 个项目但 1 个检查

05-02 08:00:02.409 4293-4293/com.example.irambi.irmobilewizard D/returned value:: 2 1 2

之后,2 个项目但 1 个检查

05-02 08:00:03.726 4293-4293/com.example.irambi.irmobilewizard D/returned value:: 0 1 0

这是针对 1 件商品和 1 件商品的。我不知道这是之前还是之后,因为 logcat 上只打印了 1 个。这会崩溃,错误如下

05-02 08:03:35.345 4293-4293/com.example.irambi.irmobilewizard D/returned value:: 1 1 1

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

之前,2 个项目但 2 个检查

05-02 08:07:56.378 4596-4596/com.example.irambi.irmobilewizard D/returned value:: 2 2 2

之后,检查了 2 个项目但有 2 个 - 这会崩溃

05-02 08:07:57.671 4596-4596/com.example.irambi.irmobilewizard D/returned value:: 0 2 0
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0

所有的测试都是使用

itemsPending.remove(sp.keyAt(i));
adapterPending.remove(adapterPending.getItem(sp.keyAt(i)));

日志基于:

Log.d("returned value:", itemsPending.size() + " " + sp.size()  + " " + adapterPending.getCount());

最佳答案

试试这个

itemsPending.remove(sp.keyAt(i));
adapterPending.remove(adapterPending.getItem(sp.keyAt(i)));
adapterPending.notifyDataSetChanged();

编辑:

所以基本上列表数据的切换工作正常。搞乱代码的是他的文件编写。我是这样解决的。

首先是创建一个函数,将我的待处理文件更新为已提交。

public void submitPendingProfile(String filename){
try {
BufferedReader file = new BufferedReader(new FileReader(path + "/" + filename+".txt"));
String line;
StringBuffer inputBuffer = new StringBuffer();

while ((line = file.readLine()) != null) {
inputBuffer.append(line);
inputBuffer.append('\n');
}
String inputStr = inputBuffer.toString();

file.close();


inputStr = inputStr.replace("--PENDING SUBMIT--", "--SUBMITTED--");

FileOutputStream fileOut = new FileOutputStream(path + "/" + filename+".txt");
fileOut.write(inputStr.getBytes());
fileOut.close();

} catch (Exception e) {
System.out.println("Problem reading file.");
}
}

然后我重构循环以简化流程。就像删除 SubmittedProfile(); 行一样,如果条件为真,它会继续读取所有文本文件。这是很多过程。以下是方法。

for(int i = lstPendingPro.getAdapter().getCount() - 1 ; i >= 0; i--) {
if (sp.get(i)) {
//So when file is submitted, i update the files status using the above function.
submitPendingProfile(itemsPending.get(i));

//To avoid rereading of files, just add the item before removing it to the pending list
itemsSubmit.add(itemsPending.get(i));
adapterSubmit.notifyDataSetChanged();

itemsPending.remove(sp.keyAt(i));
adapterPending.notifyDataSetChanged();
Toast.makeText(ProList.this, "Your profiles have been submitted successfully.", Toast.LENGTH_LONG).show();
}
}

关于Android notifyDataSetChanged 删除后不刷新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127533/

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