我有一个问题,我找了一点,但我无法理解为什么它会附加......
如果有人可以帮助我。
public class Menu extends Activity
{
private SQLiteDatabase db;
private GridView grid;
private Button add;
private Button info;
private Button deco;
private String lab[];
private String id[];
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
grid = (GridView) findViewById(R.id.grid);
add = (Button) findViewById(R.id.add_account);
info = (Button) findViewById(R.id.info);
deco = (Button) findViewById(R.id.deconnexion);
add.setOnClickListener(add_listener);
info.setOnClickListener(info_listener);
deco.setOnClickListener(deco_listener);
try
{
db = openOrCreateDatabase("TMP_TTTT_TLA", MODE_PRIVATE, null);
}
catch(SQLiteException e)
{
Toast.makeText(Menu.this, "Impossible d'avoir accès à la base de donnéee.", Toast.LENGTH_LONG).show();
}
Cursor c = db.rawQuery("SELECT a_id, a_name FROM Account", null);
lab = new String[c.getCount()];
id = new String[c.getCount()];
int i = 0;
if (c.getCount() != 0 )
{
if (c.moveToFirst())
{
do
{
lab[i] = c.getString(c.getColumnIndex("a_name"));
id[i] = c.getString(c.getColumnIndex("a_id"));
i++;
}
while(c.moveToNext());
}
}
c.close();
grid.setAdapter(new Adapter_Menu(this, lab));
grid.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Intent x = new Intent(Menu.this, Account_View.class);
x.putExtra("Id", id[position]);
startActivity(x);
}
});
}
}
问题来自这条线:
x.putExtra("Id", id[position]);
它表示表达式的类型必须是数组类型,但它解析为long。
请帮帮我。
答案 0 :(得分:4)
问题是您有两个名为id的变量:
private String id[];
和
long id
onItemClick
的。
只需将onItemClickListener
更改为以下内容:
public void onItemClick(AdapterView parent, View v, int position, long anid)
{
Intent x = new Intent(Menu.this, Account_View.class);
x.putExtra("Id", id[position]);
startActivity(x);
}
请注意onItemClick
的最后一个参数的名称。