我有一个ListView,它包含来自解析的xml的数据。所以这是我的示例代码: 我不能从列表视图到新活动
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String date = ((TextView) view.findViewById(R.id.artist)).getText().toString();
Intent i = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
i.putExtra(KEY_HEADLINE, title);
i.putExtra(KEY_ARTICLEDATE, date);
startActivity(i);
}
});
答案 0 :(得分:1)
getApplicationContext()
几乎总是错的。你需要像Ashish所说的那样传递当前Activity的Context,
getApplicationContext()
返回当前进程的单个全局Application对象的上下文。这通常只应在需要生命周期与当前上下文分离的上下文时使用,该上下文与进程的生命周期而不是当前组件相关联。
在dev.android.com
答案 1 :(得分:0)
您正在Intent中传递Application上下文。传递活动上下文。
Intent i = new Intent(YourActivity.this, SingleMenuItemActivity.class);
i.putExtra(KEY_HEADLINE, title);
i.putExtra(KEY_ARTICLEDATE, date);
startActivity(i);
答案 2 :(得分:0)
您尝试从getApplicationContext()
课程致电OnItemClickListener
,但这是一种活动方法。
例如,您的活动名称为MyActivity
。
所以你应该把它称为MyActivity.this.getApplicationContext()
。
答案 3 :(得分:0)
first You have to add
list.setClickable("true");
then, get the listview position
Object o = lv.getItemIdAtPosition(position);
int posix = Integer.parseInt(o.toString());
So, Final code will be..
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
list.setClickable("true");
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String date = ((TextView) view.findViewById(R.id.artist)).getText().toString();
Object o = lv.getItemIdAtPosition(position);
int posix = Integer.parseInt(o.toString());
Intent i = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
i.putExtra(KEY_HEADLINE, title);
i.putExtra(KEY_ARTICLEDATE, date);
startActivity(i);
}
});