-1

I have four file in a subfolder in assets folder. I have written this code

 @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();
            }


        }
    });

All files are listing properly and want to show content of file in textview in listview but not reading files and showing FileNotFoundException. Please some one help..

aquib
  • 194
  • 2
  • 13

1 Answers1

0

Once your getview will over then position value become last value. if you click then it will always point to last value. So Tag the position with button by setTag() and get it in onclick by getTag()

Just debug and check, what is the last value of your fileList[].

do something like this-

show.setTag(position)
 show.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         int pos = Integer.parseInt(v.getTag());
T_V
  • 17,440
  • 6
  • 36
  • 48