Reputation: 414
i have one main activity class in which i define string array which fetch list from XML file saved in res/value which contain list of item . now i want that when any item selected it color change to yellow . how to do this please help :)
thanks in advance ::)
my two file's are list_data.XML and Main Activity.java
List_data.XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="tias_list">
<item>About us</item>
<item> Offered</item>
<item> year </item>
<item>Process</item>
<item>item1</item>>
<item>item2</item>>
<item>item3</item>>
<item>item4</item>
</string-array>
</resources>
MY Main Activity class code is
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] tias_list = getResources().getStringArray(R.array.tias_list);
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label,tias_list));
ListView lv = getListView();
lv.setCacheColorHint(0);
lv.setBackgroundResource(R.drawable.black);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,int position, long id) {
if (position == 0)
{
Intent myIntent = new Intent(getApplicationContext(), SingleListItem.class);
startActivity(myIntent);
}
else if(position == 1)
{
Intent myIntent = new Intent(getApplicationContext(), lastyear.class);
startActivity(myIntent);
}
else if(position == 2)
{
Intent myIntent = new Intent(getApplicationContext(), la.class);
startActivity(myIntent);
}
else if(position == 3)
{
Intent myIntent = new Intent(getApplicationContext(),la.class);
startActivity(myIntent);
}
else if(position == 4)
{
Intent myIntent = new Intent(getApplicationContext(), SingleListItem.class);
startActivity(myIntent);
}
else if(position == 5)
{
Intent myIntent = new Intent(getApplicationContext(), SingleListItem.class);
startActivity(myIntent);
}
else if(position == 6)
{
Intent myIntent = new Intent(getApplicationContext(), SingleListItem.class);
startActivity(myIntent);
}
else if(position == 7)
{
Intent myIntent = new Intent(getApplicationContext(), SingleListItem.class);
startActivity(myIntent);
}
}
});
}
Upvotes: 0
Views: 435
Reputation: 14398
To set color of selected Item use
view.setBackgroundColor(Color.YELLOW);
i.e in your code as
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View view,int position, long id) {
view.setBackgroundColor(Color.YELLOW);
// your code
Upvotes: 0
Reputation: 7439
IN your getView method, you have to do some code over there.
int currentPosition=0;
if (position == currentPosition) {
convertView.setBackgroundColor(Color.YELLOW);
} else {
convertView.setBackgroundColor(Color.YOUR DEFAULT COLOR);
}
//click item code:
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
currentPosition = position;
adapter.notifyDataSetChanged();
}
});
Upvotes: 0
Reputation: 8023
The adapter that you are currently using is the Default Adapter. However, now you want to modify it's properties. To do so you'll need to implement a custom adapter - an adapter of your own. Just Google Custom ListView Adapter if you don't know how to do that.
And in that custom adapter, there'll be a getView() method and in the getView() method of the custom adapter check if the position of item is same as the selected item. If yes, set background colour.
Here's a complete answer : https://stackoverflow.com/a/16978159/1239966
Upvotes: 1