主要Activity.java 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHorizontalListView = (Gallery) findViewById(R.id.horizontallistview);
myAdapter = new MyAdapter(this, itemsArray, data);
myHorizontalListView.setAdapter(myAdapter);
myHorizontalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//---------------------------------------------------------------------------
rank = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
country = new String[]{"China", "India", "United States",
"Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh",
"Russia", "Japan"};
population = new String[]{"1,354,040,000", "1,210,193,422",
"315,761,000", "237,641,326", "193,946,886", "182,912,000",
"170,901,000", "152,518,015", "143,369,806", "127,360,000"};
//---------------------------------------------------------------------------
list = (ListView) findViewById(R.id.contentlist);
adapter = new ListViewAdapter(MainActivity.this, rank, country, population);
// Binds the Adapter to the ListView
Log.e("cccccccccccccchhhhhh","" + position);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(MainActivity.this, ViewPagerActivity.class);
i.putExtra("rank", rank);
i.putExtra("country", country);
i.putExtra("population", population);
i.putExtra("position", position);
Log.e("cccccccccccccchhhhhh", "::::::::::" + position);
startActivity(i);
}
});
Toast.makeText(
MainActivity.this, parent.getItemAtPosition(position).toString() + " Clicked", Toast.LENGTH_LONG).show();
}
});
}
}
MyAdapter 代码:
public class MyAdapter extends BaseAdapter {
Context context;
String[] itemsArray;
String[] data;
public MyAdapter(Context c, String[] itemsArray, String[] data) {
context = c;
this.itemsArray = itemsArray;
this.data = data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemsArray.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemsArray[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
private class ViewHolder {
TextView itemtext;
TextView data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = new ViewHolder();
View rowView = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.row, null);
holder.itemtext = (TextView) rowView.findViewById(R.id.itemtext);
holder.data = (TextView) rowView.findViewById(R.id.itemName);
holder.itemtext.setText(itemsArray[position]);
holder.data.setText(data[position]);
return rowView;
}
}