我使用列表作为侧边栏,并根据列表项在主窗口中启动不同的活动。现在,如何在布局或代码中更改特定列表项的颜色。所以,每当我加载新活动时,我希望特定列表项具有不同的颜色。我不能使用 Event Click here 来触发它。
这是我的代码 -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout6);
listView = (ListView) findViewById(R.id.listView);
String[] values = getResources().getStringArray(R.array.menuList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
switch( position )
{
case 0: Intent newActivity = new Intent(Activity6.this,MainActivity.class);
startActivity(newActivity);
break;
case 1: Intent newActivity1 = new Intent(Activity6.this,Activity2.class);
startActivity(newActivity1);
break;
case 2: Intent newActivity2 = new Intent(Activity6.this,Activity3.class);
startActivity(newActivity2);
break;
case 3: Intent newActivity3 = new Intent(Activity6.this,Activity4.class);
startActivity(newActivity3);
break;
case 4: Intent newActivity4 = new Intent(Activity6.this,Activity5.class);
startActivity(newActivity4);
break;
}
Toast.makeText(getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
}
});
}