ListView没有填充数据

时间:2019-09-04 14:37:30

标签: java android listview hashmap

在此应用程序中,数据被插入到edittext中。然后,当您按Enter键时,应用程序将检查插入的数据是否存在于文本文件中。如果是,将拆分文本文件中的信息并将其添加到列表视图。

主要操作有效,但列表视图未填充数据。

我尝试将listView.setAdapter(adapter)onCreate方法更改为enterKey侦听器内部。当我这样做时,列表视图将被填充,但始终会使用相同的数据。

public class Main4Activity extends AppCompatActivity{

    private static final String TAG = "Main4Activity";

    ListView listView;

    Boolean scanOpenStatus;

    EditText editText4;

    SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        editText4 = (EditText) findViewById(R.id.editTextCodigo);

        listView = findViewById(R.id.listViewXML);
        final List<HashMap<String, String>> listagem = new ArrayList<>();
        adapter = new SimpleAdapter(this, listagem, R.layout.layoutlist, new String[] {"Principal", "Sub"}, new int[] {R.id.textViewListViewMain, R.id.textViewListViewSub});
        final HashMap<String, String> dados = new HashMap<>();
        listView.setAdapter(adapter);
        listView.setFocusable(false);
        listView.setItemsCanFocus(false);

        editText4.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                boolean check = false;

                if ((keyCode == KeyEvent.KEYCODE_ENTER) &&
                        (event.getAction() == KeyEvent.ACTION_DOWN)) {

                    String codigoLido = editText4.getText().toString().trim();
                    editText4.setText("");

                    if (!codigoLido.equals("")) {
                        try {

                            File caminhoTxt = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/lookupscangs1.txt");

                            if (caminhoTxt.exists()) {

                                try {

                                    String line;
                                    FileReader fileReader = new FileReader(caminhoTxt);
                                    BufferedReader buffer = new BufferedReader(fileReader);

                                    while ((line = buffer.readLine()) != null) {
                                        String[] codDesc = line.split(";");
                                        String codigo = codDesc[0];
                                        String descricao = codDesc[1];

                                        if (codigo.equals(codigoLido)) {

                                            dados.put("Principal", codigoLido);
                                            dados.put("Sub", descricao);
                                            listagem.add(dados);
                                            check = true;
                                            break;
                                        }
                                    }

                                    dados.put("Principal", codigoLido);
                                    dados.put("Sub", "Código Não Cadastrado!");
                                    listagem.add(dados);
                                    check = true;
                                }

                                catch (Exception e) {
                                    Toast.makeText(getApplicationContext(), "Catch 01: " + (CharSequence) e, Toast.LENGTH_SHORT).show();

                                }

                            }

                        } catch(Exception e){
                            Toast.makeText(getApplicationContext(), "Catch 02: " + e, Toast.LENGTH_SHORT).show();
                        }
                    }
                    else {
                        Toast.makeText(getApplicationContext(), "Insira um Código!", Toast.LENGTH_SHORT).show();
                        check = false;
                    }
                }
                editText4.requestFocus();
                return check;
            }

        });

    }

}

2 个答案:

答案 0 :(得分:1)

您是否尝试过手动更新显示内容

dummyData = c(5,8, 9, 25, 30, 55, 55)

dummyData

sort(dummyData,TRUE)
[1] 55 55 30 25  9  8  5

答案 1 :(得分:1)

两种解决方法,要么添加

adapter.notifyDataSetChanged();

adapter = new SimpleAdapter(this, listagem, R.layout.layoutlist, new String[] {"Principal", "Sub"}, new int[] {R.id.textViewListViewMain, R.id.textViewListViewSub};listView.setAdapter(adapter);

在底部的try块中。