我正在尝试使用ListActivity的OnItemListClick方法来启动一个新活动,其中包含有关列表项的更多特定信息。为了做到这一点,我正在使用How do I pass an object from one activity to another on Android?的最佳答案所建议的捆绑,但无论我尝试OnItemListClick似乎都没有做任何事情。我也试过没有通过捆绑思考导致问题,因为这里建议Using an intent to start new activity from ListActivity并且也没有做任何onClick。如果单击listview中的某个项目,它返回与该行关联的对象并将其传递给新活动,我该怎么做呢?
public class TaskList extends ListActivity {
private ArrayList<Task> stuffToDo, completed;
private TaskAdapter t_adapter;
public Button add;
private File saveFile;
private Bundle clickBundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_list);
stuffToDo = new ArrayList<Task>();
completed = new ArrayList<Task>();
t_adapter = new TaskAdapter(this, R.layout.row_rl, stuffToDo);
setListAdapter(t_adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Make a bundle and put the Task selected in it then pass this to the
// intent to start the new activity
Intent launchIntent = new Intent(getApplicationContext(), TaskDisplay.class);
Log.d("Debug", "Intent made");
clickBundle.clear();
clickBundle.putSerializable("Task", stuffToDo.get(position));
Log.d("Debug", "Task put in bundle");
launchIntent.putExtras(clickBundle);
this.startActivity(launchIntent);
Log.d("Debug", "launched");
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.addtaskbutton:
promptUserForInfo();
t_adapter.notifyDataSetChanged();
// added for debug
saveInfo();
return true;
case R.id.cleartasks:
clearTasks();
t_adapter.notifyDataSetChanged();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
和我在更具体的活动中的代码
// Set the bundle equal to the extra put with the intent
input = this.getIntent().getExtras();
// If there are extras(a task) then assign it to the empty task variable
if (input != null){
task = (Task) input.getSerializable("Task");
}
答案 0 :(得分:1)
这意味着你指向列表。
更改Intent launchIntent = new Intent(this, TaskDisplay.class);
到意图launchIntent = new Intent(youractivityname.this, TaskDisplay.class);
或Intent launchIntent = new Intent(getApplicationContext(), TaskDisplay.class);
并将此super.onListItemClick(l, v, position, id);
列表项的第一行点击方法。
答案 1 :(得分:0)
试试这个......
this.startActivity(launchIntent);