有谁知道如何突出在Android的ListView中选定的项目? [英] Does anyone know how to highlight a selected item in a Android listView?
问题描述
我想创建一个列表视图,其中在攻一个项目的背景颜色会发生变化。到目前为止,在相对的看得见的电池我目前的实施效果有它的背景发生变化。例如,如果我选择在列表顶部的项目,底部项目被高亮显示。谁能帮我实现所需的功能?
我的列表活动:
公共无效的onCreate(包savedInstanceState){
Log.e(TAG启动症状的活动......);
super.onCreate(savedInstanceState);
的setContentView(R.layout.symptom_listview);
//设置列表
this.setupSymptomScreen();
startManagingCursor(CS);
//得到处理上的按钮
按钮Next按钮=(按钮)findViewById(R.id.symptom_next);
按钮返回按钮=(按钮)findViewById(R.id.symptom_goBack);
ListView的LV = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemsCanFocus(假);
SymptomAdapter适配器=新SymptomAdapter(这一点,
R.layout.symptom_item,CS,新的String [] {} DbAdapter.KEY_SYMPTOM,
新的INT [] {R.id.txt_symptom});
selectedItems =新的HashSet<字符串>();
adapter.setLookupTable(selectedItems);
setListAdapter(适配器);
lv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
如果(!selectedItems.contains(view.toString()))
{
view.setBackgroundColor(Color.CYAN);
字符串症状=((TextView中)view.findViewById(R.id.txt_symptom))的getText()的toString()。
selectedItems.add(症状);
}
其他
{
view.setBackgroundColor(Color.WHITE);
selectedItems.remove(view.toString());
}
}
});
nextButton.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
//进口选定项目到一个列表
importSelected();
//找出类别
处理();
}
});
//清单症状IDS
symptoms_id =新的ArrayList&其中;整数>(cs.getCount());
}
和我使我自己的适配器中,我已重写getView
公开查看getView(INT位置,查看convertView,ViewGroup中父){
如果(convertView!= NULL){
字符串症状=((TextView中)convertView.findViewById(R.id.txt_symptom))的getText()的toString()。
如果(!selectedItems.contains(症状))
convertView.setBackgroundColor(Color.WHITE);
}
返回super.getView(位置,convertView,父母);
}
下面是一个例子:
私有类ItemAdapter扩展SimpleAdapter {
私人诠释mItemIndex = -1;
公共ItemAdapter(上下文的背景下,
名单,LT ;?扩展地图<字符串,>>数据,INT资源,
的String []从,INT []键){
超(背景下,数据,资源,从,到);
// TODO自动生成构造函数存根
}
/ *(非Javadoc中)
* @see android.widget.SimpleAdapter#getView(INT,android.view.View,android.view.ViewGroup)
* /
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
查看查看= super.getView(位置,convertView,父母);
如果(位置== mItemIndex){
convertView.setSelected(真正的);
convertView.set pressed(真正的);
convertView.setBackgroundColor(Color.parseColor(#FF9912));
}
返回查看;
}
公共无效setSelectItem(INT指数){
mItemIndex =指数;
}
公众诠释getSelectItem(){
返回mItemIndex;
}
}
当您选择项目时, onItemClick
的功能是这样的:
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
((ItemAdapter)list.getAdapter())setSelectItem(ARG2)。
}
I'm trying to create a listview where upon tapping an item the background changes color. So far my current implementation results in the "opposite" visible cell having its background changed. For example, if I chose the top item in the list, the bottom item is highlighted. Can anyone help me implement the desired functionality?
My list Activity:
public void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "Starting Symptom Activity...");
super.onCreate(savedInstanceState);
setContentView(R.layout.symptom_listview);
// Setup list
this.setupSymptomScreen();
startManagingCursor(cs);
//get handle on buttons
Button nextButton = (Button) findViewById(R.id.symptom_next);
Button backButton = (Button) findViewById(R.id.symptom_goBack);
ListView lv =getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemsCanFocus(false);
SymptomAdapter adapter = new SymptomAdapter(this,
R.layout.symptom_item, cs, new String[] { DbAdapter.KEY_SYMPTOM },
new int[] { R.id.txt_symptom });
selectedItems = new HashSet<String>();
adapter.setLookupTable(selectedItems);
setListAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(!selectedItems.contains(view.toString()))
{
view.setBackgroundColor(Color.CYAN);
String symptom = ((TextView) view.findViewById(R.id.txt_symptom)).getText().toString();
selectedItems.add(symptom);
}
else
{
view.setBackgroundColor(Color.WHITE);
selectedItems.remove(view.toString());
}
}
});
nextButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// import selected items into a list
importSelected();
// figure out category
process();
}
});
//List for symptom ids
symptoms_id = new ArrayList<Integer>(cs.getCount());
}
And I also have made my own adapter in which I have overridden getView
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView != null){
String symptoms = ((TextView) convertView.findViewById(R.id.txt_symptom)).getText().toString();
if(!selectedItems.contains(symptoms))
convertView.setBackgroundColor(Color.WHITE);
}
return super.getView(position, convertView, parent);
}
Here is an example:
private class ItemAdapter extends SimpleAdapter {
private int mItemIndex = -1;
public ItemAdapter(Context context,
List<? extends Map<String, ?>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see android.widget.SimpleAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = super.getView(position, convertView, parent);
if (position == mItemIndex) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
return view;
}
public void setSelectItem(int index) {
mItemIndex = index;
}
public int getSelectItem() {
return mItemIndex;
}
}
When you select an item, the onItemClick
function is like:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
((ItemAdapter)list.getAdapter()).setSelectItem(arg2);
}
这篇关于有谁知道如何突出在Android的ListView中选定的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!