android listview 动态改变item内空间的颜色

系统 1865 0

做一个词典,要将测试的结果在最后显示出来。

举例:

单词:anderstand

选项:A:知道

         B:牛

         C:大便

         D:热火队

 

显然答案是 A。

做测测试的时候,类似这样的单词测试有50个,每次都会让你选择一个的。比如我选择了

B:牛,那么这是错的。

 

我要在最后的单词测试中将显示:

 

单词:anderstand

选项: A:知道

         B:牛

         C:大便

         D:热火队

 

将正确的答案的颜色设置成为蓝色,选错的答案设置成为红色,类似于


android listview 动态改变item内空间的颜色

-----------------------------------------------------------------------------------------------》》》

也就是动态的显示我们a b  c  d四个选项的颜色喽。

listview用的是适配器是继承了baseadapter,所以我们从网上找答案,

(其实,我相信,好多同学朋友,起初使用listview都是通过百度,或者是谷歌来找代码做demo的)

于是百度,谷歌下,

 

http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

代码:

    public class MyAdapter extends BaseAdapter{
 
        private LayoutInflater mInflater;
         
         
        public MyAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size();
        }
 
        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }
 
        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
             
            ViewHolder holder = null;
            if (convertView == null) {
                 
                holder=new ViewHolder();  
                 
                convertView = mInflater.inflate(R.layout.vlist2, null);
                holder.img = (ImageView)convertView.findViewById(R.id.img);
                holder.title = (TextView)convertView.findViewById(R.id.title);
                holder.info = (TextView)convertView.findViewById(R.id.info);
                holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
                convertView.setTag(holder);
                 
            }else {
                 
                holder = (ViewHolder)convertView.getTag();
            }
             
             
            holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
            holder.title.setText((String)mData.get(position).get("title"));
            holder.info.setText((String)mData.get(position).get("info"));
             
            holder.viewBtn.setOnClickListener(new View.OnClickListener() {
                 
                @Override
                public void onClick(View v) {
                    showInfo();                 
                }
            });
             
             
            return convertView;
        }
         
    }
  
 

还有: http://xqjay19910131-yahoo-cn.iteye.com/blog/1167956

代码:

 

    public class MyAdapter extends BaseAdapter{  
          
        private LayoutInflater flater;  
          
        public MyAdapter(Context context) {  
            super();  
            this.flater = LayoutInflater.from(context);  
        }  
  
        public int getCount() {  
            // TODO Auto-generated method stub  
            return list.size();  
        }  
  
        public Object getItem(int arg0) {  
            // TODO Auto-generated method stub  
            return null;  
        }  
  
        public long getItemId(int arg0) {  
            // TODO Auto-generated method stub  
            return 0;  
        }  
  
        public View getView(int position, View convertView, ViewGroup parent) {  
            // TODO Auto-generated method stub  
            ViewHolder holder = null;  
            if(convertView == null){  
                holder = new ViewHolder();  
                convertView = flater.inflate(R.layout.user2, null);  
                holder.headimg = (ImageView) convertView.findViewById(R.id.headimg);  
                holder.nickname = (TextView) convertView.findViewById(R.id.nickname);  
                holder.textsign = (TextView) convertView.findViewById(R.id.textsign);  
                holder.listbtn = (Button) convertView.findViewById(R.id.listbtn);  
                convertView.setTag(holder);  
            }  
            else{  
                holder = (ViewHolder) convertView.getTag();  
            }  
            holder.headimg.setBackgroundResource((Integer)list.get(position).get("headimg"));  
            holder.nickname.setText((String)list.get(position).get("nickname"));  
            holder.textsign.setText((String)list.get(position).get("textsign"));  
            holder.listbtn.setOnClickListener(new View.OnClickListener() {  
                public void onClick(View arg0) {  
                }  
            });  
            return convertView;  
        }  
    }  
}  
  
 

又或者两者的代码是一样的。

