我已经使用图片和一些文本制作了listview。我正在尝试在每个被点击的项目上打开一个新活动。但是无论点击哪个项目,都会打开相同的活动。这是代码
MainActivity.java
public class Dslr extends Activity implements OnItemClickListener
{
public static final String[] titles=new String[]{"Nikon D3300","Canon EOS 1200D","C","D"};
public static final String[] Descriptions=new String[]{"The Nikon D3300 is the latest entry-level model to Nikon's series: an affordable and well-balanced choice to introduce you to the world of DSLR.","The Canon EOS 1200D might be a safe replacement of the nearly four-year-old 1100D model, but it holds enough weight to keep the entry-level DSLR market bubbling along.","nnvnv","ggs"};
public static final Integer[] images={ R.drawable.d1,R.drawable.d2,R.drawable.s11,R.drawable.s2};
ListView listView;
List<RowItem> rowItems;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dslr);
back=(Button)findViewById(R.id.button3);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent s=new Intent(getApplicationContext(),M.class);
startActivity(s);
}
});
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], Descriptions[i]);
rowItems.add(item);
}
listView=(ListView)findViewById(R.id.list);
CustomListViewAdapter adapter=new CustomListViewAdapter(this,R.layout.list_item,rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{ switch(position)
{
case 0: Intent i=new Intent(getApplicationContext(),D.class);
startActivity(i);
case 1:Intent b=new Intent(getApplicationContext(),D_1.class);
startActivity(b);
case 2: Intent z=new Intent(getApplicationContext(),D.class);
startActivity(z);
}
}
}
D.java
public class D extends Activity {
Button buy,back;
ImageView iv;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_d);
iv=(ImageView)findViewById(R.id.imageView1);
iv.setScaleType(ScaleType.FIT_XY);
t1=(TextView)findViewById(R.id.textView1);
t1.setMovementMethod(new ScrollingMovementMethod());
buy=(Button)findViewById(R.id.button1);
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("https://www.google.co.in/aclk?sa=L&ai=CoF1Zol9ZVtzADciuoAP57pvIDfDzl6cL0MSdhLsCuI6-3rwDCAQQAigFYOXC5IOkDsgBB6oEJE_QiSIJrNnjAN5b8R-9M8Ju79TsseUlGme7UcNxXdnFBBkwlMAFBaAGJoAH2KTjTpAHA6gHpr4b2AcB4BL88bWn1rDTp6YB&sig=AOD64_3x0dFtyFndnyF_-tFHuIrwp4DQtQ&ctype=5&clui=5&q=&ved=0ahUKEwi1xoHc07LJAhXRH44KHZv3BL0Qww8IJQ&adurl=http://5231.xg4ken.com/trk/v1%3Fprof%3D435%26camp%3D92%26affcode%3Dpg19701%26kct%3Dgoogle%26kchid%3D6215386675%26cid%3D84519575488%26networkType%3Dsearch%26kdv%3Dc%26kpid%3DB00KBI7ANQ%26kmc%3D105201768%26criteriaid%3Dkwd-119383492408%26adgroupid%3D22913451688%26campaignid%3D336119728%26locphy%3D9061721%26adpos%3D1o2%26url%3Dhttp://www.amazon.in/Nikon-D3200-24-2MP-Digital-18-55mm/dp/B00KBI7ANQ%3Ftag%3Dgooginhydr18418%26tag%3Dgooginkenshoo-21%26ascsubtag%3D_kenshoo_clickid_"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
back=(Button)findViewById(R.id.button2);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent b=new Intent(getApplicationContext(),Dslr.class);
startActivity(b);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.d, 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);
}
}
D_1.java
public class D_1 extends Activity {
ImageView d;
TextView t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_d_1);
d=(ImageView)findViewById(R.id.imageView_d_1);
d.setScaleType(ScaleType.FIT_XY);
t2=(TextView)findViewById(R.id.textView_d_1);
t2.setMovementMethod(new ScrollingMovementMethod());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.d_1, 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 :(得分:0)
这只是因为你的switch case
语句一直在执行,因为没有写break
语句:
switch (position) {
case 0: Intent i=new Intent(getApplicationContext(),D.class);
startActivity(i);
break;
case 1: Intent b=new Intent(getApplicationContext(),D_1.class);
startActivity(b);
break;
case 2: Intent z=new Intent(getApplicationContext(),D.class);
startActivity(z);
break;
}