2015-04-06 83 views
2

嗨我想讓我的列表視圖與每個部分打開不同的活動。截至目前,我可以開展一項活動,但他們都開放相同的活動。我需要知道改變,例如:Android ListView-開始與每個項目的新活動

item1將打開activity1 item2將打開activity2等等。

任何幫助將非常感激,這裏是我的代碼(一些評論的是形成一個教程,我跟着我的經驗不足很抱歉):

ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_about); 

    //get listview from xml 
    listView = (ListView)findViewById(R.id.listView); 

    //defined array to show in listview 
    String[] values = new String[]{"About the App", 
            "How to Use", 
            "Build log", 
            }; 
    //define a new context 
    //First parameter - Context 
    // Second parameter - Layout for the row 
    // Third parameter - ID of the TextView to which the data is written 
    // Forth - the Array of data 

    ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, android.R.id.text1, values); 

    //Assign adapter to listview 
    listView.setAdapter(adapter); 

    // ListView Item Click Listener 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      /*//listview clicked item index 
      int itemPosition = position; 

      //Listview clicked item value 
      String itemValue = (String)listView.getItemAtPosition(position); 

      //show alert 
      Toast.makeText(getApplicationContext(), 
        "Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/ 

      Intent appInfo = new Intent(About.this, about_app.class); 
       startActivity(appInfo); 

     } 
    }); 
} 

回答

3
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      /*//listview clicked item index 
      int itemPosition = position; 

      //Listview clicked item value 
      String itemValue = (String)listView.getItemAtPosition(position); 

      //show alert 
      Toast.makeText(getApplicationContext(), 
        "Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/ 
      switch(itemPosition)  

      case 0 :Intent appInfo = new Intent(About.this, about_app.class); 
       startActivity(appInfo); 
      break; 
      case 1 :Intent appInfo = new Intent(About.this, Activity1.class); 
       startActivity(appInfo); 
      break; 
      case 2 :Intent appInfo = new Intent(About.this, Activity2.class); 
       startActivity(appInfo); 
      break; 
     } 
    }); 
+0

非常感謝你的幫助:D – 2015-04-06 06:51:25

+0

@noobvenvonhack謝謝 – 2015-04-06 06:52:28

1

對於這一點,你就不得不提這樣的:

if (position == 1) { 
Intent appInfo = new Intent(About.this, about_app.class); 
startActivity(appInfo); 
} 
else if (position == 2) { 
Intent appInfo1 = new Intent(About.this, Test.class); 
startActivity(appInfo1); 
} 
1

檢查。

public interface IAction { 
    public abstract void doAction(); 
    public abstract String getCaption(); 
} 

ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_about); 

    //get listview from xml 
    listView = (ListView)findViewById(R.id.listView); 

    //defined array to show in listview 
    final ArrayList<String> values = new ArrayList<String>(); 
    //define a new context 
    //First parameter - Context 
    // Second parameter - Layout for the row 
    // Third parameter - ID of the TextView to which the data is written 
    // Forth - the Array of data 

    final ArrayList<IAction> actions = new ArrayList<IAction>(); 
    prepareActions(actions); 
    for (int i = 0; i < actions.size(); i++) 
     values.add(actions.get(i).getCaption()); 
    ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, values); 

    //Assign adapter to listview 
    listView.setAdapter(adapter); 

    // ListView Item Click Listener 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     /*//listview clicked item index 
     int itemPosition = position; 

     //Listview clicked item value 
     String itemValue = (String)listView.getItemAtPosition(position); 

     //show alert 
     Toast.makeText(getApplicationContext(), 
       "Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/ 

      actions.get(position).doAction(); 

     } 
    }); 
} 
    private void prepareActions(final ArrayList<IAction> actions) { 


     actions.add(new IAction() { 
      @Override 
      public void doAction() { 
       startActivity(new Intent(About.this, about_app.class)); 
      } 

      @Override 
      public String getCaption() { 
       return "About the App"; 
      } 
     }); 

     actions.add(new IAction() { 
      @Override 
      public void doAction() { 
       startActivity(new Intent(About.this, HowtoUse.class)); 
      } 

      @Override 
      public String getCaption() { 
       return "How to Use"; 
      } 
     }); 

     actions.add(new IAction() { 
      @Override 
      public void doAction() { 
       if (NeedToBuildLog) 
        startActivity(new Intent(About.this, Buildlog.class)); 
       /*else 
        Toast.makeText(getApplicationContext(), 
          "Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/ 
      } 

      @Override 
      public String getCaption() { 
       return "Build log"; 
      } 
     }); 
    }