【发布时间】: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方法中。
-
请贴出完整代码。问题可能与位置变量的值有关