本文介绍了Android的ListView中自定义适配器getView()从来没有所谓了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个新的Android developer.I我自定义列表视图中发现的问题。我不能够进入到 getView()
方法,但我能够进入 getCount将()
method.Kindly帮我,我没有找到任何合适的答案,同时寻找问题。任何建议。先谢谢了。
活动code:
CustomAdapterAddFriend适配器=新CustomAdapterAddFriend(SearchActivity.this,friendsArrayList); mListView.setAdapter(适配器); // friendsArrayList为String类型的ArrayList中
适配器code以下:
公共类CustomAdapterAddFriend延伸BaseAdapter { 上下文语境;
ArrayList的<串GT;名单;
Image_Loader imgLoader;
公共CustomAdapterAddFriend(上下文的背景下,ArrayList的<串GT;名单){
this.context =背景;
this.lists =名单;
} @覆盖
公众诠释的getCount(){
// TODO自动生成方法存根
Log.e(Sizee ==,+ lists.size());
Log.e(名称===,+ lists.get(0));
返回lists.size(); } @覆盖
公共对象的getItem(INT为arg0){
// TODO自动生成方法存根
返回lists.get(为arg0);
} @覆盖
众长getItemId(INT为arg0){
// TODO自动生成方法存根
返回0;
} @TargetApi(Build.VERSION_ codeS.GINGERBREAD)
@燮pressLint(NewApi)
@覆盖
公共查看getView(INT POS,查看convertView,ViewGroup中ARG2){
ViewHolder支架=无效;
Log.e(Sizee ==内,+ lists.size());
Log.e(名===内部,+ lists.get(POS));
如果(android.os.Build.VERSION.SDK_INT> 9){
。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建();
StrictMode.setThreadPolicy(政策);
} LayoutInflater mInflater =(LayoutInflater)上下文
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.friend_search_list_row,NULL);
持有人=新ViewHolder();
holder.image =(ImageView的)convertView.findViewById(R.id.addbtn);
holder.mName =(TextView中)convertView.findViewById(R.id.friendname);
holder.mSex =(TextView中)convertView.findViewById(R.id.friendsex); convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
}
字符串CurrentString = lists.get(POS)
的String [] =分离CurrentString.split(#* @);
字符串名称=分离[0];
串性;分离[1]; Log.e(名称===,+名称);
holder.mName.setText(名);
holder.mSex.setText(性);// INT装载机= R.drawable.boy;
// imgLoader.DisplayImage(list.get(POS),装载机,holder.image);
返回convertView; } 类ViewHolder {
ImageView的形象;
TextView的MNAME,MSEX;
}
}
解决方案
请返回位置
从 getItemId(INT位置)
方法。
像
@覆盖
众长getItemId(INT位置){返回的位置;
}
I am a new Android developer.I am finding problem in custom listview. I am not able to enter into getView()
method but I am able to enter into getCount()
method.Kindly help me.I don't found any suitable answer while searching for problem. Any suggestion. Thanks in advance.
Activity Code:
CustomAdapterAddFriend adapter = new CustomAdapterAddFriend(SearchActivity.this,friendsArrayList);
mListView.setAdapter(adapter);//friendsArrayList is ArrayList of String type
Adapter code below:
public class CustomAdapterAddFriend extends BaseAdapter {
Context context;
ArrayList<String> lists;
Image_Loader imgLoader ;
public CustomAdapterAddFriend(Context context, ArrayList<String> lists) {
this.context = context;
this.lists = lists;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.e("Sizee==", ""+lists.size());
Log.e("name===", ""+lists.get(0));
return lists.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return lists.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
public View getView(int pos, View convertView, ViewGroup arg2) {
ViewHolder holder = null;
Log.e("Sizee== inside", ""+lists.size());
Log.e("name=== inside", ""+lists.get(pos));
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.friend_search_list_row, null);
holder = new ViewHolder();
holder.image=(ImageView)convertView.findViewById(R.id.addbtn);
holder.mName=(TextView)convertView.findViewById(R.id.friendname);
holder.mSex=(TextView)convertView.findViewById(R.id.friendsex);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String CurrentString=lists.get(pos);
String[] separated = CurrentString.split("#*@");
String name =separated[0];
String sex=separated[1];
Log.e("name===", ""+name);
holder.mName.setText(name);
holder.mSex.setText(sex);
// int loader = R.drawable.boy;
// imgLoader.DisplayImage(list.get(pos) , loader, holder.image);
return convertView;
}
class ViewHolder {
ImageView image;
TextView mName,mSex;
}
}
解决方案
Try returning position
from getItemId(int position)
method.like
@Override
public long getItemId(int position) {
return position;
}
这篇关于Android的ListView中自定义适配器getView()从来没有所谓了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!