2016-03-01 15 views
-1

我已經創建了一個ListView並在列表 中創建了A,B和C,我想當用戶單擊A時,它將用戶引導到一個名爲D的新活動。當他點擊B時,將他引導至新的Activity E,點擊C並轉到活動F.
我應該怎麼做才能做到這一點?如何去ListView的新活動?

這裏是我的代碼

public class MainActivity extends Activity implements 
              OnItemClickListener{ 

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

    ListView support_device_list=(ListView) 

findViewById(R.id.support_device_list); 
    support_device_list.setOnItemClickListener(this); 
    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

----------------------------------- ---------------------------------------------新代碼

是這樣嗎?

public class MainActivity extends Activity implements 
              OnItemClickListener{ 

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

    ListView support_device_list=(ListView) 

ListView list = (ListView) findViewById(R.id.listview); 
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
long id) { 
if(position==0) 
{ 
Intent in=new Intent(lt18iActivity.this,lt18iActivity.class); 
startActivity(in); 
} 
if(position==1) 
{ 
Intent in=new Intent(YourActivity.this,ActivityB.class); 
startActivity(in); 
} 
if(position==2) 
{ 
Intent in=new Intent(YourActivity.this,ActivityC.class); 
startActivity(in); 
} 
} 
}); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

在您的代碼? –

+1

在你的'ListView'上實現'OnItemClickListener'並在其中開始活動。 – Rohit5k2

+0

#rico chu這種類型的問題已經提出。請在Google上搜索 –

回答

0

試試這個

ListView list = (ListView) findViewById(R.id.listview); 
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    if(position==0) 
{ 
Intent in=new Intent(YourActivity.this,ActivityA.class); 
startActivity(in); 
} 
if(position==1) 
{ 
Intent in=new Intent(YourActivity.this,ActivityB.class); 
startActivity(in); 
} 
if(position==2) 
{ 
    Intent in=new Intent(YourActivity.this,ActivityC.class); 
startActivity(in); 
} 
} 
}); 
+0

更好地使用其他情況與其他案件與if。如果你不作爲例子所有其他案件將被檢查 – Aerox

+0

我可以創建一個字符串數組並將/ Intent in = newIntent(YourActivity.this,ActivityA.class);/Intent in = newIntent(YourActivity2.this,ActivityB.class); ? –

+0

@ricochu只是試一試 –