0

what i'm trying to do is get entries, one-by-one, here's the code.

Teams.Java
public String getData(String key) {
        // TODO Auto-generated method stub
        String[] columns = new String[] { Key };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        String result = "";
        int iName = c.getColumnIndex(Key);
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + "," + c.getString(iName);

        }
        return result;
    }

the activity

MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView t1 = (TextView) findViewById(R.id.tvINames);
        Teams stat = new Teams(this);
        stat.open();
        String Names = stat.getName("KEY_TEAMS");
        stat.close();
        t1.setText(Names);

if the logcat is needed, just tell me.

ken
  • 113
  • 7
  • Just get all the values in that function. Return the ArrayList or String[] and iterate to use that data. What's issue in that? – MysticMagicϡ Sep 19 '14 at 08:32
  • i should have done that but im using datatable for each field.i mean, its really 3 different fields that im getting. – ken Sep 19 '14 at 09:15

1 Answers1

0

First of all, the 'Key' on the lines:

String[] columns = new String[] { Key };

and

int iName = c.getColumnIndex(Key);

uses a capital 'K' where as the parameter in the method-header is named with a normal 'k':

public String getData(String key) {

Do you have a field defined with the name 'Key' inside Teams.java? I can imagine that this does not lead to the expected behaviour of your code, since the parameter is not even used.

Another thing I would definitely change is using a StringBuilder (or StringBuffer if it has to be thread-safe) for concatenating the string inside the getData()-method.

And for iterating over the cursor I would just use:

while (c.moveToNext()) {
    ...
}

Another inconsistency exists on following line:

String Names = stat.getName("KEY_TEAMS");

you are calling another method than the shown getData(String key)-method.


If I may rewrite your method I would suggest something like this:

//Teams.Java
public String getData(String key) {

    String[] columns = new String[] { key };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

    final int iName = c.getColumnIndex(key);

    StringBuilder result = new StringBuilder();
    while (c.moveToNext()) {
        result.append(c.getString(iName));
        result.append(",");
    }

    return result.toString();
}

AND

//MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Teams stat = new Teams(this);

    try {
        stat.open();

        TextView t1 = (TextView) findViewById(R.id.tvINames);
        t1.setText(stat.getData("KEY_TEAMS"));
    } catch (Exception e) {
        Log.e(MainActivity.class.getSimpleName(), "some meaningful error message 42!", e);
    } finally {
        stat.close();
    }

}

In general it is a very good idea to wrap all access to cursors into a try-catch-finally block for making sure, that it is closed in any case!

AZOM
  • 265
  • 5
  • 15
  • as for the public String getData(String key){..it was Key, i just misplelled. if i have a filed defined with the name key, i don't know if thats needed anymore since i pass the variable from MainActivity.java. and can you explain more about that StringBuffer thing? – ken Sep 19 '14 at 09:07
  • you can read more about StringBuffer/StringBuilder here:http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer – AZOM Sep 19 '14 at 09:11
  • For making you code clean and easier to understand what is going on, I would remove any field inside Teams.java with the name 'Key' or 'key'. If you need them as a fallback, when the parameter from the methods are not usable, you can still have a field named 'mKey' (m stands for member, so you know immediately where it comes from) or even use a constant private static final String DEFAULT_KEY; – AZOM Sep 19 '14 at 09:39