下面的代码给出了生成ListView
public class MyList extends ListActivity {
static final String[] COUNTRIES = new String[] {LONG LIST OF COUNTRIES};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,R.layout.main,COUNTRIES));
ListView lv=getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
Intent i=new Intent(MyList.this,Another.class);
Bundle b = new Bundle();
b.putInt("id", (int)id);
intent.putExtras(b);
startActivity(intent);
}
});
}
}另一项活动是
public class Another extends Activity{
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mainseocnd);
Bundle b=new Bundle();
int value= b.getInt("id",0);
TextView tv=(TextView)findViewById(R.id.text);
tv.setText(""+value);
}
}现在,当我点击任何列表项,假设id-5,它总是显示0,我想得到列表视图项id,就像如果用户点击列表中的第二个项,另一个活动应该显示1(b/c从0开始)。请纠正我哪里出了问题。提前感谢!!
发布于 2011-11-10 23:16:25
在第二个活动中而不是
Bundle b=new Bundle();
int value= b.getInt("id",0); 使用
int value = icicle.getInt("id",0);这会给你一个解决方案..。:)
发布于 2011-11-10 23:23:41
您检索值的方式是错误的:
Bundle b=new Bundle();
int value= b.getInt("id",0);您创建了一个新的bundle,并尝试在没有(它是新的)时从其中获取一个值。相反,您必须获得该活动的启动意图提供的额外内容。试一试
int value = getIntent().getIntExtra("id", 0);而不是。
https://stackoverflow.com/questions/8081445
复制相似问题