Android notifyDataSetChanged 删除后不刷新项目
Android notifyDataSetChanged not refreshing the items after removing
所以我试图删除 itempending 但是它不会在单击时立即从列表视图中删除它。我必须返回并返回此屏幕,它将被删除。但是我在同一屏幕上的其他列表视图立即更新(提交的配置文件方法)。我试图创建一个方法来执行我需要的相同功能,并在 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());
您是否尝试过将代码更改为该订单
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();
itemsPending.remove(sp.get(i));
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();
}
}
}
adapterSubmit.notifyDataSetChanged();
adapterPending.notifyDataSetChanged();
}
});
试试这个
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();
}
}
试试这个代码..
itemsPending.remove(itemsPending.indexOf(sp.get(i)));
adapterPending.notifyDataSetChanged();
itemsPending.remove(sp.get(i));
adapterPending.notifyDataSetChanged();
listview.invalidate();
尝试上面的代码可能会对您有所帮助。
所以我试图删除 itempending 但是它不会在单击时立即从列表视图中删除它。我必须返回并返回此屏幕,它将被删除。但是我在同一屏幕上的其他列表视图立即更新(提交的配置文件方法)。我试图创建一个方法来执行我需要的相同功能,并在 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());
您是否尝试过将代码更改为该订单
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();
itemsPending.remove(sp.get(i));
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();
}
}
}
adapterSubmit.notifyDataSetChanged();
adapterPending.notifyDataSetChanged();
}
});
试试这个
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();
}
}
试试这个代码..
itemsPending.remove(itemsPending.indexOf(sp.get(i)));
adapterPending.notifyDataSetChanged();
itemsPending.remove(sp.get(i));
adapterPending.notifyDataSetChanged();
listview.invalidate();
尝试上面的代码可能会对您有所帮助。