Android notifyDataSetChanged删除后没有刷新项目

时间:2018-05-02 05:14:40

标签: android listview sparse-matrix notifydatasetchanged

所以我试图删除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();
            }
        }
    }
}

LOG

之前,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项已检查。我不知道这是在之前还是之后,因为只有1只印在logcat上。这将崩溃,错误如下

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

4 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

您是否尝试过将代码更改为此订单

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

});

答案 2 :(得分:0)

试试这段代码..

    itemsPending.remove(itemsPending.indexOf(sp.get(i)));
    adapterPending.notifyDataSetChanged();

答案 3 :(得分:0)

itemsPending.remove(sp.get(i));
adapterPending.notifyDataSetChanged();
listview.invalidate();

尝试以上代码可能会对您有所帮助。