从Android的其他活动回来的时候列表视图所选项目颜色改变 [英] listview selected item color changed when come back from other activity in android
本文介绍了从Android的其他活动回来的时候列表视图所选项目颜色改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
怎么办列表视图
Android中来自其他活动回来时,所选的项目颜色保持变化?
请给我一个妥善的解决办法。
在这里,我连着适配器
类
公共类SelectedAdapter扩展ArrayAdapter {//用来保持在ListView中选定的位置
私人INT selectedPos = -1; //不选择初始化值
私人诠释selitem;
公共SelectedAdapter(上下文的背景下,INT textViewResourceId,List对象){
超(背景下,textViewResourceId,对象);
} 公共无效setSelectedPosition(INT POS){ selectedPos = POS; //告知这个变化的看法
notifyDataSetChanged();
} 公众诠释getSelectedPosition(){
返回selectedPos;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
视图V = convertView; //只有膨胀的观点,如果它是空
如果(V == NULL){
LayoutInflater六
=(LayoutInflater)this.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.simplerow,NULL);
} //获取文本视图
TextView的标签=(TextView中)v.findViewById(R.id.rowTextView); //根据选定的状态更改行的颜色
如果(selectedPos ==位置){
label.setBackgroundColor(Color.CYAN); //label.setBackgroundResource(R.color.blue2);
}其他{
label.setBackgroundColor(Color.WHITE);
} label.setText(this.getItem(位置)的ToString());
/ *
//使用比的ToString其他东西()
MyClass的MyObj中=(MyClass的)this.getItem(位置);
label.setText(myobj.myReturnsString());
* /
(五)返回;
}}
主要活动类
最后上下文的背景下=这;
保护的ListView applv;
私人ArrayAdapter<串GT; listAdapter;
私人SelectedAdapter selectedAdapter;
私人ArrayList的列表; 名单=新的ArrayList(); list.add(执行机构);
list.add(搅拌器 - 液体);
list.add(搅拌器 - 浆);
list.add(空分);
list.add(带犁);
list.add(鼓风机);
applv =(ListView控件)findViewById(R.id.lvapps);
applv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); selectedAdapter =新SelectedAdapter(这一点,0,清单);
selectedAdapter.setNotifyOnChange(真);
applv.setAdapter(selectedAdapter);
applv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,
长ID){ itemsel = applv.getAdapter()getItemViewType(位置)。
// TODO自动生成方法存根
项目= applv.getAdapter()的getItem(位置)的ToString()。
selectedAdapter.setSelectedPosition(位置); } });
解决方案
要做到这一点ü需要保存是否选择TextView的状态。
首先创建的地图作为一个全球性以乌尔类,
地图<整数,布尔> map_textcolor =新的HashMap<整数,布尔>();
然后更改乌尔getView方法,这样的..
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
视图V = convertView; //只有膨胀的观点,如果它是空
如果(V == NULL){
LayoutInflater六
=(LayoutInflater)this.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.simplerow,NULL);
} //获取文本视图
TextView的标签=(TextView中)v.findViewById(R.id.rowTextView);
************************************************** *****************************
//根据选定的状态更改行的颜色
布尔selectedPos = map_textcolor.get(位置);
如果(selectedPos = NULL&放大器;!&安培; selectedPos.booleanValue())
{
//label.setSelected(true);
label.setTextColor(Color.CYAN);
selected_view =标签;
}
其他
{
//label.setSelected(false);
label.setTextColor(Color.WHITE);
}
************************************************** ****************************
label.setText(this.getItem(位置)的ToString());
再加入code到听者像这样...
最终诠释P =位置;
label.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
// TODO自动生成方法存根
如果(selected_view!= NULL)
{
//selected_view.setSelected(false);
label.setTextColor(Color.WHITE);
map_textcolor.remove(prev_pos);
}
selected_view =视图。
// selected_view.setSelected(真);
label.setTextColor(Color.CYAN);
map_textcolor.put(对,真);
prev_pos = P;
}
});
/ *
//使用比的ToString其他东西()
MyClass的MyObj中=(MyClass的)this.getItem(位置);
label.setText(myobj.myReturnsString());
* /
(五)返回;
}
How to do Listview
selected item color stay changed when come back from other activity in android?
Please give me a proper solution.
Here, I attached Adapter
class
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);
}
}
Main Activity class
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);
}
});
解决方案
To do so u need to save textview states that is selected or not. first create map as a global to ur class as,
Map<Integer, Boolean> map_textcolor=new HashMap<Integer, Boolean>();
then change ur getView method like this..
@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());
then add code to listner like this...
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);
}
这篇关于从Android的其他活动回来的时候列表视图所选项目颜色改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文