public class SimpleAdapterForClickChangeColor extends SimpleAdapter {

    public SimpleAdapterForClickChangeColor(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {   
        super(context, items, resource, from, to); 
    }  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
     convertView = null;     convertView = super.getView(position, convertView, parent);   
    
     convertView.setBackgroundColor(Color.TRANSPARENT);
    
     //点击变色
     convertView.setOnClickListener(new OnClickListener(){
    
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//此处怎么写
}
    
     });
    
    
     //改变item的高度
     ViewGroup.LayoutParams params=convertView.getLayoutParams();
        params.height=50;
        convertView.setLayoutParams(params);        return convertView;
        
    }
  
}
请问:onClick方法里怎么写?谢谢

解决方案 »

  1.   

    convertView.setBackgroundColor(context.getResources().getColor(R.color.blue));
      

  2.   

    看起来和SimpleAdapter无关啊,不用这么写,直接setOnItemClickListener,获得点击的View,改个背景色,再把这个View存到tag中,点别的把这个改回原来的颜色,看楼主那段代码性能很差
      

  3.   

    我以前就是那么做的。但是很多问题啊~
    android的listView有滚动条时(即实际Item数比显示出来的Item数多),其点击item变色代码入下:
    for(int i=0;i<parent.getCount();i++){
    View v=parent.getChildAt(i);
    if (position == i) {
    v.setBackgroundColor(0xFFB679D4);
    } else {
    v.setBackgroundColor(Color.TRANSPARENT);//此行空指针错
    }
    }
    点击时v.setBackgroundColor(Color.TRANSPARENT);报空指针错。
    错误分析:因为实际Item数比显示出来的Item数多,即parent.getCount()>parent.getChildCount(),所以View v=parent.getChildAt(i);当v不在显示的view内时,得到的v为null。所以报错
    尝试方法1:parent.getCount()改成parent.getChildCount(),此时出现的新问题是往下滑动时,没有点击的Item也会变色。
    尝试方法2:parent.getCount()改成一个整数,该整数=parent.getChildCount(),此时没有往下滑动时正常,但是往下滑动时并不能点击变色。
    尝试方法3:做分页,代价太大。所以放弃
    麻烦各位给出解决方案~~困扰很久了~
      

  4.   

    太难描述了,贴代码了哈,希望你能看懂。。
    package com.renrenwei.adapter;import com.renrenwei.activity.R;import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;public class ActivityGroupAdapter extends BaseAdapter {
    private int bottomnum;
    private LayoutInflater layoutinflater;
    private View myview;
    private int index = 0; public ActivityGroupAdapter(Context c, int bottomnum) {
    this.bottomnum = bottomnum;
    this.layoutinflater = LayoutInflater.from(c);
    } public int getCount() {
    return bottomnum;
    } public void SetFocus(int index) {
    this.index = index;
    // this.notifyDataSetChanged();
    this.notifyDataSetInvalidated();// 刷新界面
    } public Object getItem(int position) {
    return position;
    } public long getItemId(int position) {
    return position;
    } public View getView(int position, View convertView, ViewGroup parent) {
    myview = layoutinflater.inflate(R.layout.group_bottom_item, null);
    ImageView imageview = (ImageView) myview
    .findViewById(R.id.imageview_bottom);
    TextView textview = (TextView) myview
    .findViewById(R.id.textview_bottom);
    if (position == 0) {
    imageview.setBackgroundResource(android.R.drawable.ic_menu_mapmode);
    textview.setText(R.string.Group_Item_Main);
    }
    if (position == 1) {
    imageview
    .setBackgroundResource(android.R.drawable.ic_menu_myplaces);
    textview.setText(R.string.Group_Item_My);
    }
    if (position == 2) {
    imageview.setBackgroundResource(android.R.drawable.ic_menu_share);
    textview.setText(R.string.Group_Item_square);
    }
    if (position == 3) {
    imageview.setBackgroundResource(android.R.drawable.ic_menu_more);
    textview.setText(R.string.Group_Item_More);
    }
    if (position == index) {
    myview.setBackgroundResource(R.drawable.css_biaoqian);
    } else {
    myview.setBackgroundResource(0);
    }
    return myview;
    }
    }
    在你的点击事件中把position传递过来
    class BottomListener implements OnItemClickListener { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
    SwitchActivity(arg2);
    }
    }
    方法就是这样。改变颜色等你自己在getview里面搞
      

  5.   

    晕list.setOnItemClickListener(new OnItemClickListener() { @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    View tag=(View)parent.getTag();
    if (tag != null)
    {
        if (tag.getId()==id) return;
         tag.setBackgroundColor(color1);
    }
    view.setBackgroundColor(color2);
    parent.setTag(view);
    }
    });
      

  6.   

    还有一点补充,SimpleAdapter.getView可能要重设颜色为初始值
    public View getView(int position, View convertView, ViewGroup parent) {
    View convertView可以直接拿来用的
      

  7.   

    Thank you!最后还有个小问题 如果在getview中加入convertView.setBackgroundColor(Color.TRANSPARENT);来解决上述问题,那么滑下去又回到原来的位置时,选中的没有变色,请问有没有好的解决方案~
      

  8.   

    简单啊,记下选中的位置,还是getView中判断,到它了,判断一下要不要改颜色
      

  9.   

    哥  convertView.setOnClickListener()事件屏蔽了activity的setOnItemClickListener()事件。。
      

  10.   

    @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
        
         convertView = null;
        
         convertView = super.getView(position, convertView, parent);  
        
         convertView.setBackgroundColor(Color.TRANSPARENT);
        
         convertView.setTag(new Integer(position));
        
         Log.i("convertView.getTag()",String.valueOf(convertView.getTag()));
         Log.i("selectedPosition",String.valueOf(selectedPosition));
        
         if(convertView.getTag().equals(selectedPosition)){ 
         convertView.setBackgroundColor(0xFFB679D4);
         Log.i("equals",String.valueOf(selectedPosition)+"xxxx");
         }
        
         convertView.setOnClickListener(new OnClickListener(){ @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    selectedPosition = (Integer)v.getTag();
    Log.i("selectedPosition",String.valueOf(selectedPosition)+",,,,,,");
    }
        
         });
        
        
         //改变item的高度
         ViewGroup.LayoutParams params=convertView.getLayoutParams();
            params.height=50;
            convertView.setLayoutParams(params);        return convertView;
            
        }
    中的convertView.setOnClickListener()事件屏蔽了
    //板块列表点击事件
    plateList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView< ?> parent, View view,
    int position, long id) {

    Log.i("selectedPlateName","selectedPlateName"); View tag=(View)parent.getTag();
                    if (tag != null)
                    {
                        if (tag.getId()==id) return;
                        tag.setBackgroundColor(Color.TRANSPARENT);
                    }
                    view.setBackgroundColor(0xFFB679D4);
                    parent.setTag(view);                 ListView listView = (ListView) parent;
    HashMap<String, String> itemData = (HashMap< String, String>) listView.getItemAtPosition(position);
    selectedPlateName = itemData.get("name");
    Log.i("selectedPlateName",selectedPlateName);
    }
    });
    事件,求解决方案~
      

  11.   

    convertView.setBackgroundColor(context.getResources().getColor(R.color.blue));
      

  12.   

    解决方案时不要把convertView.setOnClickListener(new OnClickListener(){写在getView中,你最好先理解下void android.widget.AbsSpinner.setAdapter(SpinnerAdapter adapter)
      

  13.   

    呵呵 不用这个事件convertView.setOnClickListener(new OnClickListener()怎么记录被点击的位置?
      

  14.   

    这样就算记录下来了,又怎么传入到getView中?难道要做出静态变量?
      

  15.   

    可以用一个set方法进去
    mListView.setOnItemClickListener(new OnItemClickListener()
    {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id)
    {
    adapter.setmSelectItem(position);
    adapter.notifyDataSetInvalidated();
      

  16.   

    更改了背景色之后怎样使更改后背景色全屏呢?也就是说更改颜色之后,gridView内部组件并没有占满边缘。
      

  17.   

    更改了item的背景颜色,颜色应该占满整个item,可是边缘并没有占满?难道是我的xml的配置问题么?
      

  18.   

    new ColorDrawable(Color.TRANSPARENT)