2015-09-08 24 views
-1

如何使用自定義listview的不同意圖活動。當我點擊的所有項目如何在使用自定義列表視圖時使用意圖

代碼

@SuppressLint("NewApi") 
public class MainActivity extends Activity { 

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

    ActionBar actionBar = getActionBar(); 

    // set the icon 
    actionBar.setIcon(R.drawable.excel); 
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#546E7A"))); 

    ArrayList<Excel> list = new ArrayList<Excel>(); 
    list.add(new Excel("Google", "Android")); 
    list.add(new Excel("Apple", "IOS")); 

    ListAdapter adapter = new ListAdapter(this, list); 

    ListView listView = (ListView) findViewById(R.id.id_list); 
    listView.setAdapter(adapter); 

    // event handler click item of list 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
      public void onItemClick(AdapterView arg0, View arg1, 
      int position, long arg3) { 
        // call new layout with intent 
       Intent intent = new Intent(MainActivity.this, Apple.class); 
      startActivity(intent); 

     } 
    }); 
} 
} 

代碼只是查看Apple.class活動,如何運行不同的活動例如:

項目谷歌運行Google.class

項目Apple run Apple.class

+0

使用'if else'並開始適當的活動。 – Rustam

回答

0

根據您當前的代碼使用

Intent intent; 
if(position==0){ 
    intent = new Intent(MainActivity.this, Google.class); 
}else if(position==1){ 
    intent = new Intent(MainActivity.this, Apple.class); 
} 
startActivity(intent); 
0

嘗試像這樣

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
      public void onItemClick(AdapterView arg0, View arg1, 
      int position, long arg3) { 
      switch(Position) 
       { 
        case 0: 
        //Call Google Intent 
       break; 
       case 1: 
        // call new layout with intent 
       Intent intent = new Intent(MainActivity.this, Apple.class); 
       startActivity(intent); 
        break; 
       default: 
       // default code 
       break; 
       } 

     } 
    }); 
0
public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    switch(position) 
    { 
     case 0: Intent newActivity = new Intent(this, Apple.class);  
      startActivity(newActivity); 
      break; 
     case 1: Intent newActivity = new Intent(this, Google.class);  
       startActivity(newActivity); 
      break; 


    } 
} 
+0

非常感謝。 –

0

使用反射來獲取Class.The類名必須包含packagename.Like這一點。

try{ 
       Class classType=Class.forName("com.zfeng.stackquesimg.Google"); 
       Intent intent=new Intent(); 
       intent.setClass(MainActivity.this,classType); 
       startActivity(intent); 
      }catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
相關問題