0

I am new to Android App Development. I am try to view my database using a Textview in my activity.

Here is my 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);
}

I seem to be getting and error at tv.setText(data);. Stating "The method setText(CharSequence) in the type TextView is not applicable for the arguments (ArrayList)" Then when i do the recommended fix it changes

tv.setText(data)

to

tv.setText((CharSequence) data);

Then when I test the application I get an error stating that it cannot be cast. What do I need to change to be able to view my database in the textview?

Any advice and help would be greatly appreciated. Thanks

BAlvernaz
  • 27
  • 3
  • 7
  • You could use data.toArray().toString() – nedaRM Sep 23 '13 at 01:07
  • `tv.setText(data)` is not applicable for array of string? and you cant cast the list to CharSequence object? you can passed some of strings in the data list `tv.setText(data.get(0))` 0 is index of the list – Husam Sep 23 '13 at 01:10
  • Tired that but now where the database values should be it say "Ljava.lang.Object@415b0b38" – BAlvernaz Sep 23 '13 at 01:13

2 Answers2

1

You probably want to take each String out of the ArrayList and add them to a single String object then add that to your TextView. Something like

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

and you can separate the Strings however you want them to be displayed.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
1

If you want to keep it simple you can use

tv.setText(data.toString());

instead of

tv.setText(data);

It will show something like this: ([field1],[field2],...)

Marco Altran
  • 376
  • 2
  • 9