Well, its simple. Here is how I do it.
Firstly you need two images for showing the highligted and normal state of the ListView Item. Then you need to use custom row for your ListView. Lets say it has a TextView in it.
Keep an integer variable in your Activity. this integer will keep the selected index of the ListView. Initially assign it -1 value.
Now, as you are using custom Adapter, you can check in you getView() that whether the int variable == postition. When it is same, set the background of the TextView as Highlighted, otherwise, normal. And override the OnListItemClick(). In this, save the position in that int variable and call notifyDataSetChanged(). Below is the sample code for this:
public class MySampleActivity extends ListActivity
{
ArrayList<String> lst_string = new ArrayList<String>();
int selected_item = -1;
MyListAdapter adptr;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lst_string.add("item 1");
lst_string.add("item 2");
lst_string.add("item 3");
lst_string.add("item 4");
lst_string.add("item 5");
lst_string.add("item 6");
lst_string.add("item 7");
lst_string.add("item 8");
lst_string.add("item 9");
adptr = new MyListAdapter();
setListAdapter(adptr);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
selected_item = position;
adptr.notifyDataSetChanged();
}
private class MyListAdapter extends BaseAdapter
{
@Override
public int getCount()
{
return lst_string.size();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
private LayoutInflater mInflater;
public MyListAdapter()
{
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.tv_row);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(lst_string.get(position));
if(selected_item == position)
{
holder.textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_selected));
}
else
{
holder.textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.list_normal));
}
return convertView;
}
}
public static class ViewHolder
{
public TextView textView;
}
}