2012-12-24 47 views
0

可能重複:
Android Replace StringsAndroid的列表視圖替換字符串

下面是我的列表視圖。數據從SD卡收集並列在列表中。但從SD卡檢索到的文件名以「.txt」結尾如何從列出的文件名中刪除「.txt」?

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); 
        } 
       }); 
+3

不是它解決了這裏http://stackoverflow.com/q/14018950/1626878 –

回答

1

替換從文件名".txt",當你檢索,然後將其設置在列表中。

for(int i=0; i< list.length; i++) 
    { 
     String fileName = list[i].getName(); 
     if(fileName.contains(".txt")){ 
       fileName = fileName.replace(".txt", ""); 
     } 
     myList.add(fileName); 
     } 
0

嘗試下面的代碼,它必須幫助你

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

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

     myList.add(list[i].getName()); 
    } 
0

myList.add(名單[I] .getName()的replaceAll( 「\ TXT $」, 「」));