I am not able to do the simpliest things :-) ( I guess it should be simple )
I try to highlight a Item in a listview when clicking on a button. I click and nothing happens :-(
Does anyone see, what I am doing wrong with following code, or does anyone have a hint ?
When starting the activity the listview is shown and also the third item is highlighted (int isymbolindex=2;). But when try to change the selection in the onClick or onItemClick it doesn't wotk. When Debugging it goes through the 'public View getView' method and isymbolindex=3, but gui (listview ) is not updated.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CityEditorActivity extends Activity {
int isymbolindex=2;
Context croot=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city_editor);
final ListView lvsymbols= (ListView)findViewById(R.id.listViewdymbols);
String[] values = new String[] { "Value1", "Value2", "Value3", "Value4"};
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
lvsymbols.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvsymbols.setSelector(android.R.color.darker_gray);
Button bvon1=(Button)findViewById(R.id.vonadd1);
bvon1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isymbolindex=3;
lvsymbols.setSelection(3);
lvsymbols.invalidate();
}
});
lvsymbols.setAdapter( new ArrayAdapter(this, R.layout.own_simple_list_item, list)
{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View renderer = super.getView(position, convertView, parent);
if (position == isymbolindex)
{
//TODO: set the proper selection color here:
renderer.setBackgroundResource(android.R.color.darker_gray);
}else{
renderer.setBackgroundResource(android.R.color.white);
}
return renderer;
}
});
lvsymbols.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
isymbolindex=position;
arg0.setSelection(isymbolindex);
view.getFocusables(position);
view.setSelected(true);
lvsymbols.invalidate();
}
});
}
}