之前我曾询问过使用数据库中的数据填充列表视图。现在,我正在尝试使项目可单击并将其指向另一个类,其中包含一些将用于更新类的信息。
我认为我已经设法通过使用意图将它指向“另一个”类,但问题是我对意图的数据不会转移到'另一个'类。
这是代码: MySQLView.java
ArrayList<String> data = info.geData();
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
/***I made the value of the item, numbers. cos I think its easier?***/
String draftID = lv.getItemAtPosition(position).toString();
Intent i =new Intent(SQLView.this, com.android.its.SQLiteExample.class);
i.putExtra("draftID", draftID);
startActivity(i);
/***just to check if it has value***/
Toast.makeText(getBaseContext(),"position is : "+position+" and value is "+
draftID,Toast.LENGTH_SHORT).show();
}
});
然后这是包含editText
的SQLiteExample类case R.id.bgetInfo:
try {
Bundle extras = getIntent().getExtras();
String s= extras.getString("draftID");
long l = Long.parseLong(s);
HotOrNot hon = new HotOrNot(this);
hon.open();
String returnedName = hon.getName(l);
String returnedHotness = hon.getHotness(l);
hon.close();
sqlName.setText(returnedName);
sqlHotness.setText(returnedHotness);
} catch (Exception e) {
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
break;
这是我的databasehelper类
public ArrayList<String> geData() {
// TODO Auto-generated method stub
String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
Cursor c =ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
ArrayList<String> result = new ArrayList<String>();
int iRow=c.getColumnIndex(KEY_ROWID);
for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
result.add(c.getString(iRow));
}
return result;
}
if (c!=null){
c.moveToFirst();
String hotness=c.getString(2);
return hotness;
}
return null;
}
public String getName(long l) throws SQLiteException{
// TODO Auto-generated method stub
String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
Cursor c= ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID+"="+l, null, null, null, null);
if (c!=null){
c.moveToFirst();
String name=c.getString(1);
return name;
}
return null;
}
public String getHotness(long l)throws SQLiteException {
// TODO Auto-generated method stub
String[]columns=new String[]{ KEY_ROWID,KEY_NAME, KEY_HOTNESS};
Cursor c= ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID+"="+l, null, null, null, null);
(这是我之前的问题How to get data from my database and put it on a ListView that is clickable?)
更新:好的,虽然我的代码非常混乱,但它似乎一直在运行,我没有测试getInfo按钮,这就是它没有出现的原因。很抱歉打扰你们:p但是我仍然想知道你将一个ROWID(数据库)放在listview中并将其价值放在其他类上的方法是什么?
答案 0 :(得分:0)
应该是。您通过Intent启动的SqlLiteExample
类应该检索您在onCreate
方法中放入Intent的数据
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getintent()
String draftID = intent.getStringExtra("draftID");
}
你这样做吗?