在Android中索引OutOfBoundsException

时间:2018-01-06 08:43:53

标签: android database firebase firebase-realtime-database indexoutofboundsexception

OnCreateView方法

UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
logoView.image = [UIImage imageNamed:@"logo.png"];
[logoView setContentMode:UIViewContentModeScaleAspectFit];

UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
[workaroundImageView addSubview:logoView];

self.navigationItem.titleView = workaroundImageView;

的UserDetails

ListView usersList;
TextView noUsersText;
ArrayList<String> al = new ArrayList<>();
int totalUsers = 0;
ProgressDialog mProgressDialogue;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);

    usersList = (ListView)rootView.findViewById(R.id.usersList);
    noUsersText = (TextView)rootView.findViewById(R.id.noUsersText);

    mProgressDialogue = new ProgressDialog(getActivity());
    mProgressDialogue.setMessage("Loading...");
    mProgressDialogue.show();

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Users");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<String> arrayList = new ArrayList<>();
            ListView listView = (ListView) rootView.findViewById(R.id.usersList);
            ArrayAdapter<String> adapter;
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("Name").getValue().toString();
//                    Log.d("TAG", name);
                arrayList.add(name);
            }
            listView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(),"Please Check Your Internet Connection",Toast.LENGTH_SHORT).show();
        }
    };
    usersRef.addListenerForSingleValueEvent(eventListener);

    usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            UserDetails.chatWith = al.get(position);
            Intent intent = new Intent(getActivity(), Chat.class);
            startActivity(intent);
        }
    });
    return rootView;
}

我还是初学者。只是尝试使用电话号码进行身份验证的简单聊天应用程序。注册和登录似乎很好。甚至更新用户配置文件也没问题。然后我到达这个片段,向用户显示使用该应用程序的联系人列表。此片段成功从数据库中提取名称列表并显示它,但是当用户单击其中一个名称时,它会显示异常。用户应该被带到chat.class,在那里他将能够与其他人聊天(希望如此) 请帮忙... 在UserDetails.chatWith = al.get(Position)行中显示和IndexOUtOfBounds异常 索引0无效,大小为0

2 个答案:

答案 0 :(得分:0)

将数据保存到al,而不是像这样的新ArrayList:

@Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ListView listView = (ListView) rootView.findViewById(R.id.usersList);
            ArrayAdapter<String> adapter;
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, al);
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("Name").getValue().toString();
//                    Log.d("TAG", name);
                al.add(name);
            }
            listView.setAdapter(adapter);
        }

答案 1 :(得分:0)

使用 al arrayList 以及另外一个建议,不要在onDataChange中初始化/设置适配器。您可以在onCreateView初始化或设置它,并从onDataChange通知,如下所示:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);

    usersList = (ListView) rootView.findViewById(R.id.usersList);
    noUsersText = (TextView) rootView.findViewById(R.id.noUsersText);

    ArrayList<String> arrayList = new ArrayList<>();
    ListView listView = (ListView) rootView.findViewById(R.id.usersList);
    ArrayAdapter<String> adapter;
    listView.setAdapter(adapter);

    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

    mProgressDialogue = new ProgressDialog(getActivity());
    mProgressDialogue.setMessage("Loading...");
    mProgressDialogue.show();

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Users");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("Name").getValue().toString();
//              Log.d("TAG", name);
                arrayList.add(name);
                // or al.add(name);
            }
            adapter.notifyDataSetChanged(); 
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(), "Please Check Your Internet Connection", Toast.LENGTH_SHORT).show();
        }
    };
    usersRef.addListenerForSingleValueEvent(eventListener);

    usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            UserDetails.chatWith = arrayList.get(position);
            //UserDetails.chatWith = al.get(position);
            Intent intent = new Intent(getActivity(), Chat.class);
            startActivity(intent);
        }
    });
    return rootView;
}