0
我在我的Android程序中使用了ListView
。如何在列表視圖中添加onItemClickListener以顯示該行的圖像
我有一些行,我一旦點擊了一行,我想打開一個新的活動。我怎樣才能做到這一點?
我的代碼:
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);
}
})
您的代碼面臨的問題是什麼。 – Thalaivar