我知道如何通过使用intent向listview添加活动,你可以在下面的代码中看到我使用customadapter.java文件将back.class添加到mainactivity.java中。 但是当我每次点击listview的每一行时它只打开一个活动(back.class)。现在我想使用intent为我的mainactivity.java添加不同的活动(leg.class,abs.class,chest.class等)所以当你点击listview的每一行时,它将打开不同的活动。我不知道怎么做?
这是我的mainactitvity.java文件
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String[] excercise = {"Back DAY", "Legs DAY", "Abs DAY", "Chest DAY", "Shoulder DAY", "Arms DAY"};
final int[] imgs = {R.drawable.back, R.drawable.leg, R.drawable.abs, R.drawable.chest, R.drawable.shoulder, R.drawable.arms};
ListAdapter saruadapter = new customadapter(this, excercise, imgs);
ListView sarulistview = (ListView) findViewById(R.id.sarelistview);
sarulistview.setAdapter(saruadapter);
// Intent intent = new Intent();
// startService(intent);
sarulistview.setOnItemClickListener(
new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// String excercise = String.valueOf(parent.getItemAtPosition(position));
// Toast.makeText(MainActivity.this, excercise, Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this, back.class);
startActivity(i);
}
}
);
}
}
这是我的customadapter.java文件
public class customadapter extends ArrayAdapter<String>{
private int[] imgs;
public customadapter(Context context, String[] excercise, int[] imgs) {
super(context, R.layout.custom_row, excercise);
this.imgs = imgs;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater saruinflator = LayoutInflater.from(getContext());
View coustomView = saruinflator.inflate(R.layout.custom_row, parent, false);
String excerciseitem = getItem(position);
TextView saruview = (TextView) coustomView.findViewById(R.id.sareview);
ImageView saruimg = (ImageView) coustomView.findViewById(R.id.sareimage);
saruview.setText(excerciseitem);
saruimg.setImageResource(imgs[position]);
return coustomView;
}
}
这是我的back.java文件
public class back extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bacon2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle back = getIntent().getExtras();
if(back == null)
{
return;
}
}
}
答案 0 :(得分:1)
使用切换条件并使用位置打开新活动。
sarulistview.setOnItemClickListener(
new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// String excercise = String.valueOf(parent.getItemAtPosition(position));
// Toast.makeText(MainActivity.this, excercise, Toast.LENGTH_LONG).show();
switch (position) {
case 0:
Intent i0 = new Intent(MainActivity.this, back.class);
startActivity(i0);
break;
case 1:
Intent i1 = new Intent(MainActivity.this, leg.class);
startActivity(i1);
break;
case 2:
Intent i2 = new Intent(MainActivity.this, abs.class);
startActivity(i2);
break;
case 3:
Intent i3 = new Intent(MainActivity.this, chest.class);
startActivity(i3);
break;
}
}
}
);
答案 1 :(得分:0)
当您将 onItemClick 设置为 ListView 时,您会获得所点击项目的位置。所以你需要根据你打开不同活动的需要进行某种类型的检查。一种可能的检查可以使用位置。例如: -
sarulistview.setOnItemClickListener(
new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// String excercise = String.valueOf(parent.getItemAtPosition(position));
// Toast.makeText(MainActivity.this, excercise, Toast.LENGTH_LONG).show();
if (position == 1) {
Intent i0 = new Intent(MainActivity.this, back.class);
startActivity(i0);
}
else if(position == 2){
Intent i1 = new Intent(MainActivity.this, chess.class);
startActivity(i1);
}else {
Intent i2 = new Intent(MainActivity.this, abc.class);
startActivity(i2);
}
}
}
);
这只是一个示例方法。业务逻辑将取决于您的需求。