gpt4 book ai didi

java - 如何用我的 listView 项目打开不同的 Activity ?

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:28 26 4
gpt4 key购买 nike

我有一个包含 9 个不同字符串的字符串数组,并使用这些字符串创建了一个 listView;现在,当我单击 listView 的每个元素时,我想打开一个不同的 Activity 。我已经试过了

row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent((Activity)context, Workouts.class));
}
});

但它只为所有项目打开相同的 Activity 。如果您能为我提供一个工作代码,那就太好了,因为我已经尝试过解决这个问题。

主要 Activity :

public class MainActivity extends Activity implements OnItemClickListener {

private DrawerLayout drawerLayout;
private ListView listView;

private ActionBarDrawerToggle drawerListener;
private MyAdapter MyAdapter;

@Override
protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
drawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);

listView=(ListView)findViewById(R.id.drawerList);
MyAdapter=new MyAdapter(this);
listView.setAdapter(MyAdapter);
listView.setOnItemClickListener(this);

drawerListener= new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.listmenu,
R.string.drawer_open, R.string.drawer_close){

@Override
public void onDrawerClosed(View drawerView) {
// TODO Auto-generated method stub
super.onDrawerClosed(drawerView);
}

@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, " Drawer Opened ",
Toast.LENGTH_SHORT).show();
}

};

drawerLayout.setDrawerListener(drawerListener);

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub

if (drawerListener.onOptionsItemSelected(item))
{
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);

drawerListener.onConfigurationChanged(newConfig);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}

public void selectItem(int position) {
listView.setItemChecked(position, true);
}

public void setTitle(String title)
{
getActionBar().setTitle(title);

class MyAdapter extends BaseAdapter {
private Context context;
String[] planets;

int[] images = { R.drawable.home, R.drawable.workout, R.drawable.week,
R.drawable.gallery, R.drawable.statistics, R.drawable.settings};

public MyAdapter(Context context) {
this.context=context;
planets=context.getResources().getStringArray(R.array.planets);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return planets.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return planets[position];
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View row =null;

if (convertView==null)
{
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

row=inflater.inflate(R.layout.custom_row, parent, false);
}
else
{
row = convertView
}

row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent((Activity) context, Workouts.class));
}
});

TextView titleTextView = (TextView) row.findViewById(R.id.textView1);
ImageView titleImageView = (ImageView) row.findViewById(R.id.imageView1);

titleTextView.setText(planets[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}
}

最佳答案

可以根据位置传递到不同的activity。

row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Switch(position){


case 0:
//startActivity 1
break;
case 1:
//startActivity 2
break;

/**** continue **********/
}

}
});

关于java - 如何用我的 listView 项目打开不同的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30234690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com