2014-02-13 31 views
0

我想使用ListIctivity的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。我該如何做到這一點,當點擊列表視圖中的項目時,它會返回與該行關聯的對象並將其傳遞給新的活動?無法從ListActivity內啓動意圖OnItemListClick

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

是否監聽器捕捉到的事件呢? –

+0

您是否在您的Manifest中指定了TaskDisplay類? –

回答

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);列表項的點擊方法的第一道防線。

+0

我不認爲這會有所作爲 –

+0

我給了這個嘗試,我與你上面的人說了什麼,但它仍然沒有工作 – user3282276

+0

可以請你在這裏粘貼更多的代碼。 –

0

嘗試......

this.startActivity(launchIntent); 
+0

我給了這是一個嘗試,它沒有奏效 – user3282276