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