String[] Locations;
ListView LocationList;
ArrayList<HashMap<String, String>> LocationAndTypeList = new ArrayList<HashMap<String, String>>(32);
int SelectedLocation;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Populate select_location
// R.array.location points to a StringArray of 25 location names
Location = getResources().getStringArray(R.array.location);
String[] FromList = new String[] {"text1", "text2"};
int[] ToList = new int[] {R.id.text1, R.id.text2};
for (int i = 0; i < Locations.length; i++)
{
HashMap<String, String> M = new HashMap<String, String>();
M.put("text1", Locations[i]);
M.put("text2", "Row " + Integer.toString(i + 1));
LocationAndTypeList.add(M);
}
// Populate the list view
LocationList = (ListView)findViewById(R.id.location_list);
SimpleAdapter LocationListAdapter = new SimpleAdapter(this, LocationAndTypeList, R.layout.twolinelistitemrow, FromList, ToList);
LocationList.setAdapter((ListAdapter)LocationListAdapter);
LocationList.setOnItemClickListener(new LocationListClickListener());
// Set the initially selected value to -1
SelectedLocation = -1;
}
// When an item is clicked, change its background color
public class LocationListClickListener implements OnItemClickListener
{
public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
{
// "Unselect" the previously selected location
if (SelectedLocation >= 0)
{
View previousLocation = parent.getChildAt(SelectedLocation);
previousLocation.setBackgroundColor(Color.TRANSPARENT);
}
SelectedLocation = pos;
view.setBackgroundColor(Color.rgb(0, 80, 0));
}
}