于是,我弄他们的代码下来到我的代码中就成了这样的:

     // 查询历史上搜索过的单词 ,查询的表是history_word
	private class TestWordResutListAdapter extends BaseAdapter {
		// List<Map<String , String>> lst ;
		private LayoutInflater mInflater;
		public TestWordResutListAdapter(Context context, List<Map<String, String>> lst) {
			// this.lst = lst ;
			this.mInflater = LayoutInflater.from(context);
			
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lst.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 获取ListView 每个布局文件
			
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(R.layout.wordtestresult_list_item, null); // 获取每个ListView行的布局
			}
			
			
			//获得item word单词的id的值的
			TextView tv_wordtestresult_item_id = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_id); // 显示的单词在屏幕上的顺序
			//显示序号的
			TextView tv_wordtestresult_item_index = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_index); // 显示的单词在屏幕上的顺序
			//显示单词的
			TextView tv_wordtestresult_item_word = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_word); // 显示的单词在屏幕上的顺序
			//显示答案1
			TextView tv_wordtestresult_item_meana = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_meana); // 显示的单词在屏幕上的顺序
			//显示答案2
			TextView tv_wordtestresult_item_meanb = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_meanb); // 显示的单词在屏幕上的顺序
			//显示答案3
			TextView tv_wordtestresult_item_meanc = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_meanc); // 显示的单词在屏幕上的顺序
			//显示答案4
			TextView tv_wordtestresult_item_meand = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_meand); // 显示的单词在屏幕上的顺序
			
		
			
			// 根据List lists的值进行设置
			Map<String, String> wordListMap = (Map<String, String>) lst
					.get(position);
			
			
			String id = wordListMap.get("id")  ;
			String keyid = "[" +  wordListMap.get("keyid") + "]";
			String name = wordListMap.get("name");
			String ans_A = wordListMap.get("ans_A");
			String ans_B = wordListMap.get("ans_B");
			String ans_C = wordListMap.get("ans_C");
			String ans_D = wordListMap.get("ans_D");
			
			String ans_index = wordListMap.get("ans_index").trim();
			String ans_press = wordListMap.get("ans_press").trim();
			
			System.out.println("------->>ans_index" + ans_index + "------>>id=" + id);
			System.out.println("------->>ans_press" + ans_press );
			
			tv_wordtestresult_item_meanb.setTag(id) ;
			
			if (ans_index.equals("1")) {  //为正确答案标记绿色的颜色
				tv_wordtestresult_item_meana.setTextColor(Color.GREEN) ;
				System.out.println("-------1>>id =" + id);
				
			}
			else if (ans_index.equals("2")) {
				
				tv_wordtestresult_item_meanb.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------2>>id =" + id);
			
			}
			else if (ans_index.equals("3")) {
				tv_wordtestresult_item_meanc.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------3>>id =" + id);
			
            }
			else if (ans_index.equals("4")) {
				tv_wordtestresult_item_meand.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------4>>id =" + id);
			
            }
			
			
			
			if (!ans_index.equals(ans_press)) { //为选择错误的标记颜色为红色
				System.out.println("ans_press------->>id =" + id);
				if (ans_press.equals("1")) {
					System.out.println("ans_press-------1>>id =" + id);
					tv_wordtestresult_item_meana.setTextColor(Color.RED) ;
				}
				
				else if (ans_press.equals("2")) {
					System.out.println("ans_press-------2>>id =" + id);
					tv_wordtestresult_item_meanb.setTextColor(Color.RED) ;
				}
				else if (ans_press.equals("3")) {
					System.out.println("ans_press-------3>>id =" + id);
					tv_wordtestresult_item_meanc.setTextColor(Color.RED) ;
                }
				else if (ans_press.equals("4")) {
					System.out.println("ans_press-------4>>id =" + id);
					tv_wordtestresult_item_meand.setTextColor(Color.RED) ;
                }
			}
			
			
			tv_wordtestresult_item_id.setText(id) ;
			tv_wordtestresult_item_index.setText(keyid) ;
			tv_wordtestresult_item_word.setText(name) ;
			tv_wordtestresult_item_meana.setText("A) " +ans_A) ;
			tv_wordtestresult_item_meanb.setText("B) " +ans_B) ;
			tv_wordtestresult_item_meanc.setText("C) " +ans_C) ;
			tv_wordtestresult_item_meand.setText("D) " +ans_D) ;
			
			tv_wordtestresult_item_id = null ;
			tv_wordtestresult_item_index = null ;
			tv_wordtestresult_item_word = null ;
			tv_wordtestresult_item_meana = null ;
			tv_wordtestresult_item_meanb = null ;
			tv_wordtestresult_item_meanc = null ;
			tv_wordtestresult_item_meand = null ;
			
			ans_index = null ;
			ans_press = null ;
			return convertView;
		}

	} // end class

  
 

  这里的

    			
			tv_wordtestresult_item_meana 
			tv_wordtestresult_item_meanb
			tv_wordtestresult_item_meanc 
			tv_wordtestresult_item_meand 
  

