Android 动态ListView项未填充XML

Android 动态ListView项未填充XML,android,listview,firebase,Android,Listview,Firebase,我有一个基于Firebase侦听器更新的动态ListView。我首先填充ListView,然后随着项目添加到Firebase,我调用notifySetDataChanged()来更新ListView 代码: public class Discussion\u活动扩展了AppCompative活动{ 私有图像查看mpolimage; 私有文本视图问题; 私人编辑文集; 私人字符串库; 私人影像博物馆; 私有列表视图mPollCommentsList; 专用阵列适配器mCommentAdapter;

我有一个基于Firebase侦听器更新的动态ListView。我首先填充ListView,然后随着项目添加到Firebase,我调用notifySetDataChanged()来更新ListView

代码:

public class Discussion\u活动扩展了AppCompative活动{
私有图像查看mpolimage;
私有文本视图问题;
私人编辑文集;
私人字符串库;
私人影像博物馆;
私有列表视图mPollCommentsList;
专用阵列适配器mCommentAdapter;
私人咨询公司;
私人ArrayList McCommentArrayList;
私有数组列表mCommentIDArrayList;
私人消防基地mBaseRef;
私人消防基地mPollsRef;
私人消防基地;
私人消防基地mCommentsRef;
私有日期格式mDateFormat;
私有日期mDate;
私有字符串mCurrentDateString;
私有内联指数;
私有ChildEventListener mUpdateComments;
专用工具栏;
私有静态最终字符串FIREBASE_URL=”https://fan-polls.firebaseio.com/";
私有静态最终字符串注释\u LABEL=“COMMENTS”;
私有静态最终字符串轮询\u LABEL=“POLLS”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.activity_讨论);
工具栏=(工具栏)findViewById(R.id.tool\u栏);
设置支持操作栏(工具栏);
setTitle(R.string.discussion\u title\u text);
mDateFormat=新的SimpleDataFormat(“MM dd yyyy”);
mDate=新日期();
mCurrentDateString=mDateFormat.format(mDate);
mBaseRef=新Firebase(Firebase_URL);
mPollsRef=mBaseRef.child(轮询标签);
mPollImage=(ImageView)findViewById(R.id.comments\u image);
mPollCommentQuestion=(TextView)findViewById(R.id.poll\u comment\u question);
mUserComment=(EditText)findViewById(R.id.user\u comment);
mUserAvatar=(ImageView)findViewById(R.id.profile\u image\u avatar);
mPollCommentsList=(ListView)findViewById(R.id.poll\u comments\u list);
mCommentArrayList=新的ArrayList();
mCommentIDArrayList=newarraylist();
mCommentAdapter=newlistadapter(getApplicationContext(),R.layout.individual_comment,mCommentArrayList);
mPollCommentsList.setAdapter(mCommentAdapter);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入状态\u隐藏);
Intent=getIntent();
String pollID=intent.getStringExtra(“POLL_ID”);
mpollinex=intent.getIntExtra(“轮询索引”,0);
mUpdateRef=mPollsRef.child(mCurrentDateString.child)(String.valueOf(mpollinex+1));
mCommentsRef=mUpdateRef.child(注释和标签);
mUpdateRef.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
setImage(dataSnapshot);
设置问题(dataSnapshot);
createInitialCommentArray(dataSnapshot);
mNumberOfCommentsAtPoll=(int)dataSnapshot.child(COMMENTS_LABEL).getChildrenCount();
对于(int i=0;ipublic class Discussion_Activity extends AppCompatActivity {

private ImageView mPollImage;
private TextView mPollCommentQuestion;

private EditText mUserComment;
private String mUserID;
private ImageView mUserAvatar;
private ListView mPollCommentsList;
private ArrayAdapter<Comments> mCommentAdapter;
private int mNumberOfCommentsAtPoll;
private ArrayList<Comments> mCommentArrayList;
private ArrayList<String> mCommentIDArrayList;

private Firebase mBaseRef;
private Firebase mPollsRef;
private Firebase mUpdateRef;
private Firebase mCommentsRef;

private DateFormat mDateFormat;
private Date mDate;
private String mCurrentDateString;

private int mPollIndex;

private ChildEventListener mUpdateComments;

private Toolbar toolbar;


private static final String FIREBASE_URL = "https://fan-polls.firebaseio.com/";
private static final String COMMENTS_LABEL = "Comments";
private static final String POLLS_LABEL = "Polls";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.activity_discussion);
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    setTitle(R.string.discussion_title_text);

    mDateFormat = new SimpleDateFormat("MM-dd-yyyy");
    mDate = new Date();
    mCurrentDateString = mDateFormat.format(mDate);

    mBaseRef = new Firebase(FIREBASE_URL);
    mPollsRef = mBaseRef.child(POLLS_LABEL);

    mPollImage = (ImageView) findViewById(R.id.comments_image);
    mPollCommentQuestion = (TextView) findViewById(R.id.poll_comment_question);

    mUserComment = (EditText) findViewById(R.id.user_comment);
    mUserAvatar = (ImageView) findViewById(R.id.profile_image_avatar);
    mPollCommentsList = (ListView) findViewById(R.id.poll_comments_list);
    mCommentArrayList = new ArrayList<Comments>();
    mCommentIDArrayList = new ArrayList<String>();
    mCommentAdapter = new ListAdapter(getApplicationContext(), R.layout.individual_comment, mCommentArrayList);
    mPollCommentsList.setAdapter(mCommentAdapter);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    Intent intent = getIntent();
    String pollID = intent.getStringExtra("POLL_ID");
    mPollIndex = intent.getIntExtra("POLL_INDEX", 0);
    mUpdateRef = mPollsRef.child(mCurrentDateString).child(String.valueOf(mPollIndex + 1));
    mCommentsRef = mUpdateRef.child(COMMENTS_LABEL);


    mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            setImage(dataSnapshot);
            setQuestion(dataSnapshot);
            createInitialCommentArray(dataSnapshot);
            mNumberOfCommentsAtPoll = (int) dataSnapshot.child(COMMENTS_LABEL).getChildrenCount();
            for (int i = 0; i < mNumberOfCommentsAtPoll; i++){
                String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("COMMENT").getValue();
                Log.v("COMMENT_ID", "The comment ID is " + commentID);
                String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("USER_ID").getValue();
                Log.v("USER_ID", "The user ID is " + userID);
                mCommentArrayList.add(new Comments(mUserAvatar, userID, commentID));
                mCommentAdapter.notifyDataSetChanged();
            }

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

    //TODO: Store unique comment ID's in an array

    //TODO: Figure out how to programmatically add images to AWS and then store URL in Firebase
    ImageView fab = (ImageView) findViewById(R.id.add_comment);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            HashMap<String, Object> commentMap = new HashMap<String, Object>();
            commentMap.put("USER_ID", mBaseRef.getAuth().getUid());
            commentMap.put("COMMENT", mUserComment.getText().toString());
            mUpdateRef.child(COMMENTS_LABEL).push().updateChildren(commentMap);
            mCommentAdapter.notifyDataSetChanged();

            hideKeyboard(view);
            mUserComment.setText("");
            Toast.makeText(getApplicationContext(),R.string.comment_added, Toast.LENGTH_LONG).show();
        }
    });
}


