2

I make a connection to an external server SQL Server, but I can only display the data with the following code:

void cursorset (){
    java.sql.DatabaseMetaData dm = null;

    try {
        connection = this.getConnection();

        if (connection != null) {
            Resultset = result;
            private final String statement = "select*from *******";
            dm = connection.getMetaData();            
            Statement select = connection.createStatement();
            result = select.executeQuery(statement);

            while (result.next()) {
                mostrar_datos.append("" +result.getObject(1)+result.getObject(2)+" "+result.getObject(3));
            }

            result.close();
            result = null;
            closeConnection();
        } else {
            mostrar_datos.append("Error: No active Connection");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    dm = null;
}

The problem is I need to display this returned data, on tables with dynamic rows and columns, or alertdialog and I have no idea how to do it.

2 Answers 2

1

how to display data from a SQL Server query into android tables

So you should create some class(es) that interprets table(s) in real database and your method described above should return List of values.

Subsequently, returned data you can simple display in AlertDialog or some ListView.

Example:

Your database has one table: User with 3 columns - id(primary key), name, surname. So you create class with "same structure"

public class User {

   private int id;
   private String name;
   private String surname;

   // getters and setters
}

Then your method can looks like this:

public List<User> getAll() {
   List<User> users = new ArrayList<User>();
   User u = null;
   // initialise connection etc.
   while (resultSet.next()) {
      u = new User();
      u.setId(resultSet.getInt(1));
      u.setName(resultSet.getString(2));
      u.setSurname(resultSet.getString(3));
      users.add(u);  
   }
   return users;
   // in finally block close connection.
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this code but I can not put the arguments in alertdialog because of this error: The method setItems (int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments (LinkedList <String>, new DialogInterface.OnClickListener () {})
What about create custom Adapter for AlertDialog?
0

You could easily display the data in a ListView with a corresponding CursorAdapter, your query would return a Cursor object. The CursorAdapters responsibility is to map data from the Cursor to ids in the layout of a row in the ListView.

Here are a few tutorials to get you started:

http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_4_-_using_cursoradapters

http://developer.android.com/reference/android/app/ListActivity.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.