-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);
                    }
                });
1

4 Answers 4

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 );
      }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.