0

ListView在我的 Android 程序中使用 a 。

我有一些行,一旦单击一行,我想打开一个新活动。我怎样才能做到这一点?

我的代码:

public class MainActivity extends Activity {

private ListView listView1;
Weather weather_data[] =null;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Weather weather_data[] = new Weather[]
    {
        new Weather(R.drawable.a, "Mercedes Benz"),
        new Weather(R.drawable.b, "Xuv 500"),
        new Weather(R.drawable.c, "Audi"),
        new Weather(R.drawable.d, "Benz"),
        new Weather(R.drawable.e, "Ferrari"),
        new Weather(R.drawable.a, "Mercedes Benz"),
        new Weather(R.drawable.b, "Xuv 500"),
        new Weather(R.drawable.c, "Audi"),
        new Weather(R.drawable.d, "Benz"),
        new Weather(R.drawable.e, "Ferrari")
    };

    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);

   listView1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,long id) 
    {

         //int Integer = ((ImageView)v).getId();
        // Launching new Activity on selecting single List Item
        Weather wea=(Weather) weather_data[position];
        String product=wea.title;
         Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
         // sending data to new activity
         intent.putExtra("index", product);
         startActivity(intent);
        }
})
4

1 回答 1

0

onItemClick()在您的方法中调整以下代码。

if(position == ROW_NO_1){
    Intent intent = new Intent(YOUR_CONTEXT,FirstActivity.class);
     // sending data to new activity
     intent.putExtra("index", VALUE_OF_THIS_ROW_ITEM);
     startActivity(intent);
} else if(position == ROW_NO_2){
    Intent intent = new Intent(YOUR_CONTEXT,SecondActivity.class);
     // sending data to new activity
     intent.putExtra("index", VALUE_OF_THIS_ROW_ITEM);
     startActivity(intent);
} else if(position == ROW_NO_3){
    Intent intent = new Intent(YOUR_CONTEXT,ThirdActivity.class);
     // sending data to new activity
     intent.putExtra("index", VALUE_OF_THIS_ROW_ITEM);
     startActivity(intent);
} 

注意:这只是一个提示。您可以根据需要制定代码。

顺便问一下,您目前面临什么问题?

于 2013-06-25T11:16:02.890 回答