[英]get selected item - ListView Android
I know this has been asked here but the answers were quite confusing. 我知道这里有人问过这个问题,但是答案却很混乱。 I have 3 items in my
ListView
. 我的
ListView
有3个项目。 They are "Aluminium", "Gold" and "Zinc". 它们是“铝”,“金”和“锌”。 Through each one of them, I want to start different activities and for that I have created the 3 activities which i named "Aluminium.java","Gold.java" and "Zinc.java"
我想通过它们中的每一个开始不同的活动,为此,我创建了3个活动,分别命名为“ Aluminium.java”,“ Gold.java”和“ Zinc.java”。
I have used this ListView
in a drawer layout
for the navigation drawer. 我在导航抽屉的
drawer layout
使用了此ListView
。 I implemented navigation drawers through the code given below which i got from a site.This code changes fragments and its not working properly. 我通过下面从站点获得的代码实现了导航抽屉,该代码更改了片段并且无法正常工作。 Instead of fragments, I want to switch activities.
除了片段,我想切换活动。
I want to achieve 3 things: 我想实现三件事:
Sorry if its too dumb but I am a beginner. 对不起,如果它太笨了,但我是初学者。 Please help me out with the code.
请帮助我的代码。
Java code Java代码
public class MainActivity extends FragmentActivity {
final String[] data ={"Aluminium","Gold","Zinc"};
final String[] fragments ={
"com.Chinmay.navigationdrawer.Gold",
"com.Chinmay.navigationdrawer.Aluminium",
"com.Chinmay.navigationdrawer.Zinc"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
@Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
android.support.v4.app.FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame, Fragment.instantiate(MainActivity.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
android.support.v4.app.FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame,Fragment.instantiate(MainActivity.this, fragments[0]));
tx.commit();
}
});
}
} }
Make a base activity class and put all your drawer code there, and extend this base class for your 3 activity, in that way, you'll have drawer for your all activities. 创建一个基本活动类,并将所有抽屉式代码放在此处,并为您的3个活动扩展该基本类,这样,您将拥有所有活动的抽屉。
class Gold extends BaseActivity{
}
For the clicking part, you already set an item click listener, just make a switch case such as 对于单击部分,您已经设置了一个项目单击侦听器,只需设置一个开关盒,例如
switch (pos){
case 0:
Intent i = new Intent(this,Gold.java);
startActivity(i);
break;
}
// fill the rest
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.