关于在adapter使用LinearLayout的findviewbyid问题

来源:互联网 发布:js如何封装函数 编辑:程序博客网 时间:2024/05/06 04:10

         学安卓不久,最近在用自定义adaptert时遇到了几个问题,就来记到这里(为什么?,,因为我发现每次遇到问题写在这里以后对记忆好而且以后还可以看)。

      首先是findviewbyid的问题我在LinearLayout中放了一张图片和一个文本框,但是在设置textview的值时没有findviewbyid,之后发现原来要这样写:

LinearLayout lView4 = (LinearLayout) View.inflate(context,R.layout.ac_4httphelp, null);((TextView) lView4.findViewById(R.id.tv4_httphelp)).setText(m4_http[position]);
之后我就养成了        父.findviewbyid(R.id.xxx)      的习惯。在上面父(也就是说R.id.xxx在这个“父”里面)是lView4、所以写成
lView4.findViewById(R.id.tv4_httphelp);
在activity中父是activity所以我就写成<pre name="code" class="java">this.findViewById(R.id.tv4_httphelp);
怕自己说话逻辑不清,以后看不知写的是什么,粘贴一下。
<pre name="code" class="java">@Overridepublic View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) {  LinearLayout lView4 = (LinearLayout) View.inflate(context, R.layout.ac_4httphelp, null);((TextView) lView4.findViewById(R.id.tv4_httphelp)).setText(m4_http[position]);return lView4;}((TextView) convertView.findViewById(R.id.tv4_httphelp)).setText(m4_http[position]);return convertView;}



0 0
原创粉丝点击