Android listview自定义适配器getView()从未调用过

时间:2014-06-14 19:17:07

标签: android listview android-listview

我是一名新的Android开发人员。我在自定义列表视图中发现了问题。我无法进入getView()方法,但我能够进入getCount()方法。请帮助我。我在搜索问题时找不到合适的答案。任何建议。提前谢谢。

活动代码:

 CustomAdapterAddFriend adapter = new CustomAdapterAddFriend(SearchActivity.this,friendsArrayList);

  mListView.setAdapter(adapter);//friendsArrayList is ArrayList of String type

以下适配器代码:

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;
    }


}

2 个答案:

答案 0 :(得分:1)

尝试从position方法返回getItemId(int position)。 喜欢

@Override
public long getItemId(int position) {

return position;
}

答案 1 :(得分:-1)

更改

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

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