There is a text file uploaded in the raw folder filled in with a column of the following entries:

abc bac knc 

Is it possible to somehow output data from this file to the listview ?

Help highlights the red line in my code

 View rootView = inflater.inflate(layout.fragment_search, container, false); getActivity().setTitle(R.string.fr2); listView=(ListView) rootView.findViewById(id.lst); editText=(EditText) rootView.findViewById(id.txtsearch); initList(); editText.clearFocus(); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.toString().equals("")){ // Обновление listview initList(); } else { // выполнение поиска searchItem(s.toString()); } } @Override public void afterTextChanged(Editable s) { } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); FragmentTransaction tr = getFragmentManager().beginTransaction(); String currPos = listItems.get(position); if (currPos.equals(textPos0)) { //Создание нового фрагмента и транзакции Fragment Stay1 = new Stay1(); // Замените все, что есть в представлении fragment_container, этим фрагментом, //И добавьте транзакцию в задний стек tr.replace(R.id.container, Stay1); tr.addToBackStack(null); //Завершить транзакцию tr.commit(); } else if (currPos.equals(textPos1)) { Fragment Stay2 = new Stay2(); tr.replace(R.id.container, Stay2); tr.addToBackStack(null); tr.commit(); } else if (currPos.equals(textPos3)){ Fragment Stay3 = new Stay3(); tr.replace(R.id.container, Stay3); tr.addToBackStack(null); tr.commit(); } } }); return rootView; } public List<String> getTextFromRaw() { List<String> arr1 = new ArrayList<>(); try { InputStream inputStream = getResources().openRawResource(R.raw.stm); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = bufferedReader.readLine())!= null) { arr1.add(line); } bufferedReader.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return arr1; } public void searchItem(String textToSearch){ textToSearch = textToSearch.toLowerCase(); for(String item:items){ if(!item.toLowerCase().contains(textToSearch)){ listItems.remove(item); } } adapter.notifyDataSetChanged(); } //передача результатов поиска в listview public void initList(){ List<String> arr1 = getTextFromRaw(); //String [] arr1; //arr1 = getResources().getStringArray(R.array.stops); items=arr1; listItems=new ArrayList<>(Arrays.asList(items)); adapter=new ArrayAdapter<String>(getActivity(), layout.list_item, id.txtitem, listItems); listView.setAdapter(adapter); textPos0 = listItems.get(0); textPos1 = listItems.get(1); textPos3 = listItems.get(9); } items=arr1; вот эту 

    1 answer 1

    An example of how to read data from a file in the raw folder

     public List<String> getTextFromRaw() { List<String> strList = new ArrayList<>(); BufferedReader bufferedReader; try { InputStream inputStream = getResources().openRawResource(R.raw.test); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; int count = 0; while ((line = bufferedReader.readLine()) != null) { strList.add(line); count++; if (count==10){ break; } } bufferedReader.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return strList; } List<String> strList = getTextFromRaw(); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strList); listView.setAdapter(arrayAdapter); 
    • in the line (R.raw.test); highlights the red test - Grohovac
    • @Joel instead of test name of your file - Rasul
    • But with or without the extension? - Grohovac
    • without. With the introduction of R.raw. you should have a list of files in your raw folder - Rasul A-s
    • But if you are still here, how can you output a certain number of lines, let's say the first 10? - Grohovac