2013-09-23 40 views
0

我是Android應用程序開發新手。我嘗試在我的活動中使用Textview查看我的數據庫。將setText()轉換爲數據庫的文本視圖

這裏是我的setText()的Java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_viewlogs); 
TextView tv = (TextView) findViewById(R.id.tvSqlinfo); 
LogsDB info = new LogsDB(this); 
info.open(); 
ArrayList<String> data = info.getData(); 
info.close(); 
tv.setText(data); 
} 

我似乎在tv.setText(數據)是越來越和錯誤;.指出「在類型TextView的方法的setText(CharSequence的)是不適用的參數(ArrayList的)」然後,當我這樣做推薦修復它改變

tv.setText(data) 

tv.setText((CharSequence) data); 

然後當我測試該應用程序我收到一個錯誤,指出它不能被轉換。 我需要更改以便能夠在textview中查看我的數據庫?

任何意見和幫助,將不勝感激。 感謝

+0

你可以使用data.toArray()。的toString() – nedaRM

+0

'tv.setText(數據)'不適用於字符串數組?你不能將列表強制轉換爲CharSequence對象?您可以在數據列表的推移,一些字符串'​​tv.setText(data.get(0))'0是列表的指數 – Husam

+0

太累了,但現在在數據庫中的值應該是它說「[email protected] 「 – ba2210

回答

1

如果你想保持它的簡單,你可以使用

tv.setText(data.toString()); 

代替

tv.setText(data); 

它會顯示這樣的事情: ([字段1],[字段2],... )

+0

當我向數據庫添加第二個條目時,出現在第一個條目下的任何方式都可以使用。 – ba2210

+0

你能解釋一下更詳細一點嗎?我不明白你的問題。 –

+0

您需要附加換行符。見[這個答案](http://stackoverflow.com/questions/5382490/how-to-add-a-line-break-in-an-android-textview) – codeMagic

1

你可能要採取的每個StringArrayList,並將它們添加到一個String對象,然後添加到您的TextView。類似於

String text = "These are my database Strings "; 
for (int i=0; i<data.size(), i++) 
{ 
    text = text.concat(data.get(i)); // might want to add a space, ",", or some other separator 
} 
tv.setText(text); 

並且您可以分開String s,但是希望顯示它們。