当从android中的其他活动回来时,如何Listview
所选项目颜色保持不变?
请给我一个合适的解决方案。
在这里,我附上了Adapter
类
public class SelectedAdapter extends ArrayAdapter {
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
private int selitem;
public SelectedAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi
= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.simplerow, null);
}
// get text view
TextView label = (TextView)v.findViewById(R.id.rowTextView);
// change the row color based on selected state
if(selectedPos == position){
label.setBackgroundColor(Color.CYAN);
//label.setBackgroundResource(R.color.blue2);
}else{
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
/*
// to use something other than .toString()
MyClass myobj = (MyClass)this.getItem(position);
label.setText(myobj.myReturnsString());
*/
return(v);
}
}
主要活动类
final Context context = this;
protected ListView applv ;
private ArrayAdapter<String> listAdapter ;
private SelectedAdapter selectedAdapter;
private ArrayList list;
list = new ArrayList();
list.add("Actuator");
list.add("Agitator - Liquid");
list.add("Agitator - Slurry");
list.add("Air Separator");
list.add("Belt Plow");
list.add("Blower");
applv = (ListView) findViewById( R.id.lvapps);
applv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
selectedAdapter = new SelectedAdapter(this,0,list);
selectedAdapter.setNotifyOnChange(true);
applv.setAdapter( selectedAdapter );
applv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
itemsel = applv.getAdapter().getItemViewType(position);
// TODO Auto-generated method stub
item = applv.getAdapter().getItem(position).toString();
selectedAdapter.setSelectedPosition(position);
}
});
答案 0 :(得分:0)
为此,您需要保存所选的文本视图状态。 首先将地图创建为全局到您的类,
Map<Integer, Boolean> map_textcolor=new HashMap<Integer, Boolean>();
然后像这样改变你的getView方法..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi
= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.simplerow, null);
}
// get text view
TextView label = (TextView)v.findViewById(R.id.rowTextView);
*******************************************************************************
// change the row color based on selected state
Boolean selectedPos=map_textcolor.get(position);
if(selectedPos!=null&&selectedPos.booleanValue())
{
//label.setSelected(true);
label.setTextColor(Color.CYAN);
selected_view=label;
}
else
{
//label.setSelected(false);
label.setTextColor(Color.WHITE);
}
******************************************************************************
label.setText(this.getItem(position).toString());
然后将代码添加到listner中......
final int p=position;
label.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(selected_view!=null)
{
//selected_view.setSelected(false);
label.setTextColor(Color.WHITE);
map_textcolor.remove(prev_pos);
}
selected_view=view;
// selected_view.setSelected(true);
label.setTextColor(Color.CYAN);
map_textcolor.put(p, true);
prev_pos=p;
}
});
/*
// to use something other than .toString()
MyClass myobj = (MyClass)this.getItem(position);
label.setText(myobj.myReturnsString());
*/
return(v);
}