简体   繁体   中英

Android/Java - Defining variables at different “places”

Okay this title might sound strange but I don't really know how to explain it.

What I'm trying to do: query a database, use the #of results to define a string array length and use the results to fill a view. All of this works, theoretically, but when I try to move my code from onCreate "up", I get syntax errors I can't fix. It might make more sense to just read my comments in the code below!

public class A_customlist extends ListActivity {

Integer runme = 5;
int[] imgb = new int[runme];
{
    for (int number = 0; number < imgb.length; number++) {
        imgb[number] = R.drawable.spatz_adult;
    }
    ;
};

SQLiteDatabase TPBDB;
String[] myString2 = new String[5];

// what I want to do: new String[count]
// basically do all the TPBDB stuff (see below) first, so I can access the
// count variable
// defining myString in onCreate makes it inaccessible in onListItemClick
String[] myString = new String[5];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // "TPBDB stuff"
    TPBDB = openOrCreateDatabase("TPBDB1", MODE_PRIVATE, null);
    Cursor cur = TPBDB.rawQuery("SELECT * from Vogel", null);
    String mycur = cur.toString();
    int count = cur.getCount();
    cur.moveToFirst();

    // String[] myString = new String[count+1]; // 4 entries, runme = 5!

    for (Integer j = 0; j < count; j++) {
        myString[j] = Long.toString(cur.getLong(cur.getColumnIndex("uid")));
        myString2[j] = cur.getString(cur.getColumnIndex("datum"));
        cur.moveToNext();
    }
    ;

    TPBDB.close();
    // --- "TPBDB stuff"

    getListView().setDividerHeight(2);
    getListView().setAdapter(
            new BindDataAdapter(this, imgb, myString, myString2));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(myString[position] + " is clicked.");
    builder.setPositiveButton("OK", null);
    builder.show();
}

// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.activity_list, menu);
// return true;
// }

}

Like this you can access your array in the onClickListener, in case thats what you wanted to achieve

String[] myString;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // "TPBDB stuff"
    TPBDB = openOrCreateDatabase("TPBDB1", MODE_PRIVATE, null);
    Cursor cur = TPBDB.rawQuery("SELECT * from Vogel", null);
    String mycur = cur.toString();
    int count = cur.getCount();

    myString = new String[count]; // init array here

    ...
}

you just have to declare a field "above" oncreate, you dont have to initialize it at the same time.

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