好吧这个标题可能听起来很奇怪,但我真的不知道如何解释它。
我正在尝试做的事情:查询数据库,使用#of结果定义字符串数组长度并使用结果填充视图。从理论上讲,所有这些都有效,但是当我尝试从onCreate“up”移动我的代码时,我得到了无法解决的语法错误。在下面的代码中阅读我的评论可能更有意义!
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;
// }
}
答案 0 :(得分:0)
像这样你可以在onClickListener中访问你的数组,以防你想要实现的目标
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
...
}
你只需要在oncreate上面声明一个字段,你就不必同时初始化它。