【问题标题】:Getting java.io.FileNotFoundException获取 java.io.FileNotFoundException
【发布时间】:2015-04-23 04:56:29
【问题描述】:

我在资产文件夹的子文件夹中有四个文件。我已经写了这段代码

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
    }
    final TextView textView1 = (TextView) convertView.findViewById(R.id.textView1);
    final TextView textView2 = (TextView) convertView.findViewById(R.id.textView2);
    final Button show = (Button) convertView.findViewById(R.id.show1);
    final Button hide = (Button) convertView.findViewById(R.id.hide1);

    textView1.setText(data[position]);

    show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            show.setVisibility(View.INVISIBLE);
            hide.setVisibility(View.VISIBLE);

            try{

                Resources resources = getResources();
                AssetManager assetManager = resources.getAssets();
                String fileList[] = assetManager.list("latest_trials");

                if (fileList != null){
                    for (int i = 0; i < fileList.length; i++){
                        Log.v("Files", fileList[i]);
                    }
                }

                InputStream inputStream;
                    try{
                        inputStream = assetManager.open(fileList[position]);
                        byte[] buffer = new byte[inputStream.available()];
                        inputStream.read(buffer);

                        String value = new String(buffer);
                        textView2.setMaxLines(Integer.MAX_VALUE);
                        textView2.setVisibility(View.VISIBLE);
                        textView2.setText(value);
                    }catch (IOException e){
                        e.printStackTrace();
                    }

            }catch (IOException e){
                e.printStackTrace();
            }


        }
    });

所有文件都正确列出,并希望在 listview 中的 textview 中显示文件内容,但不读取文件并显示 FileNotFoundException。请有人帮忙..

【问题讨论】:

  • 这段代码在自定义列表适配器的getview方法中。
  • 请贴出完整代码。问题可能与位置变量的值有关

标签: android android-assets


【解决方案1】:

一旦您的 getview 结束,则位置值将成为最后一个值。如果单击,它将始终指向最后一个值。 所以用setTag()的按钮标记位置并通过getTag()在onclick中获取它

只需调试并检查,fileList[] 的最后一个值是多少。

做这样的事情-

show.setTag(position)
 show.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         int pos = Integer.parseInt(v.getTag());

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2017-01-01
    • 2013-04-21
    • 2014-08-31
    相关资源
    最近更新 更多