从资源文件夹中读取.txt文件

时间:2016-05-03 06:35:09

标签: android android-recyclerview assets

我正在通过传递上一个活动列表项中的字符串从Asset文件夹中读取.txt文件。

让我们考虑一下,Recyclerview项目大小为5。

  1. Android - 第1项
  2. IOS - 第2项
  3. BlackBerry - 第3项
  4. Symbian - 第4项
  5. Bada - 第5项
  6. 当我点击Android时,“Android”字符串将通过Intent和 分配给 歌曲 的变量。

     InputStream inputstream = getResources().getAssets().open(***Song*** + ".txt");
    

    并从资源文件夹中读取Android.txt文件。只调用第一个项目。当我单击列表中的其他项时,intent字符串传递给下一个活动,但没有从文件夹中打开相应的.txt文件。它跳了起来。

    这是我的代码。

     Intent i = getIntent();
            Song = i.getExtras().getString("Title");
            textView.setText(Song);
            System.out.println(".....1");
            try {
                System.out.println(".....2");
                InputStream inputstream = getResources().getAssets().open(Song + ".txt");
                System.out.println(".....3");
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream));
                String m = reader.toString();
                StringBuilder total = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    total.append(line);
                }
                System.out.println(".....4");
                m = total.toString();
                System.out.println(".....5" + m);
    
                textView.setText(m);
                textView.setTextColor(Color.BLACK);
                System.out.println(".....6");
            } catch (IOException ex) {
                System.out.println(".....7" + ex);
            }
    

    System.out.println --->打印第1项中的所有项目单击

    除了点击第1项外,在LogCat中仅打印以下内容 System.out:..... 1
    System.out:..... 2

    我点击RecyclerView项目

    的适配器类
    public class SongAdapter extends RecyclerView.Adapter<SongAdapter.ViewHolder> {
        Context con;
    
        ArrayList<String> songHeading = new ArrayList<>();
        SharedPreferences pref;
        String MyPREFERENCES = "Preference";
    
    
        public SongAdapter(Context context, ArrayList<String> heading) {
            this.con = context;
            this.songHeading = heading;
        }
    
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_list_item, parent, false);
            ViewHolder viewHolder = new ViewHolder(v);
    
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
    
            pref = con.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
            holder.txtSong.setText(songHeading.get(position));
    
            holder.Clicker.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(view.getContext(), MainActivity.class);
                    i.putExtra("Title", songHeading.get(position));
                    view.getContext().startActivity(i);
                }
            });
        }
    
    
        @Override
        public int getItemCount() {
            return songHeading.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            public final View mView;
            public TextView txtSong;
            LinearLayout Clicker;
    
            public ViewHolder(View itemView) {
                super(itemView);
                mView = itemView;
                txtSong = (TextView) itemView.findViewById(R.id.textView);
                Clicker = (LinearLayout) itemView.findViewById(R.id.Clicker);
            }
        }
    }
    

    更新:

    更改代码后,如下所示获取详细错误

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.song_list_item2);
            context = this;
    
            pref = this.context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
            textView = (TextView) findViewById(R.id.textView);
    
            Intent i = getIntent();
            Song = i.getExtras().getString("Title");
            textView.setText(Song);
            System.out.println(".....1");
            textView.setText(loadJSONFromAsset());
        }
    
        private String loadJSONFromAsset() {
            String json = null;
            try {
    
                InputStream is = getAssets().open(Song + ".txt");
    
                int size = is.available();
    
                byte[] buffer = new byte[size];
    
                is.read(buffer);
    
                is.close();
    
                json = new String(buffer, "UTF-8");
    
            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }
            return json;
        }
    

    at hitesh.androidjsonapp.MainActivity.loadJSONFromAsset(MainActivity.java:40)指出这一行

       InputStream is = getAssets().open(Song + ".txt");
    

    enter image description here

1 个答案:

答案 0 :(得分:0)

确保文件名不附加任何特殊字符(\ n)或空格,并使用try..catch添加finally块,您需要关闭输入流

13588-13588/hitesh.androidjsonapp I/System.out: .....1 \n BlackBerry