item的四个控件是的眼神是根据item中,从数据库来的值进行设置的

    		String ans_index = wordListMap.get("ans_index").trim();
			String ans_press = wordListMap.get("ans_press").trim();
			
			System.out.println("------->>ans_index" + ans_index + "------>>id=" + id);
			System.out.println("------->>ans_press" + ans_press );
			
			tv_wordtestresult_item_meanb.setTag(id) ;
			
			if (ans_index.equals("1")) {  //为正确答案标记绿色的颜色
				tv_wordtestresult_item_meana.setTextColor(Color.GREEN) ;
				System.out.println("-------1>>id =" + id);
				
			}
			else if (ans_index.equals("2")) {
				
				tv_wordtestresult_item_meanb.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------2>>id =" + id);
			
			}
			else if (ans_index.equals("3")) {
				tv_wordtestresult_item_meanc.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------3>>id =" + id);
			
            }
			else if (ans_index.equals("4")) {
				tv_wordtestresult_item_meand.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------4>>id =" + id);
			
            }
			
			
			
			if (!ans_index.equals(ans_press)) { //为选择错误的标记颜色为红色
				System.out.println("ans_press------->>id =" + id);
				if (ans_press.equals("1")) {
					System.out.println("ans_press-------1>>id =" + id);
					tv_wordtestresult_item_meana.setTextColor(Color.RED) ;
				}
				
				else if (ans_press.equals("2")) {
					System.out.println("ans_press-------2>>id =" + id);
					tv_wordtestresult_item_meanb.setTextColor(Color.RED) ;
				}
				else if (ans_press.equals("3")) {
					System.out.println("ans_press-------3>>id =" + id);
					tv_wordtestresult_item_meanc.setTextColor(Color.RED) ;
                }
				else if (ans_press.equals("4")) {
					System.out.println("ans_press-------4>>id =" + id);
					tv_wordtestresult_item_meand.setTextColor(Color.RED) ;
                }
			}
  
 

于是,做好以后,运行程序,当下拉上拉listview的时候,item的颜色是不对的,就是说,不该有颜色的控件,也添加了颜色。

