How to populate table view using a database

You must Login before you can answer or comment on any questions.

Hi everyone,

I'm really stuck on this one. I have tried everything and can't get it to work. How do I populate a table view with a database? I can get it to populate, but I need the table view row to display more than just the title. I basically need it to display three labels with the Title, Time and Name from the database :(

— asked 8 months ago by Martin Joubert
2 Comments
  • Martin

    I'm sure you'll agree, that your question doesn't gives us much to work with. I strongly suggest you have a look at the guidelines to follow when asking a question.

    Also, please include some code detailing what you are trying to achieve. This will give everyone a better understanding of the problem you are facing.

    Simply asking for an entire solution without having experienced it yourself seems counter-productive.

    — commented 8 months ago by Christian Brousseau

  • +1 for Christian. If you share code, someone can tell you exactly what you need to change in it. Without that I have to point you to KitchenSink. There are examples of how to read the database and other examples (with hardcoded data) to show how you can create custom row tableview. It is really not that hard if you follow the examples in KitchenSink.

    — commented 8 months ago by Birender Saini

1 Answer

Accepted Answer

Here is the basic idea of what you need.

var win = Ti.UI.createWindow();
 
var tableView = Ti.UI.createTableView();
 
var db = Titanium.Database.open('DB');
 
var data = db.execute('SELECT * FROM table');
 
while (data.isValidRow()) {
 
    var row = Titanium.UI.createTableViewRow();
 
    var myTitle = Ti.UI.createLabel({
        text: data.fieldByName('title'),
        otherLayoutProperties:'...'
    });
 
    var myTime = Ti.UI.createLabel({
        text: data.fieldByName('time'),
        otherLayoutProperties:'...'
    });
 
    var myName = Ti.UI.createLabel({
        text: data.fieldByName('name'),
        otherLayoutProperties:'...'
    });
 
    row.add(myTitle, myTime, myName);
 
    tableView.add(row);
 
    data.next();
}
 
data.close();
 
db.close();
 
win.add(tableView);
 
win.open();
*written as a sample, not tested

— answered 8 months ago by Alan Leard
answer permalink
3 Comments
  • Thank you so much Alan. All I wanted to know. Really appreciate the help :) Sorry to everyone else who complained about me not posting any code.

    — commented 8 months ago by Martin Joubert

  • Just one more question. I'm trying to display the rest of the selected rows details in a detail view, but can't get it to work. Should I be using e.row on the tableview click event?

    — commented 8 months ago by Martin Joubert

  • Hi MArtin : suppose i have a one Tableview and the tableview data are coming from web service and i stored these data into the Table view and This data is a college names and Now I want to search college name from this tableview then how can i do..??

    — commented 8 months ago by Jigar Maheshwari

Your Answer

Think you can help? Login to answer this question!