1

Hi in the below code how to find the last position of the friends[position].userName and then how to compare it and based on the position how to set the text for holder.text1.

For example friends[position] it reaches the last positions means then upto some position friends[position].userName values are there.Now next position I want to occupy this holder.text1.setText(groupdetails); value
Can any one help me

java

public class FriendList extends ListActivity 
{
    private static final int ADD_NEW_FRIEND_ID = Menu.FIRST;
    private static final int CREATE_GROUP_ID = Menu.FIRST+1;
    private static final int EXIT_APP_ID = Menu.FIRST + 2;
    private IAppManager imService = null;
    private FriendListAdapter friendAdapter;
    String groupdetails;
    public String ownusername = new String();

    private class FriendListAdapter extends BaseAdapter 
    {       
        class ViewHolder {
            TextView text,text1;

            ImageView icon;
        }
        private LayoutInflater mInflater;
        private Bitmap mOnlineIcon;
        private Bitmap mOfflineIcon;        

        private FriendInfo[] friends = null;


        public FriendListAdapter(Context context) {
            super();            

            mInflater = LayoutInflater.from(context);

            mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
            mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);

        }

        public void setFriendList(FriendInfo[] friends)
        {
            this.friends = friends;
            }


        public int getCount() {     

            return friends.length;
        }


        public FriendInfo getItem(int position) {           

            return friends[position];
        }

        public long getItemId(int position) {

            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;


            if (convertView == null) 
            {
                convertView = mInflater.inflate(R.layout.friend_list_screen,null);


                holder = new ViewHolder();

                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.text1 = (TextView) convertView.findViewById(R.id.text1);

                holder.icon = (ImageView) convertView.findViewById(R.id.icon);                                       

                convertView.setTag(holder);
            }   
            else {

                holder = (ViewHolder) convertView.getTag();

            }

            holder.text.setText(friends[position].userName);


            if((friends[position]).userName.length()>18)
            {
                holder.text1.setVisibility(View.VISIBLE);
                holder.text1.setText(groupdetails);
            }

            holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);


            return convertView;
        }

    }
4

1 回答 1

0

getView方法仅根据方法返回的计数显示列表中的行getCount

所以我正在考虑你正在创建类构造函数,其中扩展了一些适配器。在 ArrayList 中再添加一个虚拟项目,当前使用 Array 使用 Arraylist:

 ArrayList<FriendInfo> listFriends;
public void setFriendList(FriendInfo[] friends)
  {
        this.listFriends=new ArrayList<FriendInfo>(Arrays.asList(friends));
        FriendInfo objectGroup=new FriendInfo();
        objectGroup.userName=groupdetails;
        listFriends.add(objectGroup);
  }

现在使用in AdapterlistFriends代替friendsin getCount(),getItem方法。ListView 将显示groupdetails在 ListView 的最后一行。

于 2015-02-13T11:33:36.593 回答