可能是listview这样的情况大家遇到的比较少,我网上查了下,没有这样的例子。苦逼了,最后的居然是这样的:

 

 

    @Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 获取ListView 每个布局文件
			
    
      convertView = null ;
    
    
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(R.layout.wordtestresult_list_item, null); // 获取每个ListView行的布局
			}
			
			
			//获得item word单词的id的值的
			TextView tv_wordtestresult_item_id = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_id); // 显示的单词在屏幕上的顺序
			//显示序号的
			TextView tv_wordtestresult_item_index = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_index); // 显示的单词在屏幕上的顺序
			//显示单词的
			TextView tv_wordtestresult_item_word = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_word); // 显示的单词在屏幕上的顺序
			//显示答案1
			TextView tv_wordtestresult_item_meana = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_meana); // 显示的单词在屏幕上的顺序
			//显示答案2
			TextView tv_wordtestresult_item_meanb = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_meanb); // 显示的单词在屏幕上的顺序
			//显示答案3
			TextView tv_wordtestresult_item_meanc = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_meanc); // 显示的单词在屏幕上的顺序
			//显示答案4
			TextView tv_wordtestresult_item_meand = (TextView) convertView
			.findViewById(R.id.tv_wordtestresult_item_meand); // 显示的单词在屏幕上的顺序
			
		
			
			// 根据List lists的值进行设置
			Map<String, String> wordListMap = (Map<String, String>) lst
					.get(position);
			
			
			String id = wordListMap.get("id")  ;
			String keyid = "[" +  wordListMap.get("keyid") + "]";
			String name = wordListMap.get("name");
			String ans_A = wordListMap.get("ans_A");
			String ans_B = wordListMap.get("ans_B");
			String ans_C = wordListMap.get("ans_C");
			String ans_D = wordListMap.get("ans_D");
			
			String ans_index = wordListMap.get("ans_index").trim();
			String ans_press = wordListMap.get("ans_press").trim();
			
			System.out.println("------->>ans_index" + ans_index + "------>>id=" + id);
			System.out.println("------->>ans_press" + ans_press );
			
			tv_wordtestresult_item_meanb.setTag(id) ;
			
			if (ans_index.equals("1")) {  //为正确答案标记绿色的颜色
				tv_wordtestresult_item_meana.setTextColor(Color.GREEN) ;
				System.out.println("-------1>>id =" + id);
				
			}
			else if (ans_index.equals("2")) {
				
				tv_wordtestresult_item_meanb.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------2>>id =" + id);
			
			}
			else if (ans_index.equals("3")) {
				tv_wordtestresult_item_meanc.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------3>>id =" + id);
			
            }
			else if (ans_index.equals("4")) {
				tv_wordtestresult_item_meand.setTextColor(Color.GREEN) ;
				System.out.println("ans_index-------4>>id =" + id);
			
            }
			
			
			
			if (!ans_index.equals(ans_press)) { //为选择错误的标记颜色为红色
				System.out.println("ans_press------->>id =" + id);
				if (ans_press.equals("1")) {
					System.out.println("ans_press-------1>>id =" + id);
					tv_wordtestresult_item_meana.setTextColor(Color.RED) ;
				}
				
				else if (ans_press.equals("2")) {
					System.out.println("ans_press-------2>>id =" + id);
					tv_wordtestresult_item_meanb.setTextColor(Color.RED) ;
				}
				else if (ans_press.equals("3")) {
					System.out.println("ans_press-------3>>id =" + id);
					tv_wordtestresult_item_meanc.setTextColor(Color.RED) ;
                }
				else if (ans_press.equals("4")) {
					System.out.println("ans_press-------4>>id =" + id);
					tv_wordtestresult_item_meand.setTextColor(Color.RED) ;
                }
			}
			
			
			tv_wordtestresult_item_id.setText(id) ;
			tv_wordtestresult_item_index.setText(keyid) ;
			tv_wordtestresult_item_word.setText(name) ;
			tv_wordtestresult_item_meana.setText("A) " +ans_A) ;
			tv_wordtestresult_item_meanb.setText("B) " +ans_B) ;
			tv_wordtestresult_item_meanc.setText("C) " +ans_C) ;
			tv_wordtestresult_item_meand.setText("D) " +ans_D) ;
			
			tv_wordtestresult_item_id = null ;
			tv_wordtestresult_item_index = null ;
			tv_wordtestresult_item_word = null ;
			tv_wordtestresult_item_meana = null ;
			tv_wordtestresult_item_meanb = null ;
			tv_wordtestresult_item_meanc = null ;
			tv_wordtestresult_item_meand = null ;
			
			ans_index = null ;
			ans_press = null ;
			return convertView;
		}

	} // end class

  
 

 

如上上面的一行代码就解决了问题了。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

android listview 动态改变item内空间的颜色


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论