简体   繁体   中英

setText() into a textview from a database

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

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 String s however you want them to be displayed.

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],...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM