[英]How do I pass an integer to an instance of a class and use it to call the method of another class?
here's activity 1 which is a listview.这是活动 1,它是一个列表视图。 when the user clicks on an item, I want the item clicked to launch an instance of a class and pass an int value to it, which will later be used in a switch.
当用户单击一个项目时,我希望单击该项目以启动一个类的实例并将一个 int 值传递给它,该值稍后将在开关中使用。
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
switch(position){
case 0:
Intent g = new Intent(books.this, SpecificBook.class);
Bundle b = new Bundle();
b.putInt("dt", 0);
g.putExtras(b);
books.this.startActivity(g);
break;
case 1:
Intent ex = new Intent(books.this, SpecificBook.class);
Bundle b1 = new Bundle();
b1.putInt("dt", 1);
ex.putExtras(b1);
books.this.startActivity(ex);
break;
//etc.
here's activity 2, which is supposed to retrieve the int value and call the appropriate method from the database helper class.这是活动 2,它应该检索 int 值并从数据库帮助程序类调用适当的方法。
public class SpecificBook extends Activity {
private DatabaseHelper Adapter;
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
int dt = myBundle.getInt("dt");
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listy);
ListView lv = (ListView)findViewById(R.id.listview);
Adapter = new DatabaseHelper(this);
Adapter.open();
Cursor cursor = null;
switch(dt){
case 0:cursor = DatabaseHelper.getbook1Data(); break;
case 1:cursor = DatabaseHelper.getbook2Data(); break;
//etc.
}
startManagingCursor(cursor);
etc.等等。
The database methods are queries.数据库方法是查询。 Basically, I want each item in the book class listview to run it's own query based on the item selected and display the results.
基本上,我希望书籍类 listview 中的每个项目根据所选项目运行它自己的查询并显示结果。 I get a "source not found" and runtimeexception error.
我收到“找不到源”和运行时异常错误。 where am i going wrong?
我哪里错了? is there a better way to go about this?
有没有更好的方法来解决这个问题?
I have already tried the "getter and setter" way to no avail.我已经尝试过“getter 和 setter”方式无济于事。 I've also tried the "putextra" method on the instance of the intent but that didn't work.
我也在意图的实例上尝试了“putextra”方法,但没有奏效。
The earliest you can access an Intent is in onCreate()
:最早可以访问 Intent 的是
onCreate()
:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listy);
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
int dt = myBundle.getInt("dt");
You should check if the Intent or Bundle is null and if you only want the one item from your Intent you can use:您应该检查 Intent 或 Bundle 是否为空,如果您只想要 Intent 中的一项,则可以使用:
Intent myLocalIntent = getIntent();
if(myLocalIntent != null) {
int dt = myLocalIntent.getIntExtra("dt", -1); // -1 is an arbitrary default value
}
Lastly, you don't need to create a new Bundle to pass values in an Intent and it looks like you simply want to pass the position
... So you can drastically shorten your onItemClick()
method:最后,你不需要创建一个新的 Bundle 来在 Intent 中传递值,看起来你只是想传递
position
......所以你可以大大缩短你的onItemClick()
方法:
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent g = new Intent(books.this, SpecificBook.class);
g.putInt("dt", position);
books.this.startActivity(g);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.