我无法做最简单的事情:-)(我想它应该很简单)
单击按钮时,我尝试在列表视图中突出显示项目。我点击没有任何反应: - (
有没有人看到,我对下面的代码做错了什么,或者有人有提示吗?
启动活动时,会显示列表视图,并突出显示第三项(int isymbolindex = 2;)。但是当尝试更改onClick或onItemClick中的选择时,它不会wotk。调试它时,它通过'public View getView'方法并且isymbolindex = 3,但是gui(listview)没有更新。
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();
}
});
}
}
答案 0 :(得分:1)
您可以在致电requestFocusFromTouch()
方法
setSelection()
或
也许
ListView.setItemChecked(int position, boolean checked);
答案 1 :(得分:0)
你也可以尝试这个
在您拥有列表视图的主布局上添加此
android:background="?android:attr/activatedBackgroundIndicator"
创建选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#ffffff" />
</shape>
在活动
中listView.setSelector(R.drawable.listview_item_selection_effect);
listView.setItemChecked(3, true);
onClick of button call this setItemChecked and pass the postion of the item