0

这是我的源代码,请帮助我将此列表视图链接到新活动 fe SecondActivity.class、ThirdActivity.class 和退出操作

public class MainActivity extends Activity {

private ListView listView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Weather weather_data[] = new Weather[]
    {
        new Weather(R.drawable.weather_cloudy, "Cloudy"),
        new Weather(R.drawable.weather_showers, "Showers"),
        new Weather(R.drawable.weather_snow, "Snow"),
        new Weather(R.drawable.weather_storm, "Storm"),
        new Weather(R.drawable.weather_sunny, "Sunny")
    };

    WeatherAdapter adapter = new WeatherAdapter(this,
            R.layout.listview_item_row, weather_data);


    listView1 = (ListView)findViewById(R.id.listView1);

    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);

    listView1.setAdapter(adapter);
}
4

3 回答 3

0
listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapter, View arg1, int position,
                        long arg3) {
                    // TODO Auto-generated method stub
                 Intent intent = null ;
                    switch (position) {
                case 0:
                    intent  = new Intent(MainActivity.this, FirstActivity.class);
                    startActivity(intent);          
                    break;
                case 1:
                intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                    break;
                default:
                    break;
                }


                }
            });
于 2013-05-12T21:04:43.007 回答
0

将此列表视图链接到新活动

要在 ListView 单击上打开新 Activity,您需要添加setOnItemClickListenerListView 和内部onItemClick方法,您可以打开新 Activity。这样做:

listView1.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, 
                               View arg1,int position, long arg3)
    { 
        // open new Activity here...

          Intent intent = new Intent(arg1.getContext(), SecondActivity.class);
          arg1.getContext().startActivity(intent);
    }
});

并确保您已添加SecondActivity活动AndroidManifest.xml

于 2013-05-12T21:05:10.213 回答
0

添加一个onItemClickListener到您的列表视图看到这个

或者如果使用Fragments,请使用ListFragment并覆盖该onListItemClick方法

于 2013-05-12T21:06:23.790 回答