//How to iterate through a Firbease database
private void createInitialCommentArray(DataSnapshot dataSnapshot) {
    for (DataSnapshot s : dataSnapshot.child(COMMENTS_LABEL).getChildren()) {
        mCommentIDArrayList.add(s.getKey());
        Log.v("COMMENT_ARRAY", "The comment array is " + s.getKey());
    }
}

@Override
protected void onStart() {
    super.onStart();
    mUpdateComments = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            mCommentIDArrayList.add(dataSnapshot.getKey());
            String commentID = (String) dataSnapshot.child("COMMENT").getValue();
            Log.v("New_Comment", "The new comment is " + commentID);
            String userID = (String) dataSnapshot.child("USER_ID").getValue();
            Log.v("New_User_ID", "The new userID is " + userID);
            mCommentArrayList.add(new Comments(mUserAvatar, userID, commentID));
            mCommentAdapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    };

    //TODO: Keep listeners lean; only add listener to the specific root of the data
    mCommentsRef.addChildEventListener(mUpdateComments);

    }

@Override
protected void onStop() {
    super.onStop();
    mCommentsRef.removeEventListener(mUpdateComments);
}

private void setQuestion(DataSnapshot dataSnapshot) {
    String pollQuestion = (String) dataSnapshot.child("Poll_Question").getValue();
    if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
        mPollCommentQuestion.setTextSize(24);
    } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
        mPollCommentQuestion.setTextSize(18);
    } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
        mPollCommentQuestion.setTextSize(14);
    } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
        mPollCommentQuestion.setTextSize(14);
    }
    mPollCommentQuestion.setText(pollQuestion);
}

public void hideKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
    public class ListAdapter extends ArrayAdapter<Comments> {

        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        public ListAdapter(Context context, int resource, List<Comments> items) {
            super(context, resource, items);
        }

        @Override
        public int getCount() {
            return mNumberOfCommentsAtPoll;
        }

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

            View v = convertView;

            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.individual_comment, null);
            }

            Comments p = getItem(position);

            if (p != null) {
                TextView userID = (TextView) v.findViewById(R.id.user_ID);
                TextView userComment = (TextView) v.findViewById(R.id.user_comment);

                if (userID != null) {
                    userID.setText(p.getUserID());
                }

                if (userComment != null) {
                    userComment.setText(p.getUserComment());
                }
            }


            return v;
        }

    }

    private void setImage(DataSnapshot dataSnapshot) {
        String imageURL = (String) dataSnapshot.child("Image").getValue();
        Picasso.with(getApplicationContext())
                .load(imageURL)
                .transform(new RoundedTransformation(10, 6))
                .fit()
                .into((mPollImage));
        mPollImage.setColorFilter(Color.argb(140, 255, 255, 255)); // White Tint
    }

}
String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(pollCount).child("COMMENT").getValue();
String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(pollCount).child("USER_ID").getValue();
@Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(dataSnapshot.getKey()).child("COMMENT").getValue();
            String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(dataSnapshot.getKey()).child("USER_ID").getValue();
            mCommentArrayList.add(new Comments(mUserAvatar, userID, commentID));
            mCommentAdapter.notifyDataSetChanged();
        }