-1

Possible Duplicate:
Android Replace Strings

Below is my listview. The data are gathered from the sdcard and listed out on a listview. But the filenames retrieved from the sdcard ends with ".txt" How do I remove the ".txt" from my listed filenames?

mainListView = (ListView) getActivity().findViewById( R.id.mainListView );

            myList = new ArrayList<String>();



                File sdCard = Environment.getExternalStorageDirectory();
                file = new File(sdCard.getAbsolutePath() + "/St/") ;       
                File list[] = file.listFiles();

                for( int i=0; i< list.length; i++)
                {
                        myList.add( list[i].getName() );
                }



                listAdapter = new ArrayAdapter<String>(getActivity(),R.layout.simplerow, myList);

                mainListView.setAdapter( listAdapter );  

                mainListView.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        // TODO Auto-generated method stub
                        String textToPass = myList.get(position);
                                Intent i = new Intent(getActivity(), ViewActivity.class);                      
                                i.putExtra("textToPass", textToPass);
                                startActivity(i);
                    }
                });
Community
  • 1
  • 1
KC Chai
  • 1,607
  • 9
  • 30
  • 59

4 Answers4

0

Replace the ".txt" from the file Name, when you retrieve it and then set it in the list.

 for( int i=0; i< list.length; i++)
     {
         String fileName = list[i].getName();
         if(fileName.contains(".txt")){
              fileName = fileName.replace(".txt", "");
         }
         myList.add( fileName );
      }
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
0

I would use the Java String Substring method.

Take a look here for that and other string methods. http://www.tutorialspoint.com/java/java_strings.htm

BENBUN Coder
  • 4,801
  • 7
  • 52
  • 89
0

Try the below code, It must help you

if(list[i].getName().equalsIgnoreCase(".txt")){

    String[] strrwmoverar=list[i].getName().toString().split("//.");
     myList.add( strrwmoverar[0] );
 }
else{

      myList.add( list[i].getName() );
   }
No_Rulz
  • 2,679
  • 1
  • 20
  • 33
0

myList.add(list[i].getName().replaceAll("\.txt$", ""));

Qiang Jin
  • 4,427
  • 19
  • 16