Android setAdapter无法正常工作的扩展片段

时间:2015-10-27 18:42:49

标签: android-layout listview android-fragments arraylist

使用从片段列表调用的tab1类扩展片段时遇到问题。我已经完成了关于在类中使用片段的研究和信息,但是现在我不能让我的setAdapter在tab1类中工作。以下是我的代码:tab1类和XML。

public class Tab1Fragment extends Fragment implements AdapterView.OnItemClickListener {

private static final int ADD_NEW_FRIEND_ID = Menu.FIRST;
private static final int SHOW_DETAILS = 3;
private static final int EXIT_APP_ID = Menu.FIRST + 1;
public IAppManager imService = null;
private FriendListAdapter friendAdapter;

private class FriendListAdapter extends BaseAdapter
{
    class ViewHolder {
        TextView text;
        ImageView icon;
    }
    private LayoutInflater mInflater;
    private Bitmap mOnlineIcon;
    private Bitmap mOfflineIcon;
    private ListView list;
    private ListView listView;

    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.icon = (ImageView) convertView.findViewById(R.id.icon);

            convertView.setTag(holder);
        }
        else {

            holder = (ViewHolder) convertView.getTag();
        }

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

        return convertView;
    }

}


public class MessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i("Broadcast receiver ", "received a message");
        Bundle extra = intent.getExtras();
        if (extra != null)
        {
            String action = intent.getAction();
            if (action.equals(IMService.FRIEND_LIST_UPDATED))
            { 
                Tab1Fragment.this.updateData(FriendController.getFriendsInfo(),
                        FriendController.getUnapprovedFriendsInfo());

            }
        }
    }

};


public MessageReceiver messageReceiver = new MessageReceiver();

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        imService = ((IMService.IMBinder)service).getService();

        FriendInfo[] friends = FriendController.getFriendsInfo(); //imService.getLastRawFriendList();
        if (friends != null) {
            Tab1Fragment.this.updateData(friends, null); // parseFriendInfo(friendList);
        }

        getActivity().setTitle(imService.getUsername() + "'s friend list");
    }
    public void onServiceDisconnected(ComponentName className) {
        imService = null;

    }
};

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View v = inflater.inflate(R.layout.tab1fragment, container, false);

   ListView list = (ListView) v.findViewById(R.id.lvn);




    return v;
}




void updateData(FriendInfo[] friends, FriendInfo[] unApprovedFriends)
{

    if (friends != null) {
        friendAdapter.setFriendList(friends);
 setAdapter(friendAdapter);


    }

    if (unApprovedFriends != null)
    {
        NotificationManager NM = (NotificationManager) getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE);

        if (unApprovedFriends.length > 0)
        {
            String tmp = new String();
            for (int j = 0; j < unApprovedFriends.length; j++) {
                tmp = tmp.concat(unApprovedFriends[j].userName).concat(",");
            }
            Notification notification = new Notification(R.drawable.stat_sample,
                    getText(R.string.new_friend_request_exist),
                    System.currentTimeMillis());

            Intent i = new Intent(Tab1Fragment.this.getActivity(), UnApprovedFriendList.class);
            i.putExtra(FriendInfo.FRIEND_LIST, tmp);

            PendingIntent contentIntent = PendingIntent.getActivity(this.getActivity(), 0,
                    i, 0);

            notification.setLatestEventInfo(this.getActivity(), getText(R.string.new_friend_request_exist),
                    "You have new friend request(s)",
                    contentIntent);


            NM.notify(R.string.new_friend_request_exist, notification);
        }
        else
        {
            // if any request exists, then cancel it
            NM.cancel(R.string.new_friend_request_exist);
        }
    }

}


  @Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id){

   onItemClick(parent, v, position, id);
   // Sensor sensor = sensorAdapter.getItem(AdapterView<?> parent

    Intent i = new Intent(Tab1Fragment.this.getActivity(), Messaging.class);
    FriendInfo friend = friendAdapter.getItem(position);
    if (friend.status == STATUS.ONLINE)
    {
        i.putExtra(FriendInfo.USERNAME, friend.userName);
        // i.putExtra(FriendInfo.ID, friend.uid); // Edit //
        i.putExtra(FriendInfo.PORT, friend.port);
        i.putExtra(FriendInfo.IP, friend.ip);
        startActivity(i);
    }
    else
    {
        Toast.makeText(Tab1Fragment.this.getActivity(), R.string.user_offline, Toast.LENGTH_SHORT).show();
    }

}



  }

标签的XML:

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/lvn"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />



<TextView   android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/no_friend"
    android:gravity="center_vertical|center_horizontal"/>

</LinearLayout>

0 个答案:

没有答案