2011-12-15 3 views
1

일부 줄로 구성된 일반 텍스트 파일이 있습니다. 각 줄 끝에 쉼표 (,)를 삽입합니다. 또한 텍스트 파일을 읽고 ListView에 설정할 수 있지만 잘 모르겠습니다. ListView에 쉼표로 끝나는 줄로 새 항목을 만들려면 어떻게해야합니까? (I은 단일 항목에서 한 줄을 보여주는 의미.)listView에서 텍스트 파일의 데이터를 표시하는 방법

내가 텍스트 같은 파일을했습니다 :

Test 1 , 
Test 2 , 
Test 3 , 

그리고이 내 ListActity을 :

public class MainMenuActivity extends ListActivity { 
    public String[] ListItems = new String[]{}; 
    @Override 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
     try { 
    Resources ResFiles = getResources(); 
     InputStream ReadDbFile = ResFiles.openRawResource(R.raw.test); 
     byte[] Bytes = new byte[ReadDbFile.available()]; 
     ReadDbFile.read(Bytes); 
     String DbLines = new String(Bytes); 
     ListItems= new String[]{DbLines}; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      R.layout.main_list, R.id.ListText, ListItems); 
     setListAdapter(adapter); 
     } catch (Exception e) { 
    } 
} 

ListView 그냥 날을 모두 보여주는 단일 항목의 줄, 어떤 생각?

답변

4

ListItems= new String[]{DbLines}; 한 항목의 배열을 제공합니다! ;-)

1
String DbLines = new String(Bytes); // One string containing all the file 
ListItems= new String[]{DbLines}; // an array containing the previous string 

가 왜 ListView에 여러 항목을 표시 기대보다

DbLines.split(",")가 될 수 있을까요? 이 같은 문자열 배열에 파일 내용을 분할하려고 : 또한 사용하여 분할 할 수 있습니다

public class MainMenuActivity extends ListActivity { 
    public String[] ListItems = new String[]{}; 
    @Override 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
     try { 
    Resources ResFiles = getResources(); 
     InputStream ReadDbFile = ResFiles.openRawResource(R.raw.test); 
     byte[] Bytes = new byte[ReadDbFile.available()]; 
     ReadDbFile.read(Bytes); 
     String DbLines = new String(Bytes); 
     ListItems = DbLines.split(","); // Split the content by "," 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      R.layout.main_list, R.id.ListText, ListItems); 
     setListAdapter(adapter); 
     } catch (Exception e) { 
    } 
} 
0

: 결국

ListItems = DbLines.split(","); 

것은

DbLines.append("\r\n"); //Splits by NL 

Test 1 
Test 2 
Test 3 

이 더 적합했다 내 코드와 당신을 위해있을 수 있습니다. 나는 .lineseperator 방법이 있다고 생각하지만 이것은 나를 위해 일합니다.

관련 문제